Add comprehensive test coverage and fix inflate null termination#1
Conversation
Expand test suite from 4 to 9 tests: exact round-trip equality, all ZLevel variants, and larger input. Fix missing null terminator in inflate output that the old prefix-only check was hiding.
There was a problem hiding this comment.
Build & Tests
Both CI checks pass (macOS + Ubuntu). Built and ran tests locally — all 9 tests pass.
Findings
-
realloc failure not handled (
zlib_helper.h:122). Ifreallocreturns NULL, line 123 dereferences it. However, this is the same pattern as every other realloc in the file (lines 112, 177) — not a regression, just pre-existing. -
Tests use
unsafe-from-success(tests/zlib.carp:13,17,28,33,38,43,52). Ifdeflateever fails, the test will abort rather than fail gracefully. This matches the pre-existing test style and is a minor style point, not a blocker. -
The null-termination fix is correct. At the point where the new code runs,
bytes->lenholds the exact decompressed output size (accumulated in the inner loop at line 114). Therealloctolen+1and null-terminator placement are right. Without this fix,ZRes_strreturns a string with trailing garbage because Carp treats the buffer as a null-terminated C string. Real bug, good fix. -
lencannot be 0 at the fix point. The success path (ret == Z_STREAM_END) only fires afterinflate()has produced output, so the realloc to0+1scenario doesn't arise. -
Empty string deflation crash (noted in PR description) is not triggered by any new test, and lives in
deflate_c(the assertion at line 184), not in the code this PR touches. Correctly left as a separate issue.
Verdict: merge
Clean, focused change. The inflate null-termination bug was real and the fix is correct. Test expansion from prefix-only to exact round-trip checks is a meaningful improvement. No regressions.
Summary
starts-with?check)ZLevelvariants:NoCompression,BestSpeed,BestCompression,DefaultCompressionZLib_inflate_coutput — the inflated string buffer was not null-terminated, soZRes_strreturned a string with trailing garbage. The old prefix-only test hid this bug.Note: deflating an empty string crashes (assertion failure in
ZLib_deflate_c). That's a separate issue — this PR doesn't attempt to fix it.Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.