Skip to content

Fail when a vec::reserve is too large #7578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,11 @@ impl<T> OwnedVector<T> for ~[T] {
rustrt::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;
}
Expand Down Expand Up @@ -3229,4 +3233,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);
}
}