feat(core): add `StrBuffer::prefix()` method
What changed, and why it matters
This commit adds a new helper method called prefix() to a Rust string buffer used inside the Trezor firmware. It lets code safely take the first N bytes of a string, but only if the cut point lands on a valid UTF-8 character boundary. There is no bug fix, no security patch, and no indication this change addresses a vulnerability.
No security action required. Review is routine; the method appears correctly bounded and UTF-8 aware.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces StrBuffer::prefix(usize) in core/embed/rust/src/micropython/buffer.rs. It truncates a StrBuffer to the first ‘bytes’ bytes, asserting that the requested length fits within the buffer and that the split point is a UTF-8 character boundary. The implementation uses unwrap! for the usize-to-u16 conversion and assert! for bounds and UTF-8 safety. It is a pure feature addition with no callers shown and no changes to existing behavior.
Changed components
core/embed/rust/src/micropython/buffer.rsInspect captured patch +11 / −0
diff --git a/core/embed/rust/src/micropython/buffer.rs b/core/embed/rust/src/micropython/buffer.rs
index 131aa7b48..5b9a6b8aa 100644
--- a/core/embed/rust/src/micropython/buffer.rs
+++ b/core/embed/rust/src/micropython/buffer.rs
@@ -105,6 +105,17 @@ impl StrBuffer {
off: self.off + off,
}
}
+
+ pub fn prefix(&self, bytes: usize) -> Self {
+ let new_len: u16 = unwrap!(bytes.try_into());
+ assert!(new_len <= self.len);
+ assert!(self.as_ref().is_char_boundary(bytes));
+ Self {
+ ptr: self.ptr,
+ len: new_len,
+ off: self.off,
+ }
+ }
}
impl Default for StrBuffer {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.