Skip to content
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

slice: avoid calling memcmp with size 0 #113435

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ where

// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
// The two slices have been checked to have the same size above.
// We ensure `memcp` is not called on size 0 (since we are not sure references are
// sufficiently valid for a C function in that case).
unsafe {
let size = mem::size_of_val(self);
memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
size == 0 || memcmp(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
}
}
}
Expand Down Expand Up @@ -192,9 +194,15 @@ impl SliceOrd for u8 {
let diff = left.len() as isize - right.len() as isize;
// This comparison gets optimized away (on x86_64 and ARM) because the subtraction updates flags.
let len = if left.len() < right.len() { left.len() } else { right.len() };
if len == 0 {
// At least one is empty, the length difference decides.
return diff.cmp(&0);
}
// SAFETY: `left` and `right` are references and are thus guaranteed to be valid.
// We use the minimum of both lengths which guarantees that both regions are
// valid for reads in that interval.
// We ensure `memcp` is not called on size 0 (since we are not sure references are
// sufficiently valid for a C function in that case).
let mut order = unsafe { memcmp(left.as_ptr(), right.as_ptr(), len) as isize };
if order == 0 {
order = diff;
Expand Down