about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/uninit/padding-struct-in-union.rs
blob: 132b85828362d7a631260bd3fb1e3317deba83c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#[repr(C)]
#[derive(Debug, Copy, Clone)]
struct Foo {
    val16: u16,
    // Padding bytes go here!
    val32: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
struct Bar {
    bytes: [u8; 8],
}

#[repr(C)]
union FooBar {
    foo: Foo,
    bar: Bar,
}

pub fn main() {
    // Initialize as u8 to ensure padding bytes are zeroed.
    let mut foobar = FooBar { bar: Bar { bytes: [0u8; 8] } };
    // Reading either field is ok.
    let _val = unsafe { (foobar.foo, foobar.bar) };
    // Does this assignment copy the uninitialized padding bytes
    // over the initialized padding bytes? miri doesn't seem to think so.
    foobar.foo = Foo { val16: 1, val32: 2 };
    // This resets the padding to uninit.
    let _val = unsafe { (foobar.foo, foobar.bar) };
    //~^ ERROR: uninitialized
}