1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 10:14:06 +02:00

Merge pull request 'Fix saturating overflow in ExtraData parsing' (#1) from glyphpoch/parser:fix/extra-data-read-overflow into master

Reviewed-on: https://codeberg.org/demostf/parser/pulls/1
This commit is contained in:
Robin Appelman 2025-06-03 20:39:16 +02:00
commit d4bb45bd4f
4 changed files with 26 additions and 2 deletions

View file

@ -154,7 +154,7 @@ fn test_string_table_roundtrip() {
#[serde(bound(deserialize = "'a: 'static"))]
pub struct ExtraData<'a> {
pub byte_len: u16,
#[size = "byte_len.saturating_mul(8)"]
#[size = "(byte_len as usize).saturating_mul(8)"]
pub data: Stream<'a>,
}

Binary file not shown.

View file

@ -0,0 +1,4 @@
{
"name": "ServerMapCycle",
"marker": 4039822362
}

View file

@ -4,7 +4,7 @@ use test_case::test_case;
use tf_demo_parser::demo::message::stringtable::{
parse_string_table_update, write_string_table_update, StringTableMeta,
};
use tf_demo_parser::demo::packet::stringtable::FixedUserDataSize;
use tf_demo_parser::demo::packet::stringtable::{FixedUserDataSize, StringTable};
#[test_case("test_data/string_tables/decalprecache.bin", "test_data/string_tables/decalprecache_meta.json"; "decalprecache")]
#[test_case("test_data/string_tables/downloadables.bin", "test_data/string_tables/downloadables_meta.json"; "downloadables")]
@ -67,3 +67,23 @@ fn string_table_reencode(input_file: &str, meta_file: &str) {
pretty_assertions::assert_eq!(data, out);
}
}
#[test_case("test_data/string_tables/ServerMapCycleLarge.bin", "test_data/string_tables/ServerMapCycleLarge_meta.json"; "ServerMapCycleLarge")]
fn large_string_table_parsing(input_file: &str, meta_file: &str) {
let data = fs::read(input_file).unwrap();
let meta: serde_json::Value =
serde_json::from_str(&fs::read_to_string(meta_file).unwrap()).unwrap();
let expected_table_name = meta["name"].as_str().unwrap();
let expected_marker = meta["marker"].as_u64().unwrap() as u32;
let mut stream = BitReadStream::new(BitReadBuffer::new(&data, LittleEndian));
// Parse the table data
let table: StringTable<'_> = stream.read().unwrap();
pretty_assertions::assert_eq!(expected_table_name, table.name);
// Ensure the stream position is correct (previously the byte_len.saturated_mul(8) in ExtraData could overflow and would leave the BitReadStream in a bad state).
let marker: u32 = stream.read().unwrap();
pretty_assertions::assert_eq!(expected_marker, marker);
}