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:
@@ -1172,7 +1172,11 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
|
vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
|
||||||
} else {
|
} else {
|
||||||
let alloc = n * sys::nonzero_size_of::<T>();
|
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;
|
as *mut raw::VecRepr;
|
||||||
(**ptr).unboxed.alloc = alloc;
|
(**ptr).unboxed.alloc = alloc;
|
||||||
}
|
}
|
||||||
@@ -3327,4 +3331,13 @@ mod tests {
|
|||||||
values.mut_slice(2,4).set_memory(0xFF);
|
values.mut_slice(2,4).set_memory(0xFF);
|
||||||
assert_eq!(values, [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user