Skip to content

Commit

Permalink
Merge pull request #24 from JohnTitor/fix-dereference-null-ptr
Browse files Browse the repository at this point in the history
Fix the `deref_nullptr` warning
  • Loading branch information
JohnTitor committed May 23, 2021
2 parents 9137184 + 1e2bb72 commit 64fc427
Show file tree
Hide file tree
Showing 5 changed files with 13 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
1 change: 1 addition & 0 deletions testcrate/src/bin/t1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use libc::*;
use testcrate::t1::*;
Expand Down
1 change: 1 addition & 0 deletions testcrate/src/bin/t1_cxx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use libc::*;
use testcrate::t1::*;
Expand Down
1 change: 1 addition & 0 deletions testcrate/src/bin/t2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use testcrate::t2::*;

Expand Down
1 change: 1 addition & 0 deletions testcrate/src/bin/t2_cxx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(test))]
#![deny(warnings)]
#![allow(unaligned_references)]

use testcrate::t2::*;

Expand Down

0 comments on commit 64fc427

Please sign in to comment.