Skip to content

[#1780] Guard NULL pointers, overflow, and freed outputs in ZSTD string helpers - #1781

Open
hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/zstd-string-null-and-dangling-ptr
Open

hardikkaurani wants to merge 1 commit into
pgmoneta:mainfrom
hardikkaurani:fix/zstd-string-null-and-dangling-ptr

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Closes #1780

Summary

  • Validate s, buffer, and buffer_size for NULL in pgmoneta_zstdc_string() (src/libpgmoneta/zstandard_compression.c), initialize *buffer = NULL and *buffer_size = 0, and reset *buffer = NULL after free(*buffer) on error.
  • Validate compressed_buffer and output_string for NULL in pgmoneta_zstdd_string(), initialize *output_string = NULL, guard decompressed_size > SIZE_MAX - 1, and reset *output_string = NULL after free(*output_string) on decompression failure.
  • Add MCTF_TEST_NEGATIVE(test_zstd_string_null_and_invalid) in test/testcases/test_compression.c.

Verification

  • Verified formatting with clang-format --dry-run --Werror.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:41

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

@jesperpedersen @Pranav-error Hi, this PR addresses #1780 in src/libpgmoneta/zstandard_compression.c:

  • Validates s, buffer, and buffer_size in pgmoneta_zstdc_string() and compressed_buffer / output_string in pgmoneta_zstdd_string() for NULL before dereferencing.
  • Adds a decompressed_size > SIZE_MAX - 1 overflow check before malloc(decompressed_size + 1) in pgmoneta_zstdd_string().
  • Initializes output pointers upfront and resets *buffer = NULL / *output_string = NULL after free() on error paths.
  • Adds MCTF_TEST_NEGATIVE(test_zstd_string_null_and_invalid) in test/testcases/test_compression.c.

Formatting passes clang-format --dry-run --Werror and the PR is a single signed-off commit.

size_t decompressed_size;
size_t result;

if (compressed_buffer == NULL || output_string == 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.

remove

@hardikkaurani hardikkaurani Dec 1, 2026 •

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.

Removed the SIZE_MAX check in d1bb259.

@ecleon26

ecleon26 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

@hardikkaurani good catch

Remove the SIZE_MAX check, the two sentinel values are already handled

pgmoneta_bzip2_string() has the same problem. Do all the compression string functions in one PR together with #1779

…LZ4, and BZIP2 string helpers

Signed-off-by: Hardik Kaurani <[email protected]>
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@ecleon26 Updated in d1bb2592:

  • Removed the SIZE_MAX check in pgmoneta_zstdd_string().
  • Combined the NULL pointer and dangling output pointer fixes for pgmoneta_zstdc_string() / pgmoneta_zstdd_string(), pgmoneta_lz4c_string() / pgmoneta_lz4d_string() (from #1779), and pgmoneta_bzip2_string() into this single commit.
  • Added test_zstd_string_null_and_invalid, test_lz4_string_null_and_invalid, and test_bzip2_string_null_and_invalid in test/testcases/test_compression.c.

@ecleon26

ecleon26 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

@hardikkaurani Please Rebase

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@ecleon26 Rebased onto main!

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.

zstandard_compression: missing NULL checks and dangling output pointers in string helpers

3 participants