Skip to content

Commit 7dd28e2

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40373 - TimNN:test-ub-packed, r=arielb1
Fix UB in repr(packed) tests r? @arielb1 cc rust-lang#37609 and rust-lang#27060
2 parents 828625c + 79a7ee8 commit 7dd28e2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/test/run-make/extern-fn-with-packed-struct/test.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,36 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::fmt;
12+
1113
#[repr(packed)]
12-
#[derive(Copy, Clone, PartialEq, Debug)]
14+
#[derive(Copy, Clone)]
1315
struct Foo {
1416
a: i8,
1517
b: i16,
1618
c: i8
1719
}
1820

21+
impl PartialEq for Foo {
22+
fn eq(&self, other: &Foo) -> bool {
23+
self.a == other.a && self.b == other.b && self.c == other.c
24+
}
25+
}
26+
27+
impl fmt::Debug for Foo {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
let a = self.a;
30+
let b = self.b;
31+
let c = self.c;
32+
33+
f.debug_struct("Foo")
34+
.field("a", &a)
35+
.field("b", &b)
36+
.field("c", &c)
37+
.finish()
38+
}
39+
}
40+
1941
#[link(name = "test", kind = "static")]
2042
extern {
2143
fn foo(f: Foo) -> Foo;

src/test/run-pass/packed-struct-vec.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,34 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::fmt;
1112
use std::mem;
1213

1314
#[repr(packed)]
14-
#[derive(Copy, Clone, PartialEq, Debug)]
15+
#[derive(Copy, Clone)]
1516
struct Foo {
1617
bar: u8,
1718
baz: u64
1819
}
1920

21+
impl PartialEq for Foo {
22+
fn eq(&self, other: &Foo) -> bool {
23+
self.bar == other.bar && self.baz == other.baz
24+
}
25+
}
26+
27+
impl fmt::Debug for Foo {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
let bar = self.bar;
30+
let baz = self.baz;
31+
32+
f.debug_struct("Foo")
33+
.field("bar", &bar)
34+
.field("baz", &baz)
35+
.finish()
36+
}
37+
}
38+
2039
pub fn main() {
2140
let foos = [Foo { bar: 1, baz: 2 }; 10];
2241

0 commit comments

Comments
 (0)