Skip to content

Commit

Permalink
std::at_vec: Fix segfault on overflow when resizing ~[@t]
Browse files Browse the repository at this point in the history
Easy to reproduce:

    let mut v = ~[@1];
    v.resize(-1);  // success a.k.a silent failure
    v.push(@2); // segfault
  • Loading branch information
blake2-ppc committed Sep 17, 2013
1 parent 6e538ed commit e211888
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/libstd/at_vec.rs
Expand Up @@ -230,13 +230,16 @@ pub mod raw {
// Implementation detail. Shouldn't be public
#[allow(missing_doc)]
pub fn reserve_raw(ty: *TyDesc, ptr: *mut *mut Box<Vec<()>>, n: uint) {

// check for `uint` overflow
unsafe {
let size_in_bytes = n * (*ty).size;
if size_in_bytes > (**ptr).data.alloc {
let total_size = size_in_bytes + sys::size_of::<Vec<()>>();
if n > (**ptr).data.alloc / (*ty).size {
let alloc = n * (*ty).size;
let total_size = alloc + sys::size_of::<Vec<()>>();
if alloc / (*ty).size != n || total_size < alloc {
fail!("vector size is too large: %u", n);
}
(*ptr) = local_realloc(*ptr as *(), total_size) as *mut Box<Vec<()>>;
(**ptr).data.alloc = size_in_bytes;
(**ptr).data.alloc = alloc;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/libstd/vec.rs
Expand Up @@ -3659,6 +3659,14 @@ mod tests {
v.push(2);
}

#[test]
#[should_fail]
fn test_overflow_does_not_cause_segfault_managed() {
let mut v = ~[@1];
v.reserve(-1);
v.push(@2);
}

#[test]
fn test_mut_split() {
let mut values = [1u8,2,3,4,5];
Expand Down

5 comments on commit e211888

@bors
Copy link
Contributor

@bors bors commented on e211888 Sep 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on e211888 Sep 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging blake2-ppc/rust/hazards-on-overflow = e211888 into auto

@bors
Copy link
Contributor

@bors bors commented on e211888 Sep 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blake2-ppc/rust/hazards-on-overflow = e211888 merged ok, testing candidate = d5e9033

@bors
Copy link
Contributor

@bors bors commented on e211888 Sep 17, 2013

@bors
Copy link
Contributor

@bors bors commented on e211888 Sep 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d5e9033

Please sign in to comment.