auto merge of #7578 : alexcrichton/rust/overflow, r=thestinger
This should never cause a segfault, but rather fail somehow. Possibly a condition could be used here, but for now there's not much else to do.
This commit is contained in:
commit
ac026e2e69
@ -1172,7 +1172,11 @@ impl<T> OwnedVector<T> for ~[T] {
|
||||
vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
|
||||
} else {
|
||||
let alloc = n * sys::nonzero_size_of::<T>();
|
||||
*ptr = realloc_raw(*ptr as *mut c_void, alloc + size_of::<raw::VecRepr>())
|
||||
let size = alloc + size_of::<raw::VecRepr>();
|
||||
if alloc / sys::nonzero_size_of::<T>() != n || size < alloc {
|
||||
fail!("vector size is too large: %u", n);
|
||||
}
|
||||
*ptr = realloc_raw(*ptr as *mut c_void, size)
|
||||
as *mut raw::VecRepr;
|
||||
(**ptr).unboxed.alloc = alloc;
|
||||
}
|
||||
@ -3327,4 +3331,13 @@ mod tests {
|
||||
values.mut_slice(2,4).set_memory(0xFF);
|
||||
assert_eq!(values, [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_overflow_does_not_cause_segfault() {
|
||||
let mut v = ~[];
|
||||
v.reserve(-1);
|
||||
v.push(1);
|
||||
v.push(2);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user