madler / zlib

A massively spiffy yet delicately unobtrusive compression library.
http://zlib.net/
Other
5.71k stars 2.45k forks source link

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

Closed RedworkDE closed 1 year ago

RedworkDE commented 1 year ago

Fixes #199

The problem is that minizip uses the offset 0 to mark the failure to find the central directory of the zip file, but as seen in the linked issue, an empty zip file can have the central directory at offset 0.

To fix the problem, this patch uses a newly introduced constant CENTRALDIRINVALID instead. It has the value -1, which when converted to an unsigned type, results in the largest representable value[^1], i.e. 0xffffffffffffffff in this case. This value can never be a valid offset into a file, because it also the the maximal possible length of a file and thus not a valid offset.

Compatibility: This change should be fully backwards compatible, even if ZPOS64_T is defined to be a different size / signed.[^2]

[^1]: In a surprising move for C, this conversion is actually well defined and not just something that happens to work, see ANSI C 6.2.1.2. [^2]: This is the main motivation to using -1 instead of using 0xffffffffffffffff directly as the invalid value.

Neustradamus commented 1 year ago

@gvollant: Can you look?

gvollant commented 1 year ago

@Neustradamus @madler the fix look fine for me, can be merged