Skip to content

Commit

Permalink
Rollup merge of rust-lang#40373 - TimNN:test-ub-packed, r=arielb1
Browse files Browse the repository at this point in the history
Fix UB in repr(packed) tests

r? @arielb1

cc rust-lang#37609 and rust-lang#27060
  • Loading branch information
Ariel Ben-Yehuda authored Mar 11, 2017
2 parents 828625c + 79a7ee8 commit 7dd28e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/test/run-make/extern-fn-with-packed-struct/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,36 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;

#[repr(packed)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone)]
struct Foo {
a: i8,
b: i16,
c: i8
}

impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
self.a == other.a && self.b == other.b && self.c == other.c
}
}

impl fmt::Debug for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let a = self.a;
let b = self.b;
let c = self.c;

f.debug_struct("Foo")
.field("a", &a)
.field("b", &b)
.field("c", &c)
.finish()
}
}

#[link(name = "test", kind = "static")]
extern {
fn foo(f: Foo) -> Foo;
Expand Down
21 changes: 20 additions & 1 deletion src/test/run-pass/packed-struct-vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,34 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use std::mem;

#[repr(packed)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone)]
struct Foo {
bar: u8,
baz: u64
}

impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
self.bar == other.bar && self.baz == other.baz
}
}

impl fmt::Debug for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let bar = self.bar;
let baz = self.baz;

f.debug_struct("Foo")
.field("bar", &bar)
.field("baz", &baz)
.finish()
}
}

pub fn main() {
let foos = [Foo { bar: 1, baz: 2 }; 10];

Expand Down

0 comments on commit 7dd28e2

Please sign in to comment.