fix silent truncation of long literal runs in inflate - #295
Open
spokodev wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
inflateSync(and the other auto-sizing inflate entry points) can return truncated output, with no error, for a valid DEFLATE stream thatzlibandpakoboth decode in full.The output buffer is grown at two points in
inflt: at each block start and before each match, both reservingbt + 131072. A literal, though, is written straight tobuf[bt++]with no capacity check. A block that emits a long run of literals with no intervening match runs past that reservation — the out-of-rangeUint8Arraywrites are silently dropped whilebtkeeps counting, and the finalsubarray(0, bt)clamps back to the buffer length, so the tail is lost without a thrown error.Repro
A raw-DEFLATE block whose literal/length alphabet is just
{0x41, EOB}at one bit each encodes every byte in a single bit, so the output is ~8× the input and outgrows thesl * 3initial buffer:The harness crafts the stream directly;
zlib.inflateRawSyncandpako.inflateRaware the oracles, and both return the full output for every case.Fix
Recheck capacity on the literal path the way the match path already does, and only when resizing:
resizeis false when the caller passes a fixedoutbuffer, so that path and its documented exact-size behaviour are untouched; and a literal is the only thing that advancesbtby one without a reservation, sobt == buf.lengthis the exact overflow point.Cost is negligible. Best-of-3 over 8 MB payloads: realistic text (72× ratio) auto-size decode moved 11.36 → 11.53 ms/op with the user-buffer path unchanged; a literal-heavy payload (4.5× ratio) stayed within run-to-run noise. The known-size (
out-buffer) fast path takes the branch zero times.Test
test/6-inflate.tscrafts the literal-run stream and assertsinflateSyncmatcheszlib.inflateRawSyncin length and content. It fails onmaster(375039 vs 1000000) and passes with the fix.