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

fix oom errors with malformed inputs

This commit is contained in:
Robin Appelman 2022-12-22 22:53:46 +01:00
commit 148c6c59bd
3 changed files with 7 additions and 5 deletions

View file

@ -15,6 +15,7 @@ use binrw::{BinRead, BinResult, ReadOptions};
use bitflags::bitflags;
use bv::BitVec;
use std::borrow::Cow;
use std::cmp::min;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::io::{Cursor, Read};
@ -388,7 +389,7 @@ impl VisData {
pub fn visible_clusters(&self, cluster: i16) -> BitVec<u8> {
let offset = self.pvs_offsets[cluster as usize] as usize;
let pvs_buffer = &self.data[offset..];
let mut visible_clusters = BitVec::with_capacity(self.cluster_count as u64);
let mut visible_clusters = BitVec::with_capacity(min(self.cluster_count as u64, 1024));
visible_clusters.resize(self.cluster_count as u64, false);
let mut cluster_index = 0;

View file

@ -17,6 +17,7 @@ pub use error::{BspError, StringError};
use lzma_rs::decompress::{Options, UnpackedSize};
use reader::LumpReader;
use std::{io::Read, ops::Deref};
use std::cmp::min;
pub type BspResult<T> = Result<T, BspError>;
@ -542,7 +543,7 @@ mod tests {
/// LZMA decompression with the header used by source
fn lzma_decompress_with_header(data: &[u8], expected_length: usize) -> Result<Vec<u8>, BspError> {
// extra 8 byte because game lumps need some padding for reasons
let mut output: Vec<u8> = Vec::with_capacity(expected_length + 8);
let mut output: Vec<u8> = Vec::with_capacity(min(expected_length + 8, 8 * 1024 * 1024));
let mut cursor = Cursor::new(data);
if b"LZMA" != &<[u8; 4]>::read(&mut cursor)? {
return Err(BspError::LumpDecompressError(

View file

@ -74,13 +74,13 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
}
pub fn read_visdata(&mut self) -> BspResult<VisData> {
if (self.length as usize) < std::mem::size_of::<u32>() * 2 {
if (self.length as usize) < size_of::<u32>() * 2 {
return Ok(VisData::default());
}
let cluster_count = self.inner.read_le()?;
let mut pvs_offsets = Vec::with_capacity(cluster_count as usize);
let mut pas_offsets = Vec::with_capacity(cluster_count as usize);
let mut pvs_offsets = Vec::with_capacity(min(cluster_count as usize, 1024));
let mut pas_offsets = Vec::with_capacity(min(cluster_count as usize, 1024));
for _ in 0..cluster_count {
pvs_offsets.push(self.inner.read_le()?);