Fix: check decode buffer end after the run opcode

This commit is contained in:
Ivan Smirnov 2021-12-01 17:13:50 +00:00
parent 4067d83829
commit 0d612fd3c2
2 changed files with 6 additions and 5 deletions

View file

@ -58,15 +58,12 @@ where
let mut run = 0_u16; let mut run = 0_u16;
for px_out in pixels.iter_mut() { for px_out in pixels.iter_mut() {
if unlikely(!buf.within_bounds()) {
break;
}
// TODO: check for safety that ReadBuf is not over yet
if run != 0 { if run != 0 {
run -= 1; run -= 1;
*px_out = px; *px_out = px;
continue; continue;
} else if unlikely(!buf.within_bounds()) {
return Err(Error::UnexpectedBufferEnd);
} }
let b1 = buf.read(); let b1 = buf.read();

View file

@ -12,6 +12,7 @@ pub enum Error {
InputBufferTooSmall { size: usize, required: usize }, InputBufferTooSmall { size: usize, required: usize },
OutputBufferTooSmall { size: usize, required: usize }, OutputBufferTooSmall { size: usize, required: usize },
InvalidMagic { magic: u32 }, InvalidMagic { magic: u32 },
UnexpectedBufferEnd,
// TODO: invalid colorspace // TODO: invalid colorspace
} }
@ -38,6 +39,9 @@ impl Display for Error {
Self::InvalidMagic { magic } => { Self::InvalidMagic { magic } => {
write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic) write!(f, "invalid magic: expected {:?}, got {:?}", QOI_MAGIC, magic)
} }
Self::UnexpectedBufferEnd => {
write!(f, "unexpected input buffer end while decoding")
}
} }
} }
} }