File tree Expand file tree Collapse file tree 4 files changed +35
-2
lines changed Expand file tree Collapse file tree 4 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,6 @@ fn main() { unsafe {
1818 // Turns out the discriminant is (currently) stored
1919 // in the 2nd pointer, so the first half is padding.
2020 let c = & p as * const _ as * const u8 ;
21- let _val = * c. add ( 0 ) ; // Get the padding byte.
21+ let _val = * c. add ( 0 ) ; // Get a padding byte.
2222 //~^ERROR: uninitialized
2323} }
Original file line number Diff line number Diff line change 11error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
22 --> $DIR/padding-enum.rs:LL:CC
33 |
4- LL | let _val = *c.add(0); // Get the padding byte.
4+ LL | let _val = *c.add(0); // Get a padding byte.
55 | ^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
66 |
77 = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
Original file line number Diff line number Diff line change 1+ use std:: mem;
2+
3+ // If this is `None`, the metadata becomes padding.
4+ type T = Option < & ' static str > ;
5+
6+ fn main ( ) { unsafe {
7+ let mut p: mem:: MaybeUninit < T > = mem:: MaybeUninit :: zeroed ( ) ;
8+ // The copy when `T` is returned from `transmute` should destroy padding
9+ // (even when we use `write_unaligned`, which under the hood uses an untyped copy).
10+ p. as_mut_ptr ( ) . write_unaligned ( mem:: transmute ( ( 0usize , 0usize ) ) ) ;
11+ // Null epresents `None`.
12+ assert ! ( matches!( * p. as_ptr( ) , None ) ) ;
13+
14+ // The second part, with the length, becomes padding.
15+ let c = & p as * const _ as * const u8 ;
16+ let _val = * c. add ( mem:: size_of :: < * const u8 > ( ) ) ; // Get a padding byte.
17+ //~^ERROR: uninitialized
18+ } }
Original file line number Diff line number Diff line change 1+ error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
2+ --> $DIR/padding-wide-ptr.rs:LL:CC
3+ |
4+ LL | let _val = *c.add(mem::size_of::<*const u8>()); // Get a padding byte.
5+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
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 $DIR/padding-wide-ptr.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+
You can’t perform that action at this time.
0 commit comments