Skip to content

Commit ac026e2

Browse files
committed
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.
2 parents a48ca32 + fe4a158 commit ac026e2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/libstd/vec.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,11 @@ impl<T> OwnedVector<T> for ~[T] {
11721172
vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
11731173
} else {
11741174
let alloc = n * sys::nonzero_size_of::<T>();
1175-
*ptr = realloc_raw(*ptr as *mut c_void, alloc + size_of::<raw::VecRepr>())
1175+
let size = alloc + size_of::<raw::VecRepr>();
1176+
if alloc / sys::nonzero_size_of::<T>() != n || size < alloc {
1177+
fail!("vector size is too large: %u", n);
1178+
}
1179+
*ptr = realloc_raw(*ptr as *mut c_void, size)
11761180
as *mut raw::VecRepr;
11771181
(**ptr).unboxed.alloc = alloc;
11781182
}
@@ -3327,4 +3331,13 @@ mod tests {
33273331
values.mut_slice(2,4).set_memory(0xFF);
33283332
assert_eq!(values, [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]);
33293333
}
3334+
3335+
#[test]
3336+
#[should_fail]
3337+
fn test_overflow_does_not_cause_segfault() {
3338+
let mut v = ~[];
3339+
v.reserve(-1);
3340+
v.push(1);
3341+
v.push(2);
3342+
}
33303343
}

0 commit comments

Comments
 (0)