Skip to content

fix zlib throwing memory limit exceeded - #6294

Merged
anonrig merged 1 commit into
mainfrom
yagiz/fix-zlib-memory-lmit-exceeded
Mar 11, 2026
Merged

anonrig merged 1 commit into
mainfrom
yagiz/fix-zlib-memory-lmit-exceeded

Conversation

@anonrig

@anonrig anonrig commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #6286

@anonrig
anonrig requested a review from jasnell March 11, 2026 01:46
@anonrig
anonrig requested review from a team as code owners March 11, 2026 01:46
@ask-bonk

ask-bonk Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

The change looks correct. Let me do a final review of all the pieces.

Summary of my analysis:

  1. Root cause: The do...while loop in syncProcessBuffer continues when avail_out == 0. On the next iteration, addChunk() → grow() fails because the buffer is already at maxCapacity. This is an off-by-one: the output fit exactly but the loop tried to grow anyway.

  2. Fix: Insert a check after adjustUnused() — if avail_out == 0 and the buffer is at max capacity, check whether the stream is finished. If yes, the data fit exactly, so break. If no, the output exceeds the limit, so throw.

  3. isStreamEnd() implementations: Each compression context correctly implements isStreamEnd():

    • ZlibContext: err == Z_STREAM_END — standard zlib end signal
    • BrotliEncoderContext: tracks streamEnd flag set from BrotliEncoderIsFinished()
    • BrotliDecoderContext: lastResult == BROTLI_DECODER_RESULT_SUCCESS
    • ZstdEncoderContext: !ZSTD_isError(lastResult) && lastResult == 0
    • ZstdDecoderContext: !ZSTD_isError(lastResult) && lastResult == 0
  4. No interaction issues with getError(): The getError() check runs before the new check. For all contexts, when the stream is truly done, getError() returns kj::none, so the flow correctly reaches the isStreamEnd() check.

  5. Test coverage: Covers inflateRaw, inflate, gunzip, brotliDecompress, and the too-small case. No zstd test though — but zstd is newer and less commonly used with maxOutputLength.

  6. No backward compat concern: This only fixes a case that previously threw an error incorrectly. No behavior is removed; a previously-broken path now works. No compat flag needed.

The code is clean, correct, and well-tested. There are no actionable issues.

LGTM

github run

@anonrig
anonrig enabled auto-merge March 11, 2026 14:26
@anonrig
anonrig merged commit e3e9c1c into main Mar 11, 2026
25 of 26 checks passed
@anonrig
anonrig deleted the yagiz/fix-zlib-memory-lmit-exceeded branch March 11, 2026 14:42
maschwenk added a commit to maschwenk/workerd that referenced this pull request Nov 21, 2026
zlib.inflateRawSync(data, { maxOutputLength: n }) threw "Memory limit
exceeded" for output of n bytes whenever n was above 40960, unless n
happened to land on the output buffer's growth steps: 50000-, 100000-
and 200000-byte entries all failed, while 81920 worked. adm-zip passes
an entry's exact size this way, so it fails on most entries over 40 KiB.
Any maxOutputLength that leaves less than 40 KiB of room after a full
buffer has the same problem, for every sync and callback zlib, brotli
and zstd function.

The sync loop asks its GrowableBuffer for another 40 KiB chunk each
time the codec fills the buffer, and the buffer refuses to grow past
maxOutputLength at all, rather than grow up to it. cloudflare#6294 fixed the case
where the whole output fits in the first chunk. The chunk the loop asks
for is now capped at maxOutputLength, so the codec gets whatever room is
left, and the existing check after a full buffer at maxOutputLength
decides whether the output fits.

maxOutputLengthAfterBufferGrowth and
zstdMaxOutputLengthAfterBufferGrowthTest decompress 40961 to 200000
bytes with maxOutputLength set to the output size and above it, and
check that one byte less still fails. They fail without this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report — issue in workerd zlib implementation

2 participants