Skip to content

Fixes potential out-of-bounds read in decompression#92

Merged
hellobertrand merged 5 commits intomainfrom
fix/decompress-lptr-bounds-check
Feb 13, 2026
Merged

Fixes potential out-of-bounds read in decompression#92
hellobertrand merged 5 commits intomainfrom
fix/decompress-lptr-bounds-check

Conversation

@hellobertrand
Copy link
Copy Markdown
Owner

@hellobertrand hellobertrand commented Feb 12, 2026

This PR implements mathematically proven zero-overhead bounds checking for literal (l_ptr) and destination (d_ptr) streams in the ZXC decompression hot paths, achieving memory safety against crafted inputs while maintaining performance competitive with the original unsafe implementation.

The previous implementation performed per-sequence bounds checks on every iteration, introducing 2-3% performance regression. This PR eliminates overhead by moving checks to the cold path (varint branches) and using loop guards to amortize checks across batches of sequences.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@hellobertrand hellobertrand force-pushed the fix/decompress-lptr-bounds-check branch from 68c8bb5 to e789b77 Compare February 12, 2026 21:36
@hellobertrand hellobertrand changed the title Adds bounds check for literal pointer Fixes potential out-of-bounds read in decompression Feb 12, 2026
@hellobertrand hellobertrand force-pushed the fix/decompress-lptr-bounds-check branch 2 times, most recently from 8ff0854 to e44243c Compare February 12, 2026 22:09
Adds a bounds check for the literal pointer `l_ptr` to prevent out-of-bounds reads.

This ensures that the literal data being copied does not exceed the allocated buffer size during decompression, preventing potential crashes or vulnerabilities.
Ensures `l_ptr` does not exceed `l_end` during decompression to prevent potential out-of-bounds reads. This resolves a possible vulnerability and improves the robustness of the decompression process.
Adds a check to ensure that the literal pointer (l_ptr)
does not exceed the literal end (l_end) during decompression.

This prevents potential out-of-bounds reads in the literal stream
within the fast decompression loops (4x-unrolled and 1x).
It introduces safe thresholds for l_ptr based on the maximum
literal length per sequence, ensuring l_ptr checks are performed
only when necessary, minimizing overhead in hot paths.
Ensures that the match length calculation, when extended via varint, does not lead to out-of-bounds writes during decompression.
@hellobertrand hellobertrand force-pushed the fix/decompress-lptr-bounds-check branch from b7a3f2f to cabe15f Compare February 13, 2026 07:49
Adds a bounds check for the data pointer to prevent out-of-bounds reads, and fixes the offset calculation for match length tokens to ensure correct decompression.
@hellobertrand hellobertrand marked this pull request as ready for review February 13, 2026 10:09
@hellobertrand hellobertrand force-pushed the fix/decompress-lptr-bounds-check branch from 1cd8079 to c6213f8 Compare February 13, 2026 16:53
@hellobertrand hellobertrand merged commit 0d26a42 into main Feb 13, 2026
92 checks passed
@hellobertrand hellobertrand deleted the fix/decompress-lptr-bounds-check branch February 13, 2026 20:23
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.

1 participant