Skip to content

fix zlib throwing memory limit exceeded#6294

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

fix zlib throwing memory limit exceeded#6294
anonrig merged 1 commit intomainfrom
yagiz/fix-zlib-memory-lmit-exceeded

Conversation

@anonrig
Copy link
Member

@anonrig anonrig commented Mar 11, 2026

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
Copy link
Contributor

ask-bonk bot commented Mar 11, 2026

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
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