diff options
| author | bors <bors@rust-lang.org> | 2024-04-18 10:27:42 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-04-18 10:27:42 +0000 |
| commit | 1ce64e717f01ac40aae6c2321114d293993841bd (patch) | |
| tree | ec30c41bb93a72689325992afb0d0697b8ac19cc /src/tools/miri/tests | |
| parent | 5c9924a599bb9f04ca8d0d8760b1be52d77fb3fa (diff) | |
| parent | 5ff9b2b8651255550eaa815d3c4927967c6b34f7 (diff) | |
| download | rust-1ce64e717f01ac40aae6c2321114d293993841bd.tar.gz rust-1ce64e717f01ac40aae6c2321114d293993841bd.zip | |
Auto merge of #3484 - RalfJung:realloc, r=RalfJung
make realloc with a size of zero fail Fixes https://github.com/rust-lang/miri/issues/2774
Diffstat (limited to 'src/tools/miri/tests')
| -rw-r--r-- | src/tools/miri/tests/fail-dep/realloc-zero.rs | 10 | ||||
| -rw-r--r-- | src/tools/miri/tests/fail-dep/realloc-zero.stderr | 15 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass-dep/malloc.rs | 5 |
3 files changed, 27 insertions, 3 deletions
diff --git a/src/tools/miri/tests/fail-dep/realloc-zero.rs b/src/tools/miri/tests/fail-dep/realloc-zero.rs new file mode 100644 index 00000000000..1482798e90c --- /dev/null +++ b/src/tools/miri/tests/fail-dep/realloc-zero.rs @@ -0,0 +1,10 @@ +//@ignore-target-windows: No libc on Windows + +fn main() { + unsafe { + let p1 = libc::malloc(20); + // C made this UB... + let p2 = libc::realloc(p1, 0); //~ERROR: `realloc` with a size of zero + assert!(p2.is_null()); + } +} diff --git a/src/tools/miri/tests/fail-dep/realloc-zero.stderr b/src/tools/miri/tests/fail-dep/realloc-zero.stderr new file mode 100644 index 00000000000..749a61f7396 --- /dev/null +++ b/src/tools/miri/tests/fail-dep/realloc-zero.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: `realloc` with a size of zero + --> $DIR/realloc-zero.rs:LL:CC + | +LL | let p2 = libc::realloc(p1, 0); + | ^^^^^^^^^^^^^^^^^^^^ `realloc` with a size of zero + | + = 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 + = note: BACKTRACE: + = note: inside `main` at $DIR/realloc-zero.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 + diff --git a/src/tools/miri/tests/pass-dep/malloc.rs b/src/tools/miri/tests/pass-dep/malloc.rs index f5e014c000d..35cd137931f 100644 --- a/src/tools/miri/tests/pass-dep/malloc.rs +++ b/src/tools/miri/tests/pass-dep/malloc.rs @@ -34,9 +34,8 @@ fn main() { } unsafe { - let p1 = libc::malloc(20); - - let p2 = libc::realloc(p1, 0); + // Realloc with size 0 is okay for the null pointer + let p2 = libc::realloc(ptr::null_mut(), 0); assert!(p2.is_null()); } |
