madler / zlib

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

Behavior after error in deflateInit #858

Closed lgalfaso closed 11 months ago

lgalfaso commented 11 months ago

It is not clear whether deflateEnd should be called whenever an error is returned from deflateInit. Looking at the implementation of compress it looks like it should not be called, but given that deflateInit makes some allocations, then an error in deflateInit looks like it will leak.

Note: the only way I can see this happening is if some of the allocations succeed and some do not, so this would be very unlikely.

madler commented 11 months ago

No, you do not need to call deflateEnd(), and no, deflateInit() will not leak. deflateInit() frees all memory successfully allocated so far upon an allocation failure. (See https://github.com/madler/zlib/blob/develop/deflate.c#L507 .)

Nothing bad will happen if you do, nevertheless, call deflateEnd() after deflateInit() returns an error. You will simply get a Z_STREAM_ERROR return code.

So, either is fine.