refactor(core/rust): use str::floor_char_boundary
What changed, and why it matters
This commit is a minor code cleanup in the Trezor firmware's Bluetooth module. It replaces a hand-written loop that finds a valid UTF-8 character boundary with Rust's built-in `floor_char_boundary` method. The behavior is functionally identical, and there is no security-relevant change.
No security action needed. Treat as a normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors prefix_utf8_bytes in core/embed/rust/src/trezorhal/ble/mod.rs to use the now-stable standard library method str::floor_char_boundary instead of a manual while-loop that decrements a byte index until it lands on a UTF-8 character boundary. The function still returns the longest prefix of text whose byte length does not exceed max_len and ends at a valid character boundary. No callers, signatures, or observable behavior change.
Changed components
core/embed/rust/src/trezorhal/ble/mod.rsInspect captured patch +2 / −6
diff --git a/core/embed/rust/src/trezorhal/ble/mod.rs b/core/embed/rust/src/trezorhal/ble/mod.rs
index aa6a250a..1086026d 100644
--- a/core/embed/rust/src/trezorhal/ble/mod.rs
+++ b/core/embed/rust/src/trezorhal/ble/mod.rs
@@ -17,13 +17,9 @@ pub const TX_PACKET_SIZE: usize = ffi::BLE_TX_PACKET_SIZE as usize;
const COMMAND_FAILED: Error = Error::RuntimeError(c"BLE command failed");
const WRITE_FAILED: Error = Error::RuntimeError(c"BLE write failed");
-// NOTE: replace with floor_char_boundary when stable
fn prefix_utf8_bytes(text: &str, max_len: usize) -> &str {
- let mut i = text.len().min(max_len);
- while !text.is_char_boundary(i) {
- i -= 1;
- }
- &text[..i]
+ let boundary = text.floor_char_boundary(max_len);
+ &text[..boundary]
}
pub fn res_to_result(res: bool) -> Result<(), Error> {
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.