1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

verify lump size before reading

This commit is contained in:
Robin Appelman 2022-02-20 16:39:31 +01:00
commit 65af835320
2 changed files with 11 additions and 0 deletions

View file

@ -9,6 +9,11 @@ pub enum BspError {
UnexpectedHeader(Header),
#[error("bsp lump is out of bounds of the bsp file")]
LumpOutOfBounds(LumpEntry),
#[error("Invalid lump size, lump size {lump_size} is not a multiple of the element size {element_size}")]
InvalidLumpSize {
element_size: usize,
lump_size: usize,
},
#[error("unexpected length of uncompressed lump, got {got} but expected {expected}")]
UnexpectedUncompressedLumpSize { got: u32, expected: u32 },
#[error("error while decompressing lump")]

View file

@ -31,6 +31,12 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
where
F: FnMut(&mut LumpReader<R>) -> BspResult<T>,
{
if self.length % size_of::<T>() != 0 {
return Err(BspError::InvalidLumpSize {
element_size: size_of::<T>(),
lump_size: self.length,
});
}
let num_entries = self.length / size_of::<T>();
let mut entries = Vec::with_capacity(num_entries);