Files
craft-anything/crates/formats/tests/string_offsets.rs
BFlatandAfloat 15de36614b Format parsers refuse or flag input they used to half-read (#652)
Replays #606–#615. Binary plists, base64, OIDs, string offsets, tar, cpio and source maps refuse or flag input they used to half-read.

Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
2026-11-03 20:29:18 +07:00

26 lines
930 B
Rust

use craft_anything_formats::strings::{Encoding, extract};
#[test]
fn ascii_offsets_do_not_wrap_past_the_address_space() {
let strings = extract(b"ok\0later", u64::MAX - 2, 2, 10);
assert_eq!(strings.len(), 1);
assert_eq!(strings[0].text, "ok");
assert_eq!(strings[0].offset, u64::MAX - 2);
assert!(extract(b"\0later", u64::MAX, 2, 10).is_empty());
}
#[test]
fn wide_offsets_do_not_wrap_past_the_address_space() {
let strings = extract(b"a\0b\0\0\0c\0d\0", u64::MAX - 4, 2, 10);
assert_eq!(strings.len(), 1);
assert_eq!(strings[0].text, "ab");
assert_eq!(strings[0].encoding, Encoding::Utf16le);
assert_eq!(strings[0].offset, u64::MAX - 4);
}
#[test]
fn ordinary_offsets_still_identify_both_encodings() {
let strings = extract(b"one\0t\0w\0o\0", 100, 2, 10);
assert_eq!(strings.iter().map(|s| (s.offset, s.text.as_str())).collect::<Vec<_>>(), vec![(100, "one"), (104, "two")]);
}