-
Notifications
You must be signed in to change notification settings - Fork 309
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
Return non-null pointer from malloc(0) #3580
Conversation
@rustbot ready |
Thanks!
As just mentioned in the issue, this needs two new "fail-dep" tests: a double free of malloc(0), and a use after free.
|
I am trying to create a use-after-free test, but the test below surprisingly not throwing any error? unsafe {
let ptr = libc::malloc(0);
libc::free(ptr);
let _ = *ptr;
} |
Yes, that's how Use |
I see, right now this error shows up, how is pointer dereference related to Copy?
full test: fn main() {
unsafe {
let ptr = libc::malloc(0);
libc::free(ptr);
let _val = *ptr;
}
} |
c_void is not Copy so you cannot just do a deref.
But anyway this will not give the desired error as ZST accesses are NOPs. I should not have said use after free, sorry. I meant memory leak and double free.
|
Ah I see, thanks! |
@rustbot author |
There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged. You can start a rebase with the following commands:
The following commits are merge commits: |
@rustbot ready |
src/shims/alloc.rs
Outdated
if size == 0 { | ||
return Align::from_bytes(1).unwrap(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but please move it up above the comment that says we will round down.
Also, you can use Align::ONE
to avoid an unwrap here.
Awesome. :) Please squash the commits. |
6489104
to
3a2524a
Compare
@bors r+ |
☀️ Test successful - checks-actions |
I just realized this also affects the Windows HeapAlloc function. @ChrisDenton I didn't find anything in the docs about HeapAlloc with size 0... is it okay for that to return a non-null pointer that must be freed later? |
Sure that's fine. There's no documented restriction on allocating zero bytes and |
Use non-null pointer for malloc(0) as mentioned in #3576 to detect leaks and double free of
malloc(0)
addresses.