Skip to content

Commit fd28c1d

Browse files
committed
Enhance _dosToUnixTime to handle zero and invalid datetime values; add tests for edge cases. Fixes #65
1 parent a141562 commit fd28c1d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

lib/IO/Uncompress/Unzip.pm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,14 @@ sub filterUncompressed
802802
# from Archive::Zip & info-zip
803803
sub _dosToUnixTime
804804
{
805+
# Returns zero when $dt is already zero or it doesn't expand to a value that Time::Local::timelocal()
806+
# can handle.
807+
805808
my $dt = shift;
809+
# warn "_dosToUnixTime dt=[$dt]\n";
810+
811+
# some zip files don't populate the datetime field at all
812+
return 0 if ! $dt;
806813

807814
my $year = ( ( $dt >> 25 ) & 0x7f ) + 80;
808815
my $mon = ( ( $dt >> 21 ) & 0x0f ) - 1;
@@ -813,10 +820,15 @@ sub _dosToUnixTime
813820
my $sec = ( ( $dt << 1 ) & 0x3e );
814821

815822
use Time::Local ;
816-
my $time_t = Time::Local::timelocal( $sec, $min, $hour, $mday, $mon, $year);
823+
824+
my $time_t ;
825+
# wrap in an eval to catch out of range errors
826+
eval {
827+
$time_t = Time::Local::timelocal( $sec, $min, $hour, $mday, $mon, $year);
828+
} ;
829+
817830
return 0 if ! defined $time_t;
818831
return $time_t;
819-
820832
}
821833

822834
#sub scanCentralDirectory

t/files/time-invalid.zip

122 Bytes
Binary file not shown.

t/files/time-zero.zip

122 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)