-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Prevent FixedSizeBinaryArray::value offset truncation
#9850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ pub struct FixedSizeBinaryArray { | |
| value_data: Buffer, | ||
| nulls: Option<NullBuffer>, | ||
| len: usize, | ||
| value_length: i32, | ||
| value_length: i32, // validated to be a valid usize by `try_new` | ||
| } | ||
|
|
||
| impl FixedSizeBinaryArray { | ||
|
|
@@ -169,12 +169,11 @@ impl FixedSizeBinaryArray { | |
| self.len() | ||
| ); | ||
| let offset = i + self.offset(); | ||
| let value_length = self.value_length as usize; // validated in Self::try_new | ||
| let value_offset = offset.checked_mul(value_length).expect("offset overflow"); | ||
| // SAFETY: value offset/length computed correctly using checked arithmetic | ||
| unsafe { | ||
| let pos = self.value_offset_at(offset); | ||
| std::slice::from_raw_parts( | ||
| self.value_data.as_ptr().offset(pos as isize), | ||
| (self.value_offset_at(offset + 1) - pos) as usize, | ||
| ) | ||
| std::slice::from_raw_parts(self.value_data.as_ptr().add(value_offset), value_length) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -186,7 +185,7 @@ impl FixedSizeBinaryArray { | |
| /// # Safety | ||
| /// | ||
| /// Caller is responsible for ensuring that the index is within the bounds | ||
| /// of the array | ||
| /// of the array and the resulting byte offset fits in `i32` | ||
| pub unsafe fn value_unchecked(&self, i: usize) -> &[u8] { | ||
| let offset = i + self.offset(); | ||
| let pos = self.value_offset_at(offset); | ||
|
|
@@ -486,6 +485,8 @@ impl FixedSizeBinaryArray { | |
| }) | ||
| } | ||
|
|
||
| /// Returns the byte offset for the element at index `i` without | ||
| /// checking for overflow. | ||
| #[inline] | ||
| fn value_offset_at(&self, i: usize) -> i32 { | ||
| self.value_length * i as i32 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this arithmetic can overflow if the result is larger than |
||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add new methods that are alternatives to
value_offsetandvalue_offset_atthat returnusize, so we don't need this limitation? Or at least update this method so it doesn't use them and doesn't suffer from i32 overflow. Because with this change,valuenow handles whenvalue_offsetis greater than max i32, butvalue_uncheckedstill doesn't.I would expect
value_uncheckedto work correctly for all cases wherevaluedoesn't panic.And existing code that uses
value_uncheckedmight validate the index but not be aware of this hidden safety requirement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer is yes. I also spent some more time reviewing the code in FixedSizeBinaryArray and I am now convinced there are several other miuses of
i32<->usize. I am working on an improvement, though I worry it will be a larger PRThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FixedSizeBinaryArrayi32offset overflows (try 2) #9872