Skip to content

[#1752] Fix infinite loop in pgmoneta_gunzip_string on truncated input - #1753

Merged
ecleon26 merged 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/gunzip-string-infinite-loop
Dec 1, 2026
Merged

ecleon26 merged 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/gunzip-string-infinite-loop

Conversation

@hardikkaurani

@hardikkaurani hardikkaurani commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1752

Summary

  • Fix an infinite loop and denial of service in pgmoneta_gunzip_string() (src/libpgmoneta/gzip_compression.c) when processing empty buffers, truncated gzip streams, or corrupt payloads.
  • The decompression loop while (ret == Z_OK || ret == Z_BUF_ERROR); continued without checking stream.avail_out == 0. When stream.avail_in == 0 before Z_STREAM_END, in-memory decompression cannot make further progress, causing inflate() to continuously return Z_BUF_ERROR and spin forever at 100% CPU.
  • Guard the retry condition to while (ret == Z_OK || (ret == Z_BUF_ERROR && stream.avail_out == 0)); so Z_BUF_ERROR is only retried when the output buffer was completely exhausted and needs enlargement.
  • Add input parameter validation in pgmoneta_gunzip_string() (checking NULL pointers, compressed_size == 0, and compressed_size > UINT_MAX) and pgmoneta_gzip_string().
  • Add regression test test_compression_gunzip_string_truncated_and_empty in test/testcases/test_compression.c validating NULL handling, 0-byte input, truncated gzip headers, truncated gzip streams, and full round-trip compression and decompression.

Verification

  • Formatted src/libpgmoneta/gzip_compression.c and test/testcases/test_compression.c with clang-format.
  • Verified formatting with clang-format --dry-run --Werror.
  • GitHub Actions CI builds and runs the full MCTF test suite across Linux and macOS environments.

Copilot AI balanced review requested due to automatic review settings November 29, 2026 22:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @jesperpedersen, @Pranav-error,

This PR addresses an issue where pgmoneta_gunzip_string() could hang indefinitely in an infinite loop consuming 100% CPU when passed an empty buffer, truncated stream, or corrupt payload.

Since compressed_buffer is an in-memory buffer, once stream.avail_in reaches 0 without reaching Z_STREAM_END, calling inflate(&stream, Z_NO_FLUSH) repeatedly returns Z_BUF_ERROR. Because the loop condition checked while (ret == Z_OK || ret == Z_BUF_ERROR); without verifying stream.avail_out == 0, the loop never terminated.

Key updates:

  • Constrained the retry condition to while (ret == Z_OK || (ret == Z_BUF_ERROR && stream.avail_out == 0)); so Z_BUF_ERROR is only retried when the output buffer is fully exhausted and requires expansion.
  • Added parameter validation in pgmoneta_gunzip_string() and pgmoneta_gzip_string().
  • Added unit tests in test/testcases/test_compression.c covering empty buffers, truncated headers, truncated streams, and round-trip verification.

Could you please review when you have a moment? Thanks!

@Pranav-error

Copy link
Copy Markdown
Collaborator

CI fails on the new test itself in all 6 clang Debug jobs: it expects pgmoneta_gunzip_string to fail, which logs GUNzip: Decompression failed, and MCTF fails a non-negative test that logs errors. Registering it with MCTF_TEST_NEGATIVE, like the other failure tests in test_compression.c, should fix that.

Locally with ASan it passes as MCTF_TEST_NEGATIVE, and with only the two while conditions reverted it hangs, so it covers #1752.

…ted input

When decompressing gzip strings, pgmoneta_gunzip_string() loops on
inflate() with:
    while (ret == Z_OK || ret == Z_BUF_ERROR);
When provided with an empty buffer, truncated stream, or corrupt payload
where stream.avail_in reaches 0 before Z_STREAM_END, inflate() returns
Z_BUF_ERROR. Because stream.avail_out > 0 and no more input can be
provided, the function spins in an infinite loop consuming 100% CPU.

Fix this by only retrying on Z_BUF_ERROR when stream.avail_out == 0
(indicating more output buffer capacity is needed). Also add parameter
validation in pgmoneta_gunzip_string() and pgmoneta_gzip_string(), and
add unit tests covering empty, truncated, and round-trip decompression.

Fixes pgmoneta#1752

Signed-off-by: Hardik Kaurani <[email protected]>
@hardikkaurani
hardikkaurani force-pushed the fix/gunzip-string-infinite-loop branch from 0820671 to 1c1a843 Compare November 30, 2026 16:02
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks for catching that! Updated test_compression_gunzip_string_truncated_and_empty in test/testcases/test_compression.c to use MCTF_TEST_NEGATIVE (1c1a8435) and rebased onto main.

@ecleon26
ecleon26 merged commit 7ec8d8a into pgmoneta:main Dec 1, 2026
13 checks passed
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks @ecleon26

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.

Fix infinite loop in pgmoneta_gunzip_string on truncated or empty input

4 participants