8000 FdtNodeMut::add_property panics when the strings block contains an invalid string that no property uses · Issue #109 · google/dtoolkit · GitHub
[go: up one dir, main page]

Skip to content

FdtNodeMut::add_property panics when the strings block contains an invalid string that no property uses #109

Description

@calef

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.
fn blob(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();
    let mut 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 as u32).to_be_bytes());
    }
    b.extend_from_slice(&[0; 16]);
    b.extend_from_slice(&structure);
    b.extend_from_slice(strings);
    b
}

#[test]
fn add_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.
    let mut dtb = blob(&[1, 0, 2, 9], &[0xff]);
    assert!(Fdt::new(&dtb).is_ok());

    dtb.resize(dtb.len() + 64, 0); // spare capacity for the edit
    let mut 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.

Activity

  1. added
    bugSomething isn't working
    on Nov 11, 2026
  2. added 2 commits that reference this issue on Nov 12, 2026
    6ba800b
    0bc279e
  3. Daiyuanranfeng commented on Nov 15, 2026

    @Daiyuanranfeng

    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.

  4. added 2 commits that reference this issue on Nov 16, 2026
    1882bf9
    12979d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0