-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
tidb_query: fix UB causing by from_raw_parts
#7635
Conversation
@breeswish PTAL |
@@ -69,11 +71,11 @@ impl RowSlice<'_> { | |||
if let Ok(idx) = non_null_ids.binary_search(&(id as u32)) { | |||
let offset = offsets.get(idx).ok_or(Error::ColumnOffset(idx))?; | |||
let start = if idx > 0 { | |||
offsets[idx - 1] as usize | |||
offsets.get_unchecked(idx - 1) as usize |
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.
There might not be sufficient elements in the offsets (if the data is corrupted). We'd better handle it.
Previously this case result in panic. Now it result in memory issue, which is worse. Better to change it to return error.
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.
I see this conversation marked as resolved, but what was the resolution? Why is this unchecked indexing ok?
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.
Doing an unsafe array access here isn't obviously important to performance. @zhongzc are you sure this needs to be unchecked?
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.
@brson Previous offsets.get(idx)
indicates offsets.get_unchecked(idx - 1)
is okay.
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.
Cool!
2ff256f
to
350d317
Compare
/run-all-tests |
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.
Good job. @brson Would you like to take a look at the improved implementation?
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.
Per previous comments, get_unchecked
should be marked unsafe
. I'd also like to see comment about why get_unchecked
is used during indexing after the binary search.
@@ -69,11 +71,11 @@ impl RowSlice<'_> { | |||
if let Ok(idx) = non_null_ids.binary_search(&(id as u32)) { | |||
let offset = offsets.get(idx).ok_or(Error::ColumnOffset(idx))?; | |||
let start = if idx > 0 { | |||
offsets[idx - 1] as usize | |||
offsets.get_unchecked(idx - 1) as usize |
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.
I see this conversation marked as resolved, but what was the resolution? Why is this unchecked indexing ok?
} | ||
|
||
#[inline] | ||
fn get_unchecked(&self, index: usize) -> T { |
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.
This function should be marked as unsafe since it allows the caller to do unsafe memory access.
@@ -69,11 +71,11 @@ impl RowSlice<'_> { | |||
if let Ok(idx) = non_null_ids.binary_search(&(id as u32)) { | |||
let offset = offsets.get(idx).ok_or(Error::ColumnOffset(idx))?; | |||
let start = if idx > 0 { | |||
offsets[idx - 1] as usize | |||
offsets.get_unchecked(idx - 1) as usize |
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.
Doing an unsafe array access here isn't obviously important to performance. @zhongzc are you sure this needs to be unchecked?
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc <[email protected]>
52355db
to
c7f89f0
Compare
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.
lgtm thanks @zhongzc
/merge |
/run-all-tests |
@zhongzc merge failed. |
/merge |
/run-all-tests |
@zhongzc merge failed. |
/merge |
/run-all-tests |
@zhongzc merge failed. |
Signed-off-by: zhongzc <[email protected]>
cherry pick to release-4.0 in PR #7730 |
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc <[email protected]>
Signed-off-by: zhongzc [email protected]
What problem does this PR solve?
Issue Number: close #7613
Problem Summary:
From stdlib doc:
We didn't provide this promise.
What is changed and how it works?
What's Changed:
Introduce a new type
LEBytes
. Implementbinary_search
for this type by explicitly callingstd::ptr::read_unaligned
.Check List
Tests