Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feat: transform undisplayable characters for FmtBuffer<N>
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Mar 20, 2024
1 parent 8559540 commit 5f13556
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion fmt_buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ impl<const N: usize> Buffer<N> {
}
}

pub fn as_str(&self) -> &str {
pub fn as_str(&mut self) -> &str {
// NOTE: this workaround is needed until https://github.com/LedgerHQ/ledger-device-rust-sdk/issues/146
// is handled at sdk level
for byte in self.buffer[..self.used].iter_mut() {
if *byte < 0x20 {
*byte = 0x7f;
}
}
debug_assert!(self.used <= self.buffer.len());
// .unwrap() is ok, as only bytes, comprising a sequence of valid utf8 chars
// are going to be written to `self.buffer` on `self.write_str` calls
Expand Down Expand Up @@ -135,4 +142,15 @@ mod tests {
assert_eq!("toooooo long: 400000 - 0x61", buffer.as_str());
assert_eq!(true, buffer.truncated());
}

#[test]
pub fn test_rewrite_undisplayable_chars() {
let mut buffer = Buffer::<30>::new();

buffer.write_str("Prefix: ");

buffer.write_str("\x0A\x0D");
assert_eq!("Prefix: \x7F\x7F", buffer.as_str());
assert_eq!(false, buffer.truncated());
}
}
2 changes: 1 addition & 1 deletion src/app_ui/sign/common/action/function_call_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn format<'b, 'a: 'b>(
}));

let methods_names_fields = ElipsisFields::from_fmt_buffer(
&function_call_perm.method_names,
&mut function_call_perm.method_names,
"Method Names",
&mut field_context.method_names_display_buf,
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/types/elipsis_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> ElipsisFields<'a> {
}

pub fn from_fmt_buffer<const N: usize>(
source: &'a Buffer<N>,
source: &'a mut Buffer<N>,
title: &'a str,
display_buf: &'a mut [u8; 20],
) -> Self {
Expand Down

0 comments on commit 5f13556

Please sign in to comment.