@@ -47,11 +47,9 @@ pub enum StatusCode {
47
47
fn status_create_safe ( code : StatusCode , mod_id : u32 , file : String , arg : i32 ) -> RawStatus {
48
48
// We do not expect an error since it is a valid String.
49
49
let file = CString :: new ( file) . expect ( "CString::new failed" ) ;
50
- unsafe {
51
- // Safety: the function expects a valid readonly C-string which is exactly what
52
- // CString:as_ptr() provides.
53
- status_create ( code as u32 , mod_id, file. as_ptr ( ) , arg)
54
- }
50
+ // SAFETY: the function expects a valid readonly C-string which is exactly what
51
+ // CString:as_ptr() provides.
52
+ unsafe { status_create ( code as u32 , mod_id, file. as_ptr ( ) , arg) }
55
53
// Note: file is dropped here so the C-string pointer is valid accross the function call.
56
54
}
57
55
@@ -82,21 +80,20 @@ pub struct Status {
82
80
impl Status {
83
81
pub fn from_raw_status ( status : RawStatus ) -> Result < Status > {
84
82
// We do not care about the code string but status_extract expects a non-null pointer.
85
- let mut _code_str : * const std:: os:: raw:: c_char = std:: ptr:: null ( ) ;
83
+ let mut code_str : * const std:: os:: raw:: c_char = std:: ptr:: null ( ) ;
86
84
let mut arg = 0i32 ;
87
85
let mut mod_id: [ std:: os:: raw:: c_char ; 3 ] = [ 0 ; 3 ] ;
88
- let is_err_status = unsafe {
89
- // Safety: status_extract expects:
90
- // - a non-null pointer to string pointer that will be updated to point
91
- // to the english name of the error code,
92
- // - a non-null pointer to an integer (argument),
93
- // - a non-null pointer to a char[3] buffer that is filled with the module ID.
94
- status_extract ( status, & mut _code_str, & mut arg, & mut mod_id as * mut i8 )
95
- } ;
86
+ let mod_id_ptr = & mut mod_id as * mut i8 ;
87
+ // SAFETY: status_extract expects:
88
+ // - a non-null pointer to string pointer that will be updated to point
89
+ // to the english name of the error code,
90
+ // - a non-null pointer to an integer (argument),
91
+ // - a non-null pointer to a char[3] buffer that is filled with the module ID.
92
+ let is_err_status = unsafe { status_extract ( status, & mut code_str, & mut arg, mod_id_ptr) } ;
96
93
let code = match is_err_status {
97
94
false => StatusCode :: Ok ,
98
95
true => {
99
- // Safety : nothing unsafe except that it's an FFI call.
96
+ // SAFETY : nothing unsafe except that it's an FFI call.
100
97
let raw_code = unsafe { status_err ( status) } ;
101
98
StatusCode :: try_from ( raw_code) ?
102
99
}
@@ -239,11 +236,14 @@ pub fn load_elf(elf_file: &PathBuf) -> Result<StatusCreateRecords> {
239
236
// it really is safe.
240
237
let records = status_create_records
241
238
. chunks ( RECORD_SIZE )
242
- . map ( |chunk| unsafe {
239
+ . map ( |chunk| {
243
240
// We need to provide transmute with a fixed-size array but chunk does not give us one.
244
241
// If/When as_chunks is stabilized, we can get rid of this conversion.
245
242
let chunk = <[ u8 ; RECORD_SIZE ] >:: try_from ( chunk) . unwrap ( ) ;
246
- std:: mem:: transmute :: < [ u8 ; RECORD_SIZE ] , ot_status_create_record_t > ( chunk)
243
+ // SAFETY: `chunk` comes from `struct ot_status_create_record_t` types in C which
244
+ // `ot_status_create_record_t` was `bindgen`'d from. Its bytes should always be a
245
+ // valid value for this type.
246
+ unsafe { std:: mem:: transmute :: < [ u8 ; RECORD_SIZE ] , ot_status_create_record_t > ( chunk) }
247
247
} )
248
248
. map ( StatusCreateRecord :: try_from)
249
249
. collect :: < Result < _ > > ( ) ?;
0 commit comments