[MEDIUM] Bound binary array allocation by payload length - #743
Open
OskarEichler wants to merge 1 commit into
Open
[MEDIUM] Bound binary array allocation by payload length#743OskarEichler wants to merge 1 commit into
OskarEichler wants to merge 1 commit into
Conversation
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
PG::BinaryDecoder::Arraytrusts declared dimensions when preallocating its first Ruby array. A tiny malformed binary value can therefore request a very large allocation before the decoder discovers that the element data is missing.This change verifies that the remaining payload contains at least the four-byte length word required for every declared element before allocating the result arrays. Valid payloads are unchanged, and malformed payloads keep the existing
premature enderror.Reproduction
The following 20-byte value declares 10,000,000 elements but contains none:
With current
master, Ruby'sGC.stat(:malloc_increase_bytes)rose by approximately 80 MiB beforeArgumentErrorwas raised. With this patch it rose by about 11 KiB and raised the same error at the same position.Verification
pg_service/libpq 15 failures, 9 pending.git diff --checkpasses.Limitations
The focused allocation measurement used Ruby's allocator counters on macOS. No new automated spec is included.
Breaking changes
None. Payloads that cannot contain their declared element count already raise
ArgumentError; they now do so before disproportionate allocation.