madler / zlib

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

inflate behavior -O0 vs -O2 #990

Closed brianfromoregon closed 2 months ago

brianfromoregon commented 2 months ago

Hi

I am using zlib 1.2.13. The usage code is here (I am a user but not the author): https://github.com/Razvi99/fastCSV/blob/master/lib/fastCSV/gzipReadBuffer.hpp#L95

With -O0 the very first call to inflate returns Z_OK. The fastcsv code reads the entire file (a few GB) and all is well. With -O2 the very first call to inflate returns Z_STREAM_END and the fastcsv code goes in a infloop. Everything else is the same, just running cmake with release vs. debug build. Is this expected? Is there a known issue fixed in latest 1.3.1?

Thanks Brian

brianfromoregon commented 2 months ago

I'm sure it's a little confusing to see the #include "../zlib/zlib.h" in that linked source file. I removed that line in my copy. My zlib is coming from conda.

madler commented 2 months ago

Side note on the code: you should not put anything with side effects, i.e. anything that needs to execute, inside an assert(). It is common for assert()'s to be active while testing, but then defined away in the production compile. E.g., your:

    assert(inflateInit2(&inflator, 15 + 16) == 0);

should be:

    int ret = inflateInit2(&inflator, 15 + 16);
    assert(ret == Z_OK);
brianfromoregon commented 2 months ago

Great spot! Yeah that's what is going on. I'll close this.

madler commented 2 months ago

Is it? You tested it with the asserts fixed?

brianfromoregon commented 2 months ago

Yep :)