Skip to content

Commit 9a5d108

Browse files
committed
rust: use str_from_null_terminated_ptr
The two instances in the commit implemented that ad-hoc.
1 parent 2d9d8aa commit 9a5d108

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

src/rust/bitbox02/src/app_eth.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,42 @@ pub fn erc20_params_get(chain_id: u64, contract_address: [u8; 20]) -> Option<ERC
2323
bitbox02_sys::app_eth_erc20_params_get(chain_id, contract_address.as_ptr()).as_ref()?
2424
};
2525
Some(ERC20Params {
26-
unit: {
27-
let s = unsafe {
28-
let len = crate::util::strlen_ptr(params.unit);
29-
core::slice::from_raw_parts(params.unit, len as _)
30-
};
31-
core::str::from_utf8(s).unwrap()
32-
},
26+
unit: unsafe { crate::util::str_from_null_terminated_ptr(params.unit).unwrap() },
3327
contract_address: params.contract_address,
3428
decimals: params.decimals,
3529
})
3630
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn test_erc20_params_get() {
38+
// Not found for chainID 0.
39+
assert!(erc20_params_get(
40+
0,
41+
*b"\x0f\x72\x71\x4b\x35\xa3\x66\x28\x5d\xf8\x58\x86\xa2\xee\x17\x46\x01\x29\x2a\x17"
42+
)
43+
.is_none());
44+
45+
// Contract address doesn't exist on chainID 1.
46+
assert!(erc20_params_get(
47+
1,
48+
*b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17"
49+
)
50+
.is_none());
51+
52+
let params = erc20_params_get(
53+
1,
54+
*b"\x00\x00\x00\x00\x00\x08\x5d\x47\x80\xb7\x31\x19\xb6\x44\xae\x5e\xcd\x22\xb3\x76",
55+
)
56+
.unwrap();
57+
assert_eq!(params.unit, "TUSD");
58+
assert_eq!(
59+
params.contract_address,
60+
*b"\x00\x00\x00\x00\x00\x08\x5d\x47\x80\xb7\x31\x19\xb6\x44\xae\x5e\xcd\x22\xb3\x76"
61+
);
62+
assert_eq!(params.decimals, 18);
63+
}
64+
}

src/rust/bitbox02/src/keystore.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ pub fn get_bip39_word(idx: u16) -> Result<zeroize::Zeroizing<String>, ()> {
138138
match unsafe { bitbox02_sys::keystore_get_bip39_word(idx, &mut word_ptr) } {
139139
false => Err(()),
140140
true => {
141-
let word = unsafe {
142-
let len = crate::util::strlen_ptr(word_ptr);
143-
let slice = core::slice::from_raw_parts(word_ptr, len as _);
144-
zeroize::Zeroizing::new(core::str::from_utf8(slice).unwrap().into())
145-
};
141+
let word = zeroize::Zeroizing::new(unsafe {
142+
crate::util::str_from_null_terminated_ptr(word_ptr)
143+
.unwrap()
144+
.into()
145+
});
146146
unsafe {
147147
bitbox02_sys::wally_free_string(word_ptr as _);
148148
}

src/rust/bitbox02/src/util.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
extern crate alloc;
1616
use alloc::vec::Vec;
1717

18-
/// Must be given a null-terminated string
19-
/// # Safety
20-
/// ptr must be not NULL and the memory must be valid until a null byte.
21-
pub unsafe fn strlen_ptr(ptr: *const u8) -> isize {
22-
let mut end = ptr;
23-
loop {
24-
if *end == 0 {
25-
return end.offset_from(ptr);
26-
}
27-
end = end.offset(1);
28-
}
29-
}
30-
3118
/// Parses a utf-8 string out of a null terminated buffer. Returns `Err(())` if there
3219
/// is no null terminator or if the bytes before the null terminator is invalid UTF8.
3320
pub fn str_from_null_terminated(input: &[u8]) -> Result<&str, ()> {
@@ -123,14 +110,6 @@ mod tests {
123110
assert_eq!(truncate_str("test", 6), "test");
124111
}
125112

126-
#[test]
127-
fn test_strlen_ptr() {
128-
assert_eq!(unsafe { strlen_ptr(b"\0".as_ptr()) }, 0);
129-
assert_eq!(unsafe { strlen_ptr(b"a\0".as_ptr()) }, 1);
130-
assert_eq!(unsafe { strlen_ptr(b"abcdef\0".as_ptr()) }, 6);
131-
assert_eq!(unsafe { strlen_ptr(b"abcdef\0defghji".as_ptr()) }, 6);
132-
}
133-
134113
#[test]
135114
fn test_str_from_null_terminated() {
136115
assert_eq!(str_from_null_terminated(b"\0"), Ok(""));

0 commit comments

Comments
 (0)