1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00

fixup: decrement before indexing with last_mut

This commit is contained in:
Hussein Ait Lahcen 2022-08-12 20:55:58 +02:00
commit 8ba5608262
No known key found for this signature in database
GPG key ID: E500882E75AC4E42
2 changed files with 17 additions and 1 deletions

View file

@ -45,7 +45,7 @@ impl<'a> WriteData<'a> {
fn last_mut(&mut self) -> Option<&mut u8> { fn last_mut(&mut self) -> Option<&mut u8> {
match self { match self {
WriteData::Vec(vec) => vec.last_mut(), WriteData::Vec(vec) => vec.last_mut(),
WriteData::Slice { data, length } if *length > 0 => Some(&mut data[*length]), WriteData::Slice { data, length } if *length > 0 => Some(&mut data[*length - 1]),
_ => None, _ => None,
} }
} }

View file

@ -227,3 +227,19 @@ fn test_write_to_slice() {
// 0 padded // 0 padded
assert_eq!(false, read.read_bool().unwrap()); assert_eq!(false, read.read_bool().unwrap());
} }
#[test]
fn test_write_last_slice() {
let mut data = [0; 1];
{
let mut stream = BitWriteStream::from_slice(&mut data[..], LittleEndian);
stream.write_int::<u8>(0b1000, 4).unwrap();
stream.write_bool(true).unwrap();
}
let mut read = BitReadStream::from(BitReadBuffer::new(&data[..], LittleEndian));
assert_eq!(0b1000, read.read_int::<u8>(4).unwrap());
assert_eq!(true, read.read_bool().unwrap());
}