You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FdtNodeMut::add_property panics when the strings block contains an invalid string that no property uses #109
Version: dtoolkit 0.5.0, which is also current main (7662cd1). Rust 1.99.1, debug and release.
What happens.add_property panics at src/fdt_mut.rs:168 when the strings block contains bytes that are not a NUL-terminated UTF-8 string, even though no property refers to them and Fdt::new and FdtMut::from_slice accepted the blob:
thread 'add_property_with_unreferenced_byte_in_strings_block' panicked at .../src/fdt_mut.rs:168:45:
FdtMut should be valid: FdtParseError { offset: 72, kind: InvalidString }
add_property calls add_string, which calls find_existing_string (src/fdt_mut.rs#L161-L176). That function walks the strings block from its start and expects each string it reaches to be valid. Validation in Fdt::new checks only the strings that a property's nameoff points to, so the rest of the block is never checked. The panic happens only when the new name is not found before the invalid bytes: if the name already exists earlier in the block, the lookup returns before reaching it.
Expected.add_property on a tree that Fdt::new accepted either succeeds or returns an error, without panicking.
Reproduction.
use dtoolkit::fdt::Fdt;use dtoolkit::fdt_mut::FdtMut;/// Builds a version-17 blob: the header, one empty reservation entry, then the/// structure block and the strings block.fnblob(structure:&[u32],strings:&[u8]) -> Vec<u8>{let structure:Vec<u8> = structure.iter().flat_map(|w| w.to_be_bytes()).collect();let off_struct = 40 + 16;let off_strings = off_struct + structure.len();let total = off_strings + strings.len();letmut b = Vec::new();for v in[0xd00dfeed, total, off_struct, off_strings,40,17,16,0, strings.len(), structure.len()]{
b.extend_from_slice(&(v asu32).to_be_bytes());}
b.extend_from_slice(&[0;16]);
b.extend_from_slice(&structure);
b.extend_from_slice(strings);
b
}#[test]fnadd_property_with_unreferenced_byte_in_strings_block(){// The root node alone: FDT_BEGIN_NODE with an empty name, FDT_END_NODE, FDT_END.// The strings block holds one byte, 0xff, that no property uses.letmut dtb = blob(&[1,0,2,9],&[0xff]);assert!(Fdt::new(&dtb).is_ok());
dtb.resize(dtb.len() + 64,0);// spare capacity for the editletmut fdt = FdtMut::from_slice(&mut dtb).unwrap();
fdt.root_mut().add_property("x",1u32).unwrap();// panics inside add_property, before the unwrap}
A strings block of [0xff, 0x00] (terminated, but not UTF-8) panics in the same place.
Suggestion. Have find_existing_string compare raw bytes: split the block at each NUL, compare each terminated piece with name.as_bytes(), and ignore any trailing bytes with no NUL, so that a piece which is not a valid string can never match and never panics. A local change along these lines passes the existing test suite (cargo test --all-features) and the test above.
I would like to take this issue. I will make add_property's existing-name lookup compare terminated raw bytes without assuming UTF-8, then add a regression test for an unreferenced invalid strings-block entry.
Activity
fix(fdt_mut): do not panic on unreferenced bytes in the strings block
fix(fdt_mut): do not panic on unreferenced bytes in the strings block
Daiyuanranfeng commentedon Nov 15, 2026
I would like to take this issue. I will make add_property's existing-name lookup compare terminated raw bytes without assuming UTF-8, then add a regression test for an unreferenced invalid strings-block entry.
fix(fdt_mut): do not panic on unreferenced bytes in the strings block
fix(fdt_mut): do not panic on unreferenced bytes in the strings block