Skip to content

Commit 4096c9f

Browse files
asdfgraydon
asdf
authored andcommitted
adding bound checks for raw::memcpy and memmove
1 parent 2d9b1fe commit 4096c9f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libcore/vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,9 @@ pub mod raw {
19601960
* may overlap.
19611961
*/
19621962
pub unsafe fn memcpy<T>(dst: &[mut T], src: &[const T], count: uint) {
1963+
assert dst.len() >= count;
1964+
assert src.len() >= count;
1965+
19631966
do as_mut_buf(dst) |p_dst, _len_dst| {
19641967
do as_const_buf(src) |p_src, _len_src| {
19651968
ptr::memcpy(p_dst, p_src, count)
@@ -1974,6 +1977,9 @@ pub mod raw {
19741977
* may overlap.
19751978
*/
19761979
pub unsafe fn memmove<T>(dst: &[mut T], src: &[const T], count: uint) {
1980+
assert dst.len() >= count;
1981+
assert src.len() >= count;
1982+
19771983
do as_mut_buf(dst) |p_dst, _len_dst| {
19781984
do as_const_buf(src) |p_src, _len_src| {
19791985
ptr::memmove(p_dst, p_src, count)
@@ -3730,6 +3736,15 @@ mod tests {
37303736
fail
37313737
}
37323738
}
3739+
3740+
#[test]
3741+
#[should_fail]
3742+
fn test_memcpy_oob() unsafe {
3743+
let a = [mut 1, 2, 3, 4];
3744+
let b = [1, 2, 3, 4, 5];
3745+
raw::memcpy(a, b, 5);
3746+
}
3747+
37333748
}
37343749

37353750
// Local Variables:

0 commit comments

Comments
 (0)