Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "dereferencing a null pointer" in C layout tests #2203

Merged
merged 1 commit into from
May 17, 2022

Conversation

gh2o
Copy link
Contributor

@gh2o gh2o commented May 12, 2022

Instead of dereferencing a null pointer, create a MaybeUninit from which we can extract well-defined addresses.

Instead of dereferencing a null pointer, create a MaybeUninit from which
we can extract well-defined addresses.
@gh2o gh2o force-pushed the no-null-deref-in-layout-tests branch from be49600 to a191871 Compare May 12, 2022 21:00
@kulp
Copy link
Member

kulp commented May 12, 2022

This looks as though it would close #1651.

@kulp kulp linked an issue May 12, 2022 that may be closed by this pull request
@dlrobertson
Copy link

Will also fix: #2204

&(*(::#prefix::ptr::null::<#canonical_ident>())).#field_name as *const _ as usize
let uninit = ::#prefix::mem::MaybeUninit::<#canonical_ident>::uninit();
let ptr = uninit.as_ptr();
::#prefix::ptr::addr_of!((*ptr).#field_name) as usize - ptr as usize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using offset_from() might work here (as #field_offset will be a literal you don't need to worry about usize/isize conversions)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset_from() assumes that the two pointers have the same type, which is not the case in this case, and if we were to cast to *const u8, I would rather just directly use usize.

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If tests are happy, I'm happy. Thanks for this!

@emilio emilio merged commit 5a01c55 into rust-lang:master May 17, 2022
@kulp kulp linked an issue May 17, 2022 that may be closed by this pull request
simonrw added a commit to simonrw/rust-fitsio that referenced this pull request Jul 11, 2022
It seems just a version bump was required.

Looks like this PR: rust-lang/rust-bindgen#2203
lopopolo added a commit to artichoke/artichoke that referenced this pull request Sep 23, 2022
Pull in fix for rust-lang/rust-bindgen#2203 and
remove allow on null pointer dereference lint.
mulkieran added a commit to mulkieran/stratisd that referenced this pull request Jan 3, 2023
michaellass added a commit to michaellass/e2p-sys that referenced this pull request Feb 15, 2023
copybara-service bot pushed a commit to chromeos/adhd that referenced this pull request May 31, 2023
bindgen is using addr_of! instead of "reference first and convert
to pointer later" since
rust-lang/rust-bindgen#2203.
Regenerate bindings.rs with bindgen 0.63.0.

BUG=b:239850356
TEST=cargo clippy

Change-Id: Id4f6f4c0ccbf222de27cd35d42dbdda7958dac2f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/4570362
Commit-Queue: Chih-Yang Hsia <[email protected]>
Tested-by: [email protected] <[email protected]>
Tested-by: ChromeOS Audio Quick Verifier <[email protected]>
Tested-by: Chih-Yang Hsia <[email protected]>
Reviewed-by: Li-Yu Yu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants