mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 09:23:05 +00:00
Fix binary_search_by()
overflow issue in ZST case
This commit is contained in:
parent
385ad48b35
commit
3eb5bee242
@ -2154,11 +2154,12 @@ impl<T> [T] {
|
|||||||
where
|
where
|
||||||
F: FnMut(&'a T) -> Ordering,
|
F: FnMut(&'a T) -> Ordering,
|
||||||
{
|
{
|
||||||
|
let mut size = self.len();
|
||||||
let mut left = 0;
|
let mut left = 0;
|
||||||
let mut right = self.len();
|
let mut right = size;
|
||||||
while left < right {
|
while left < right {
|
||||||
// never overflow because `slice::len()` max is `isize::MAX`.
|
let mid = left + size / 2;
|
||||||
let mid = (left + right) / 2;
|
|
||||||
// SAFETY: the call is made safe by the following invariants:
|
// SAFETY: the call is made safe by the following invariants:
|
||||||
// - `mid >= 0`
|
// - `mid >= 0`
|
||||||
// - `mid < size`: `mid` is limited by `[left; right)` bound.
|
// - `mid < size`: `mid` is limited by `[left; right)` bound.
|
||||||
@ -2174,6 +2175,8 @@ impl<T> [T] {
|
|||||||
} else {
|
} else {
|
||||||
return Ok(mid);
|
return Ok(mid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size = right - left;
|
||||||
}
|
}
|
||||||
Err(left)
|
Err(left)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use core::cell::Cell;
|
use core::cell::Cell;
|
||||||
|
use core::cmp::Ordering;
|
||||||
use core::result::Result::{Err, Ok};
|
use core::result::Result::{Err, Ok};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -64,6 +65,17 @@ fn test_binary_search() {
|
|||||||
assert_eq!(b.binary_search(&6), Err(4));
|
assert_eq!(b.binary_search(&6), Err(4));
|
||||||
assert_eq!(b.binary_search(&7), Ok(4));
|
assert_eq!(b.binary_search(&7), Ok(4));
|
||||||
assert_eq!(b.binary_search(&8), Err(5));
|
assert_eq!(b.binary_search(&8), Err(5));
|
||||||
|
|
||||||
|
let b = [(); usize::MAX];
|
||||||
|
assert_eq!(b.binary_search(&()), Ok(usize::MAX / 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_binary_search_by_overflow() {
|
||||||
|
let b = [(); usize::MAX];
|
||||||
|
assert_eq!(b.binary_search_by(|_| Ordering::Equal), Ok(usize::MAX / 2));
|
||||||
|
assert_eq!(b.binary_search_by(|_| Ordering::Greater), Err(0));
|
||||||
|
assert_eq!(b.binary_search_by(|_| Ordering::Less), Err(usize::MAX));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1982,6 +1994,7 @@ fn test_copy_within_panics_dest_too_long() {
|
|||||||
// The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
|
// The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
|
||||||
bytes.copy_within(0..4, 10);
|
bytes.copy_within(0..4, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "slice index starts at 2 but ends at 1")]
|
#[should_panic(expected = "slice index starts at 2 but ends at 1")]
|
||||||
fn test_copy_within_panics_src_inverted() {
|
fn test_copy_within_panics_src_inverted() {
|
||||||
|
Loading…
Reference in New Issue
Block a user