Skip to content

Commit 28f4ae6

Browse files
authored
Merge pull request #508 from fpdotmonkey/vec-layout-array-chk
Use the newest Layout::array size checks for vec-alloc
2 parents 91044a6 + 9bc0f07 commit 28f4ae6

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/vec/vec-alloc.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,17 @@ use std::alloc::{self, Layout};
170170
impl<T> Vec<T> {
171171
fn grow(&mut self) {
172172
let (new_cap, new_layout) = if self.cap == 0 {
173-
(1, Layout::array::<T>(1).unwrap())
173+
(1, Layout::array::<T>(1))
174174
} else {
175175
// This can't overflow since self.cap <= isize::MAX.
176176
let new_cap = 2 * self.cap;
177-
178-
// `Layout::array` checks that the number of bytes is <= usize::MAX,
179-
// but this is redundant since old_layout.size() <= isize::MAX,
180-
// so the `unwrap` should never fail.
181-
let new_layout = Layout::array::<T>(new_cap).unwrap();
182-
(new_cap, new_layout)
177+
(new_cap, Layout::array::<T>(new_cap))
183178
};
184179
185-
// Ensure that the new allocation doesn't exceed `isize::MAX` bytes.
186-
assert!(new_layout.size() <= isize::MAX as usize, "Allocation too large");
180+
// `Layout::array` checks that the number of bytes allocated is
181+
// in 1..=isize::MAX and will error otherwise. An allocation of
182+
// 0 bytes isn't possible thanks to the above condition.
183+
let new_layout = new_layout.expect("Allocation too large");
187184
188185
let new_ptr = if self.cap == 0 {
189186
unsafe { alloc::alloc(new_layout) }

src/vec/vec-final.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,17 @@ impl<T> RawVec<T> {
3333
assert!(mem::size_of::<T>() != 0, "capacity overflow");
3434

3535
let (new_cap, new_layout) = if self.cap == 0 {
36-
(1, Layout::array::<T>(1).unwrap())
36+
(1, Layout::array::<T>(1))
3737
} else {
38-
// This can't overflow because we ensure self.cap <= isize::MAX.
38+
// This can't overflow since self.cap <= isize::MAX.
3939
let new_cap = 2 * self.cap;
40-
41-
// `Layout::array` checks that the number of bytes is <= usize::MAX,
42-
// but this is redundant since old_layout.size() <= isize::MAX,
43-
// so the `unwrap` should never fail.
44-
let new_layout = Layout::array::<T>(new_cap).unwrap();
45-
(new_cap, new_layout)
40+
(new_cap, Layout::array::<T>(new_cap))
4641
};
4742

48-
// Ensure that the new allocation doesn't exceed `isize::MAX` bytes.
49-
assert!(
50-
new_layout.size() <= isize::MAX as usize,
51-
"Allocation too large"
52-
);
43+
// `Layout::array` checks that the number of bytes allocated is
44+
// in 1..=isize::MAX and will error otherwise. An allocation of
45+
// 0 bytes isn't possible thanks to the above condition.
46+
let new_layout = new_layout.expect("Allocation too large");
5347

5448
let new_ptr = if self.cap == 0 {
5549
unsafe { alloc::alloc(new_layout) }

0 commit comments

Comments
 (0)