1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-20 11:47:46 +00:00

Add invalid data test for ChatLog, add a simple check for invalid data

This commit is contained in:
Joshua Goins 2024-04-16 22:04:02 -04:00
parent 170a1d27a7
commit 087deb2bc8

View file

@ -14,7 +14,7 @@ struct ChatLogHeader {
content_size: u32, content_size: u32,
file_size: u32, file_size: u32,
#[br(count = file_size - content_size)] #[br(count = file_size.saturating_sub(content_size))]
offset_entries: Vec<u32>, offset_entries: Vec<u32>,
} }
@ -117,7 +117,11 @@ impl ChatLog {
let mut cursor = Cursor::new(buffer); let mut cursor = Cursor::new(buffer);
let header = ChatLogHeader::read(&mut cursor).expect("Cannot parse header."); let header = ChatLogHeader::read(&mut cursor).expect("Cannot parse header.");
// Dumb check for obviously wrong values
if header.content_size as usize > buffer.len() || header.file_size as usize > buffer.len() {
return None;
}
let content_offset = (8 + header.file_size * 4) as u64; let content_offset = (8 + header.file_size * 4) as u64;
// beginning of content offset // beginning of content offset
@ -150,3 +154,21 @@ impl ChatLog {
Some(ChatLog { entries }) Some(ChatLog { entries })
} }
} }
#[cfg(test)]
mod tests {
use std::fs::read;
use std::path::PathBuf;
use super::*;
#[test]
fn test_invalid() {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("random");
// Feeding it invalid data should not panic
ChatLog::from_existing(&read(d).unwrap());
}
}