Skip to content

Commit 7cff075

Browse files
committed
rust: remove custom CStr type in favor of core::ffi::CStr
core::ffi::CStr was added to core in Rust 1.64.0, so we can remove our custom CStr type to convert C strings into Rust strings. There was only one use left in the U2F registration/authentication confirmation screen.
1 parent 5bf5400 commit 7cff075

File tree

3 files changed

+5
-59
lines changed

3 files changed

+5
-59
lines changed

src/rust/bitbox02-rust-c/src/util.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -100,40 +100,6 @@ impl AsMut<[u8]> for BytesMut {
100100
}
101101
}
102102

103-
/// CStr is a null-terminated string. Null pointers are interpreted as empty strings.
104-
#[repr(C)]
105-
pub struct CStr {
106-
buf: *const c_char,
107-
len: usize,
108-
}
109-
110-
impl CStr {
111-
/// Create a CStr from a null-terminated string or null pointer. Unsafe because it will read
112-
/// until it finds a null character.
113-
pub unsafe fn new(buf: *const c_char) -> Self {
114-
if buf.is_null() {
115-
CStr {
116-
buf: core::ptr::NonNull::dangling().as_ptr(),
117-
len: 0,
118-
}
119-
} else {
120-
let mut len = 0;
121-
let mut b = buf;
122-
while b.read() != 0 {
123-
len += 1;
124-
b = b.offset(1);
125-
}
126-
CStr { buf, len }
127-
}
128-
}
129-
}
130-
131-
impl AsRef<str> for CStr {
132-
fn as_ref(&self) -> &str {
133-
unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.buf, self.len)) }
134-
}
135-
}
136-
137103
/// CStrMut is a "growable" container which keeps track of some array allocated by C with a length
138104
/// and a capacity state. It always contains a null-terminated string. The string (excluding null
139105
/// terminator) can therefore be maximally `capacity-1` long.
@@ -245,14 +211,6 @@ pub unsafe extern "C" fn rust_util_bytes_mut(buf: *mut c_uchar, len: usize) -> B
245211
BytesMut { buf, len }
246212
}
247213

248-
/// Convert buffer to str.
249-
///
250-
/// * `buf` - Must be a valid pointer to a null terminated array of bytes.
251-
#[no_mangle]
252-
pub unsafe extern "C" fn rust_util_cstr(buf: *const c_char) -> CStr {
253-
CStr::new(buf)
254-
}
255-
256214
/// Convert buffer to mutable str. The whole buffer is considered empty from start.
257215
///
258216
/// * `buf` - Must be a valid pointer to an array of bytes
@@ -301,17 +259,6 @@ mod tests {
301259
rust_util_zero(unsafe { rust_util_bytes_mut(core::ptr::null_mut(), 0) });
302260
}
303261

304-
#[test]
305-
fn test_rust_util_cstr() {
306-
let cstr = unsafe { rust_util_cstr(b"\0".as_ptr()) };
307-
assert_eq!(cstr.as_ref(), "");
308-
assert_eq!(cstr.len, 0);
309-
310-
let cstr = unsafe { rust_util_cstr(b"foo\0bar".as_ptr()) };
311-
assert_eq!(cstr.as_ref(), "foo");
312-
assert_eq!(cstr.len, 3);
313-
}
314-
315262
#[test]
316263
#[should_panic]
317264
fn create_invalid_bytes_mut() {

src/rust/bitbox02-rust-c/src/workflow.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ pub unsafe extern "C" fn rust_workflow_spawn_unlock() {
4444

4545
#[no_mangle]
4646
pub unsafe extern "C" fn rust_workflow_spawn_confirm(
47-
title: crate::util::CStr,
48-
body: crate::util::CStr,
47+
title: *const core::ffi::c_char,
48+
body: *const core::ffi::c_char,
4949
) {
50-
CONFIRM_TITLE = Some(title.as_ref().into());
51-
CONFIRM_BODY = Some(body.as_ref().into());
52-
50+
CONFIRM_TITLE = Some(core::ffi::CStr::from_ptr(title).to_str().unwrap().into());
51+
CONFIRM_BODY = Some(core::ffi::CStr::from_ptr(body).to_str().unwrap().into());
5352
CONFIRM_PARAMS = Some(confirm::Params {
5453
title: CONFIRM_TITLE.as_ref().unwrap(),
5554
body: CONFIRM_BODY.as_ref().unwrap(),

src/u2f/u2f_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void u2f_app_confirm_start(enum u2f_app_confirm_t type, const uint8_t* app_id)
213213
}
214214
_state.outstanding_confirm = type;
215215
memcpy(_state.app_id, app_id, 32);
216-
rust_workflow_spawn_confirm(rust_util_cstr(title), rust_util_cstr(app_string));
216+
rust_workflow_spawn_confirm(title, app_string);
217217
}
218218

219219
async_op_result_t u2f_app_confirm_retry(enum u2f_app_confirm_t type, const uint8_t* app_id)

0 commit comments

Comments
 (0)