pfalcon / uzlib

Radically unbloated DEFLATE/zlib/gzip compression/decompression library. Can decompress any gzip/zlib data, and offers simplified compressor which produces gzip-compatible output, while requiring much less resources (and providing less compression ratio of course).
Other
303 stars 82 forks source link

Sequencing Error in "tinf_inflate_uncompressed_block" in "tinflate.c" #8

Closed dv-extrarius closed 6 years ago

dv-extrarius commented 6 years ago

In the file "tinflate.c", the function "tinf_inflate_uncompressed_block" contains two sequencing errors that led to a huge debugging effort on my part.

The line length = uzlib_get_byte(d) + 256 * uzlib_get_byte(d); will produce incorrect results if the second function call is executed first (which is perfectly valid according to the C standard, and is what some versions of Microsoft's compiler do when given certain optimization flags). Unfortunately, in my case at least, the compiler ordered the reads for "invlength" in the same way, so the comparison between length and invlength still passes, but compression either fails or results in incorrect output.

The correct sequence, which ensures the two-byte values are read in little-endian order is:

length = uzlib_get_byte(d);
length += 256 * uzlib_get_byte(d);
invlength = uzlib_get_byte(d);
invlength += 256 * uzlib_get_byte(d);
pfalcon commented 6 years ago

Thanks for the report, fixed in d4e4a4aa06a648598e6967072b5be68ba87a4ee5.