Skip to content

Commit afea346

Browse files
feat: add failing test for zero-sized memset
Signed-off-by: vishruth-thimmaiah <[email protected]>
1 parent 27afeb0 commit afea346

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
837837
#[expect(clippy::as_conversions)]
838838
let val = val as u8;
839839

840+
// C requires that this must always be a valid pointer, even if `n` is zero, so we better check that.
840841
this.ptr_get_alloc_id(ptr_dest, 0)?;
841842

842843
let bytes = std::iter::repeat_n(val, n.try_into().unwrap());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::ptr;
2+
3+
// null is explicitly called out as UB in the C docs for `memset`.
4+
fn main() {
5+
unsafe {
6+
libc::memset(ptr::null_mut(), 0, 0); //~ERROR: null pointer
7+
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: pointer not dereferenceable: pointer must point to some allocation, but got null pointer
2+
--> tests/fail-dep/libc/memset_null.rs:LL:CC
3+
|
4+
LL | libc::memset(ptr::null_mut(), 0, 0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail-dep/libc/memset_null.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)