mirror of
https://codeberg.org/icewind/vbsp.git
synced 2026-06-03 18:54:05 +02:00
fix oom errors with malformed inputs
This commit is contained in:
parent
697052897e
commit
148c6c59bd
3 changed files with 7 additions and 5 deletions
|
|
@ -15,6 +15,7 @@ use binrw::{BinRead, BinResult, ReadOptions};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bv::BitVec;
|
use bv::BitVec;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::cmp::min;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::io::{Cursor, Read};
|
use std::io::{Cursor, Read};
|
||||||
|
|
@ -388,7 +389,7 @@ impl VisData {
|
||||||
pub fn visible_clusters(&self, cluster: i16) -> BitVec<u8> {
|
pub fn visible_clusters(&self, cluster: i16) -> BitVec<u8> {
|
||||||
let offset = self.pvs_offsets[cluster as usize] as usize;
|
let offset = self.pvs_offsets[cluster as usize] as usize;
|
||||||
let pvs_buffer = &self.data[offset..];
|
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);
|
visible_clusters.resize(self.cluster_count as u64, false);
|
||||||
|
|
||||||
let mut cluster_index = 0;
|
let mut cluster_index = 0;
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ pub use error::{BspError, StringError};
|
||||||
use lzma_rs::decompress::{Options, UnpackedSize};
|
use lzma_rs::decompress::{Options, UnpackedSize};
|
||||||
use reader::LumpReader;
|
use reader::LumpReader;
|
||||||
use std::{io::Read, ops::Deref};
|
use std::{io::Read, ops::Deref};
|
||||||
|
use std::cmp::min;
|
||||||
|
|
||||||
pub type BspResult<T> = Result<T, BspError>;
|
pub type BspResult<T> = Result<T, BspError>;
|
||||||
|
|
||||||
|
|
@ -542,7 +543,7 @@ mod tests {
|
||||||
/// LZMA decompression with the header used by source
|
/// LZMA decompression with the header used by source
|
||||||
fn lzma_decompress_with_header(data: &[u8], expected_length: usize) -> Result<Vec<u8>, BspError> {
|
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
|
// 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);
|
let mut cursor = Cursor::new(data);
|
||||||
if b"LZMA" != &<[u8; 4]>::read(&mut cursor)? {
|
if b"LZMA" != &<[u8; 4]>::read(&mut cursor)? {
|
||||||
return Err(BspError::LumpDecompressError(
|
return Err(BspError::LumpDecompressError(
|
||||||
|
|
|
||||||
|
|
@ -74,13 +74,13 @@ impl<R: BinReaderExt + Read> LumpReader<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_visdata(&mut self) -> BspResult<VisData> {
|
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());
|
return Ok(VisData::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let cluster_count = self.inner.read_le()?;
|
let cluster_count = self.inner.read_le()?;
|
||||||
let mut pvs_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(cluster_count as usize);
|
let mut pas_offsets = Vec::with_capacity(min(cluster_count as usize, 1024));
|
||||||
|
|
||||||
for _ in 0..cluster_count {
|
for _ in 0..cluster_count {
|
||||||
pvs_offsets.push(self.inner.read_le()?);
|
pvs_offsets.push(self.inner.read_le()?);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue