More tests on out-of-range accesses around unsafe code.

This commit is contained in:
KAMADA Ken'ichi 2017-03-12 19:16:00 +09:00
parent 4f2e54a6b9
commit b66b47d1fd
2 changed files with 18 additions and 0 deletions

View File

@ -130,4 +130,13 @@ mod tests {
fn out_of_range() {
BigEndian::loadu16(&[0x01, 0x02], 1);
}
// "attempt to add with overflow" with the arithmetic overflow
// check, and "slice index starts at 18446744073709551615 but ends
// at 1" without it.
#[test]
#[should_panic(expected = "at")]
fn wrap_around() {
BigEndian::loadu16(&[0x01, 0x02], (-1isize) as usize);
}
}

View File

@ -229,6 +229,7 @@ fn parse_unknown<'a>(data: &'a [u8], offset: usize, count: usize)
mod tests {
use endian::BigEndian;
use super::*;
use super::parse_short;
#[test]
fn byte() {
@ -448,4 +449,12 @@ mod tests {
}
}
}
// These functions are never called in a way that an out-of-range access
// could happen, so this test is hypothetical but just for safety.
#[test]
#[should_panic(expected = "index 5 out of range for slice of length 4")]
fn short_oor() {
parse_short::<BigEndian>(b"\x01\x02\x03\x04", 1, 2);
}
}