Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minizip: Fix being unable to open empty zip file #778

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions contrib/minizip/unzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ extern int ZEXPORT unzStringFileNameCompare (const char* fileName1,
#define BUFREADCOMMENT (0x400)
#endif

#ifndef CENTRALDIRINVALID
#define CENTRALDIRINVALID ((ZPOS64_T)(-1))
#endif

/*
Locate the Central directory of a zipfile (at the end, just before
the global comment)
Expand All @@ -416,10 +420,10 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f
ZPOS64_T uSizeFile;
ZPOS64_T uBackRead;
ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
ZPOS64_T uPosFound=0;
ZPOS64_T uPosFound=CENTRALDIRINVALID;

if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
return 0;
return CENTRALDIRINVALID;


uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
Expand All @@ -429,7 +433,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f

buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
if (buf==NULL)
return 0;
return CENTRALDIRINVALID;

uBackRead = 4;
while (uBackRead<uMaxBack)
Expand Down Expand Up @@ -459,7 +463,7 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_f
break;
}

if (uPosFound!=0)
if (uPosFound!=CENTRALDIRINVALID)
break;
}
TRYFREE(buf);
Expand All @@ -482,12 +486,12 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib
ZPOS64_T uSizeFile;
ZPOS64_T uBackRead;
ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
ZPOS64_T uPosFound=0;
ZPOS64_T uPosFound=CENTRALDIRINVALID;
uLong uL;
ZPOS64_T relativeOffset;

if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
return 0;
return CENTRALDIRINVALID;


uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
Expand All @@ -497,7 +501,7 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib

buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
if (buf==NULL)
return 0;
return CENTRALDIRINVALID;

uBackRead = 4;
while (uBackRead<uMaxBack)
Expand Down Expand Up @@ -527,47 +531,47 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib
break;
}

if (uPosFound!=0)
if (uPosFound!=CENTRALDIRINVALID)
break;
}
TRYFREE(buf);
if (uPosFound == 0)
return 0;
if (uPosFound == CENTRALDIRINVALID)
return CENTRALDIRINVALID;

/* Zip64 end of central directory locator */
if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
return 0;
return CENTRALDIRINVALID;

/* the signature, already checked */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
return 0;
return CENTRALDIRINVALID;

/* number of the disk with the start of the zip64 end of central directory */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
return 0;
return CENTRALDIRINVALID;
if (uL != 0)
return 0;
return CENTRALDIRINVALID;

/* relative offset of the zip64 end of central directory record */
if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
return 0;
return CENTRALDIRINVALID;

/* total number of disks */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
return 0;
return CENTRALDIRINVALID;
if (uL != 1)
return 0;
return CENTRALDIRINVALID;

/* Goto end of central directory record */
if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
return 0;
return CENTRALDIRINVALID;

/* the signature */
if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
return 0;
return CENTRALDIRINVALID;

if (uL != 0x06064b50)
return 0;
return CENTRALDIRINVALID;

return relativeOffset;
}
Expand Down Expand Up @@ -621,7 +625,7 @@ local unzFile unzOpenInternal (const void *path,
return NULL;

central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
if (central_pos)
if (central_pos!=CENTRALDIRINVALID)
{
uLong uS;
ZPOS64_T uL64;
Expand Down Expand Up @@ -683,7 +687,7 @@ local unzFile unzOpenInternal (const void *path,
else
{
central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
if (central_pos==0)
if (central_pos==CENTRALDIRINVALID)
err=UNZ_ERRNO;

us.isZip64 = 0;
Expand Down