Skip to content

Commit

Permalink
Auto merge of #122761 - jwong101:fix/vec-insert, r=workingjubilee,Nil…
Browse files Browse the repository at this point in the history
…strieb

fix OOB pointer formed in Vec::index

Move the length check to before using `index` with `ptr::add` to prevent an out of bounds pointer from being formed.

Fixes #122760
  • Loading branch information
bors committed Mar 20, 2024
2 parents e3df96c + 37718f9 commit 1388d7a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,9 @@ impl<T, A: Allocator> Vec<T, A> {
}

let len = self.len();
if index > len {
assert_failed(index, len);
}

// space for the new element
if len == self.buf.capacity() {
Expand All @@ -1556,10 +1559,6 @@ impl<T, A: Allocator> Vec<T, A> {
// Shift everything over to make space. (Duplicating the
// `index`th element into two consecutive places.)
ptr::copy(p, p.add(1), len - index);
} else if index == len {
// No elements need shifting.
} else {
assert_failed(index, len);
}
// Write it in, overwriting the first copy of the `index`th
// element.
Expand Down

0 comments on commit 1388d7a

Please sign in to comment.