-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3600 - tiif:non-null-posix-memalign, r=RalfJung
Use non-null pointer for size 0 posix memalign Fixes #3576
- Loading branch information
Showing
6 changed files
with
73 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/fail-dep/libc/posix_memalign_size_zero_double_free.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//@ignore-target-windows: No posix_memalign on Windows | ||
|
||
use std::ptr; | ||
|
||
fn main() { | ||
let mut ptr: *mut libc::c_void = ptr::null_mut(); | ||
let align = 64; | ||
let size = 0; | ||
unsafe { | ||
let _ = libc::posix_memalign(&mut ptr, align, size); | ||
libc::free(ptr); | ||
libc::free(ptr); //~ERROR: dangling | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/fail-dep/libc/posix_memalign_size_zero_double_free.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling | ||
--> $DIR/posix_memalign_size_zero_double_free.rs:LL:CC | ||
| | ||
LL | libc::free(ptr); | ||
| ^^^^^^^^^^^^^^^ memory access failed: ALLOC has been freed, so this pointer is dangling | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
help: ALLOC was allocated here: | ||
--> $DIR/posix_memalign_size_zero_double_free.rs:LL:CC | ||
| | ||
LL | let _ = libc::posix_memalign(&mut ptr, align, size); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: ALLOC was deallocated here: | ||
--> $DIR/posix_memalign_size_zero_double_free.rs:LL:CC | ||
| | ||
LL | libc::free(ptr); | ||
| ^^^^^^^^^^^^^^^ | ||
= note: BACKTRACE (of the first span): | ||
= note: inside `main` at $DIR/posix_memalign_size_zero_double_free.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//@ignore-target-windows: No posix_memalign on Windows | ||
|
||
use std::ptr; | ||
|
||
fn main() { | ||
let mut ptr: *mut libc::c_void = ptr::null_mut(); | ||
let align = 64; | ||
let size = 0; | ||
let _ = unsafe { libc::posix_memalign(&mut ptr, align, size) }; //~ERROR: memory leak | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: memory leaked: ALLOC (C heap, size: 0, align: 64), allocated here: | ||
--> $DIR/posix_memalign_size_zero_leak.rs:LL:CC | ||
| | ||
LL | let _ = unsafe { libc::posix_memalign(&mut ptr, align, size) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/posix_memalign_size_zero_leak.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
note: the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters