Prevents out-of-bounds memory access#129
Merged
hellobertrand merged 6 commits intomainfrom Mar 5, 2026
Merged
Conversation
Adds bounds checks to ensure that optimized 16-byte and 32-byte copies do not read beyond the end of the input buffer. This resolves potential undefined behavior when processing literals near the buffer boundary, as identified by sanitizers.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
274fa80 to
760e415
Compare
Updates the safe threshold calculation in the decompression loop to include ZXC_PAD_SIZE. This ensures that optimized 32-byte copies do not write past the end of the destination buffer, resolving potential out-of-bounds access detected by sanitizers. Prevents out-of-bounds writes using a padded scratch buffer Optimized 32-byte wild copies used during decompression can overshoot the destination buffer by up to ZXC_PAD_SIZE bytes. This change introduces an intermediate padded scratch buffer to safely contain these overshoots before performing a precise copy to the final output buffer.
The bit reader's fast path uses `zxc_le64`, which always performs an 8-byte load. Previously, this path could be triggered when the buffer had enough data to satisfy the bit requirement but fewer than 8 bytes remaining, leading to a potential over-read. This change ensures the fast path is only used when a full 8-byte read is safe and correctly bounds partial reads to the number of bytes required.
The block decoding path previously validated the destination buffer's bounds but lacked corresponding checks for the source literal stream. This change adds bounds checks to ensure that both optimized wild copies and the fallback exact copy do not read past the end of the input buffer.
Reuses the padded scratch buffer across decompression calls by moving it into the context. This reduces allocation overhead and simplifies memory management in error paths.
Verifies the scratch buffer logic used to handle wild-copy overshoots during decompression. Includes test cases for exact-fit buffers, tiny data, and malformed input to ensure memory safety and prevent regressions.
760e415 to
5c222d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enhances memory safety within the compression and decompression routines by addressing several potential out-of-bounds read and write scenarios.
Specifically: