Implement overflow check in MaxCompressedLength function#242
Open
yusmer96-maker wants to merge 3 commits into
Open
Implement overflow check in MaxCompressedLength function#242yusmer96-maker wants to merge 3 commits into
yusmer96-maker wants to merge 3 commits into
Conversation
Add overflow check for buffer allocation in MaxCompressedLength
danilak-G
reviewed
Jun 4, 2026
| size_t MaxCompressedLength(size_t source_bytes) { | ||
| // Avoid integer overflow that could cause undersized buffer allocations. | ||
| // Return SIZE_MAX to force a controlled allocation failure. | ||
| if (source_bytes > (SIZE_MAX - 32) / 7 * 6) { |
Collaborator
There was a problem hiding this comment.
LGTM. Can you use std::numeric_limits<size_t>::max() ?
Author
There was a problem hiding this comment.
Sure! I initially used SIZE_MAX because I didn't notice was included transitively through snappy-stubs-internal.h. Now that I see it's available, I'll update to std::numeric_limits<size_t>::max(). Thanks for the suggestion.
Updated the overflow check to use std::numeric_limits<size_t>::max() for clarity.
danilak-G
reviewed
Jun 4, 2026
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <cstring> | ||
| #include <memory> |
Collaborator
There was a problem hiding this comment.
Let's have #include <limits> here as well
Author
There was a problem hiding this comment.
Done, added #include <limits>
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.
Summary
Add integer overflow check in
MaxCompressedLength()to preventheap buffer overflow vulnerabilities.
Problem
The expression
32 + source_bytes + source_bytes / 6can overflowwhen
source_bytesis very large, resulting in a small return value.Callers use this value to allocate buffers, so an overflow leads to
undersized allocations and subsequent heap buffer overflows during
compression.
Fix
Added a bounds check before the calculation. If
source_bytesexceedsthe safe threshold
(SIZE_MAX - 32) / 7 * 6, the function returnsSIZE_MAX, which forces a controlled allocation failure instead ofsilent memory corruption.