Skip to content

Commit

Permalink
Fix the deref_nullptr warning
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed May 23, 2021
1 parent 9137184 commit 5d5b425
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,9 +1019,15 @@ impl TestGenerator {
}
macro_rules! offset_of {
($ty:ident, $field:ident) => (
(&((*(0 as *const $ty)).$field)) as *const _ as u64
)
($ty:ident, $field:ident) => ({
let zeroed_ty = std::mem::zeroed::<$ty>();
let ty_ptr = &zeroed_ty as *const $ty;
let field_ptr = std::ptr::addr_of!(zeroed_ty.$field);
let ty_address = ty_ptr as u64;
let field_address = field_ptr as u64;
std::mem::forget(zeroed_ty);
field_address.checked_sub(ty_address).unwrap()
})
}
"#
Expand Down

0 comments on commit 5d5b425

Please sign in to comment.