Skip to content

[#1758] Fix buffer expansion and strict aliasing in pgmoneta_bunzip2_string - #1759

Open
hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/1758-bunzip2-buffer-expansion
Open

hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/1758-bunzip2-buffer-expansion

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Closes #1758

Summary

  • In src/libpgmoneta/bzip2_compression.c, handle BZ_OUTBUFF_FULL in pgmoneta_bunzip2_string() by dynamically expanding the buffer capacity and retrying, allowing high-ratio compressed payloads to decompress successfully.
  • Pass dest_len as a properly typed unsigned int* to BZ2_bzBuffToBuffDecompress() to comply with strict aliasing and prevent partial 4-byte writes into a 64-bit size_t variable.
  • Add input parameter validation and error-path output initialization in pgmoneta_bzip2_string() and pgmoneta_bunzip2_string(), and ensure explicit null-termination of the decompressed output string.
  • Add regression tests in test/testcases/test_compression.c (test_compression_bzip2_string_bounds_and_expansion) covering parameter validation, normal round-trip decompression, buffer expansion on a stream with a > 20:1 compression ratio, and corrupted streams.

Verification

  • Formatted src/libpgmoneta/bzip2_compression.c and test/testcases/test_compression.c with clang-format (clang-format --dry-run --Werror passed).
  • Verified git diff --check with 0 whitespace warnings.
  • Rebased on latest upstream/main as a single signed-off commit.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 07:45

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 and @Pranav-error,

I have opened this PR to address #1758 in src/libpgmoneta/bzip2_compression.c:

  • Dynamic buffer expansion on BZ_OUTBUFF_FULL: In pgmoneta_bunzip2_string(), the decompression buffer previously had a fixed allocation capped at 20 * compressed_size. Highly compressible text, repetitive logs, or structured payloads exceeding a 20:1 ratio returned BZ_OUTBUFF_FULL and failed. The decompressor now dynamically doubles the buffer capacity and retries on BZ_OUTBUFF_FULL until decompression completes.
  • Strict aliasing & pointer typing: Replaced the (unsigned int*)&size cast (passing a pointer to a 64-bit size_t variable to BZ2_bzBuffToBuffDecompress) with an explicitly typed unsigned int dest_len, avoiding partial writes and strict aliasing violations on 64-bit architectures.
  • Parameter validation & null-termination: Added guards for NULL pointers and 0-size inputs in both pgmoneta_bzip2_string() and pgmoneta_bunzip2_string(), with safe error-path output initialization and guaranteed null-termination of the decompressed string.
  • Regression test: Added test_compression_bzip2_string_bounds_and_expansion in test/testcases/test_compression.c covering parameter validation, normal round-trip, high-ratio stream decompression requiring dynamic buffer expansion, and corrupted stream rejection.

Formatted with clang-format and rebased onto latest main as a single signed-off commit (2746a1f7). Please let me know if any adjustments are needed.

@Pranav-error

Copy link
Copy Markdown
Collaborator

Same CI failure as #1753: test_compression_bzip2_string_bounds_and_expansion expects a failed decompress, which logs Bzip2: Decompress failed, and MCTF fails a non-negative test that logs errors. MCTF_TEST_NEGATIVE should fix it.

@hardikkaurani
hardikkaurani force-pushed the fix/1758-bunzip2-buffer-expansion branch from 2746a1f to e9f210c Compare November 30, 2026 16:04
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Good catch — updated test_compression_bzip2_string_bounds_and_expansion in test/testcases/test_compression.c to use MCTF_TEST_NEGATIVE so the expected error log on corrupted input is handled properly, squashed and rebased onto main.

@ecleon26
ecleon26 self-requested a review December 1, 2026 19:56

@ecleon26 ecleon26 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

#1781 has the same pgmoneta_bzip2_string() changes, remove them from #1781, so the bzip2 work is only here. also rebase

Comment on lines +1062 to +1073
/* High compression ratio requiring buffer expansion (ratio > 20x) */
large_string = malloc(large_len + 1);
MCTF_ASSERT_PTR_NONNULL(large_string, cleanup, "allocation for large test string failed");
while (pos + pattern_len <= large_len)
{
memcpy(large_string + pos, pattern, pattern_len);
pos += pattern_len;
}
large_string[pos] = '\0';

MCTF_ASSERT_INT_EQ(pgmoneta_bzip2_string(large_string, &bz, &bz_size), 0, cleanup, "bzip2 compression of large payload failed");
MCTF_ASSERT_PTR_NONNULL(bz, cleanup, "compressed buffer is NULL");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Split this

the round trip and expansion should be a MCTF_TEST, and only the invalid stream / NULL cases in MCTF_TEST_NEGATIVE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ecleon26 Split into MCTF_TEST(test_compression_bzip2_string_roundtrip_and_expansion) and MCTF_TEST_NEGATIVE(test_compression_bzip2_string_invalid_and_null) in 391afddd.

@hardikkaurani
hardikkaurani force-pushed the fix/1758-bunzip2-buffer-expansion branch from e9f210c to 391afdd Compare December 1, 2026 20:12
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@ecleon26 Split test_compression_bzip2_string_bounds_and_expansion in test/testcases/test_compression.c (391afddd) into MCTF_TEST(test_compression_bzip2_string_roundtrip_and_expansion) for the round-trip/expansion cases and MCTF_TEST_NEGATIVE(test_compression_bzip2_string_invalid_and_null) for the invalid stream / NULL cases.

@ecleon26

ecleon26 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

@hardikkaurani Please rebase

@hardikkaurani
hardikkaurani force-pushed the fix/1758-bunzip2-buffer-expansion branch from 391afdd to 3b10211 Compare December 2, 2026 21:26
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@ecleon26 Rebased onto main (3b102116).

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.

pgmoneta_bunzip2_string fails on high compression ratios and violates strict aliasing

4 participants