Fix range of varint encoder; add more boundary tests.
This commit is contained in:
parent
b1b157c6be
commit
8d73a2e577
1 changed files with 28 additions and 1 deletions
29
src/ebml.rs
29
src/ebml.rs
|
@ -117,7 +117,7 @@ const EIGHT_MAX: u64 = EIGHT_FLAG - 2;
|
||||||
pub fn encode_varint(varint: Varint, buffer: &mut [u8]) -> Result<usize, WriteError> {
|
pub fn encode_varint(varint: Varint, buffer: &mut [u8]) -> Result<usize, WriteError> {
|
||||||
let (size, number) = match varint {
|
let (size, number) = match varint {
|
||||||
Varint::Unknown => (1, 0xFF),
|
Varint::Unknown => (1, 0xFF),
|
||||||
Varint::Value(too_big) if too_big >= EIGHT_MAX => {
|
Varint::Value(too_big) if too_big > EIGHT_MAX => {
|
||||||
return Err(WriteError::OutOfRange)
|
return Err(WriteError::OutOfRange)
|
||||||
},
|
},
|
||||||
Varint::Value(value) => {
|
Varint::Value(value) => {
|
||||||
|
@ -246,6 +246,33 @@ mod tests {
|
||||||
assert_eq!(encode_varint(Varint::Value(128), &mut buffer), Ok(2));
|
assert_eq!(encode_varint(Varint::Value(128), &mut buffer), Ok(2));
|
||||||
assert_eq!(&buffer[0..2], &[0x40, 128]);
|
assert_eq!(&buffer[0..2], &[0x40, 128]);
|
||||||
assert_eq!(encode_varint(Varint::Value(128), &mut no_space), Err(WriteError::BufferTooSmall));
|
assert_eq!(encode_varint(Varint::Value(128), &mut no_space), Err(WriteError::BufferTooSmall));
|
||||||
|
|
||||||
|
// 6 bytes
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x03FFFFFFFFFE), &mut buffer), Ok(6));
|
||||||
|
assert_eq!(&buffer[0..6], &[0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]);
|
||||||
|
|
||||||
|
// 7 bytes
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x03FFFFFFFFFF), &mut buffer), Ok(7));
|
||||||
|
assert_eq!(&buffer[0..7], &[0x02, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
|
||||||
|
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x01000000000000), &mut buffer), Ok(7));
|
||||||
|
assert_eq!(&buffer[0..7], &[0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
|
||||||
|
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer), Ok(7));
|
||||||
|
assert_eq!(&buffer[0..7], &[0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]);
|
||||||
|
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut no_space), Err(WriteError::BufferTooSmall));
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFE), &mut buffer[0..6]), Err(WriteError::BufferTooSmall));
|
||||||
|
|
||||||
|
// 8 bytes
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0x01FFFFFFFFFFFF), &mut buffer), Ok(8));
|
||||||
|
assert_eq!(&buffer[0..8], &[0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
|
||||||
|
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0xFFFFFFFFFFFFFE), &mut buffer), Ok(8));
|
||||||
|
assert_eq!(&buffer[0..8], &[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]);
|
||||||
|
|
||||||
|
assert_eq!(encode_varint(Varint::Value(0xFFFFFFFFFFFFFF), &mut buffer), Err(WriteError::OutOfRange));
|
||||||
|
assert_eq!(encode_varint(Varint::Value(u64::max_value()), &mut buffer), Err(WriteError::OutOfRange));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue