Skip to content

Commit 1002e40

Browse files
committed
Auto merge of #54399 - alexcrichton:fix-bug, r=steveklabnik
std: Check for overflow in `str::repeat` This commit fixes a buffer overflow issue in the standard library discovered by Scott McMurray where if a large number was passed to `str::repeat` it may cause and out of bounds write to the buffer of a `Vec`. This bug was accidentally introduced in #48657 when optimizing the `str::repeat` function. The bug affects stable Rust releases 1.26.0 to 1.29.0. We plan on backporting this fix to create a 1.29.1 release, and the 1.30.0 release onwards will include this fix. The fix in this commit is to introduce a deterministic panic in the case of capacity overflow. When repeating a slice where the resulting length is larger than the address space, there’s no way it can succeed anyway! The standard library and surrounding libraries were briefly checked to see if there were othere instances of preallocating a vector with a calculation that may overflow. No instances of this bug (out of bounds write due to a calculation overflow) were found at this time. Note that this commit is the first steps towards fixing this issue, we'll be making a formal post to the Rust security list once these commits have been merged.
2 parents 2fa1390 + 8ac88d3 commit 1002e40

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/liballoc/slice.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ impl<T> [T] {
392392

393393
/// Creates a vector by repeating a slice `n` times.
394394
///
395+
/// # Panics
396+
///
397+
/// This function will panic if the capacity would overflow.
398+
///
395399
/// # Examples
396400
///
397401
/// Basic usage:
@@ -403,6 +407,16 @@ impl<T> [T] {
403407
/// assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]);
404408
/// }
405409
/// ```
410+
///
411+
/// A panic upon overflow:
412+
///
413+
/// ```should_panic
414+
/// #![feature(repeat_generic_slice)]
415+
/// fn main() {
416+
/// // this will panic at runtime
417+
/// b"0123456789abcdef".repeat(usize::max_value());
418+
/// }
419+
/// ```
406420
#[unstable(feature = "repeat_generic_slice",
407421
reason = "it's on str, why not on slice?",
408422
issue = "48784")]
@@ -417,7 +431,7 @@ impl<T> [T] {
417431
// and `rem` is the remaining part of `n`.
418432

419433
// Using `Vec` to access `set_len()`.
420-
let mut buf = Vec::with_capacity(self.len() * n);
434+
let mut buf = Vec::with_capacity(self.len().checked_mul(n).expect("capacity overflow"));
421435

422436
// `2^expn` repetition is done by doubling `buf` `expn`-times.
423437
buf.extend(self);

src/liballoc/str.rs

+13
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ impl str {
515515

516516
/// Creates a new [`String`] by repeating a string `n` times.
517517
///
518+
/// # Panics
519+
///
520+
/// This function will panic if the capacity would overflow.
521+
///
518522
/// [`String`]: string/struct.String.html
519523
///
520524
/// # Examples
@@ -524,6 +528,15 @@ impl str {
524528
/// ```
525529
/// assert_eq!("abc".repeat(4), String::from("abcabcabcabc"));
526530
/// ```
531+
///
532+
/// A panic upon overflow:
533+
///
534+
/// ```should_panic
535+
/// fn main() {
536+
/// // this will panic at runtime
537+
/// "0123456789abcdef".repeat(usize::max_value());
538+
/// }
539+
/// ```
527540
#[stable(feature = "repeat_str", since = "1.16.0")]
528541
pub fn repeat(&self, n: usize) -> String {
529542
unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }

0 commit comments

Comments
 (0)