Repository navigation
Conversation
`Fdt::new` validates only the strings that a property's `nameoff` points to, so the `dt_strings` block of an accepted tree may contain bytes that are not a null-terminated UTF-8 string. `find_existing_string` walked the whole block and expected every string to be valid, which made `FdtNodeMut::add_property` panic whenever the new name was not found before such bytes. Compare raw bytes instead: split the block at each null terminator, compare every terminated piece with the name, and skip a trailing piece without a terminator. A piece that is not a valid string can never equal a `&str`, so it is passed over and the name is appended as before. Fixes google#109 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #111 +/- ##
=======================================
Coverage 93.61% 93.61%
=======================================
Files 26 26
Lines 3428 3428
=======================================
Hits 3209 3209
Misses 219 219 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| /// Builds a version 17 DTB consisting of the header, one empty memory | ||
| /// reservation entry, `structure` and `strings`. The buffer is padded with | ||
| /// `spare` zero bytes so the tree can grow in place. | ||
| fn build_dtb(structure: &[u32], strings: &[u8], spare: usize) -> Vec<u8> { |
There was a problem hiding this comment.
Can we use the helper that was added in #108 here instead?
m4tx
left a comment
There was a problem hiding this comment.
Looks good to me except some minor issues, thanks a lot!
A nice side effect of this should be slightly improved performance as we won't decode UTF-8 in find_existing_string anymore.
| for s in str_block.split_inclusive(|&b| b == 0) { | ||
| if s.strip_suffix(&[0]) == Some(name.as_bytes()) { |
There was a problem hiding this comment.
nit: because we operate on strings here, it's a bit more natural in my opinion to use char/string literals, i.e.:
| for s in str_block.split_inclusive(|&b| b == 0) { | |
| if s.strip_suffix(&[0]) == Some(name.as_bytes()) { | |
| for s in str_block.split_inclusive(|c| c == b'\0') { | |
| if s.strip_suffix(b"\0") == Some(name.as_bytes()) { |
| // Validation only covers the strings that properties reference, so the | ||
| // block may contain bytes that are not a valid string. Comparing raw | ||
| // bytes means such bytes never match `name` and never panic. A trailing | ||
| // piece without a null terminator is skipped. |
There was a problem hiding this comment.
This comment refers to the reason for the change and not so much to the code below. Suggest moving this to the commit message and/or rewriting to something like "comparing raw bytes to avoid decoding UTF-8 (which may not be validated)"
find_existing_stringnow compares raw bytes, as libfdt'sfdt_find_string_does: it splits the strings block at each null terminator, compares each terminated piece with the name, and skips a trailing piece with no terminator. Bytes that are not a valid string can then never match and never panic.The new test covers an unterminated byte, a terminated byte that is not UTF-8, and a name that already exists before the invalid bytes, which is reused.
Fixes #109