Skip to content

Commit 5bf5400

Browse files
committed
rust: mark rust_util_bytes_mut unsafe
It is an unsafe function like `rust_util_bytes()` as the caller must make sure that the provided buffer is valid for the given size.
1 parent 76ea1aa commit 5bf5400

File tree

1 file changed

+7
-5
lines changed
  • src/rust/bitbox02-rust-c/src

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub extern "C" fn rust_util_bytes(buf: *const c_uchar, len: usize) -> Bytes {
241241
/// * `buf` - Must be a valid pointer to an array of bytes
242242
/// * `len` - Length of buffer, `buf[len-1]` must be a valid dereference
243243
#[no_mangle]
244-
pub extern "C" fn rust_util_bytes_mut(buf: *mut c_uchar, len: usize) -> BytesMut {
244+
pub unsafe extern "C" fn rust_util_bytes_mut(buf: *mut c_uchar, len: usize) -> BytesMut {
245245
BytesMut { buf, len }
246246
}
247247

@@ -286,19 +286,19 @@ mod tests {
286286
#[test]
287287
fn zeroing() {
288288
let mut buf = [1u8, 2, 3, 4];
289-
rust_util_zero(rust_util_bytes_mut(buf.as_mut_ptr(), buf.len() - 1));
289+
rust_util_zero(unsafe { rust_util_bytes_mut(buf.as_mut_ptr(), buf.len() - 1) });
290290
assert_eq!(&buf[..], &[0, 0, 0, 4]);
291291
}
292292

293293
#[test]
294294
fn zeroing_empty() {
295295
let mut buf = [];
296-
rust_util_zero(rust_util_bytes_mut(buf.as_mut_ptr(), 0));
296+
rust_util_zero(unsafe { rust_util_bytes_mut(buf.as_mut_ptr(), 0) });
297297
}
298298

299299
#[test]
300300
fn zeroing_null() {
301-
rust_util_zero(rust_util_bytes_mut(core::ptr::null_mut(), 0));
301+
rust_util_zero(unsafe { rust_util_bytes_mut(core::ptr::null_mut(), 0) });
302302
}
303303

304304
#[test]
@@ -316,7 +316,9 @@ mod tests {
316316
#[should_panic]
317317
fn create_invalid_bytes_mut() {
318318
// Calling `as_mut()` will panic because it tries to create an invalid rust slice.
319-
rust_util_bytes_mut(core::ptr::null_mut(), 1).as_mut();
319+
unsafe {
320+
rust_util_bytes_mut(core::ptr::null_mut(), 1).as_mut();
321+
}
320322
}
321323

322324
#[test]

0 commit comments

Comments
 (0)