Rollup merge of #40373 - TimNN:test-ub-packed, r=arielb1
Fix UB in repr(packed) tests r? @arielb1 cc #37609 and #27060
This commit is contained in:
commit
d75b9ad5f4
@ -8,14 +8,36 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[repr(packed)]
|
#[repr(packed)]
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone)]
|
||||||
struct Foo {
|
struct Foo {
|
||||||
a: i8,
|
a: i8,
|
||||||
b: i16,
|
b: i16,
|
||||||
c: i8
|
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")]
|
#[link(name = "test", kind = "static")]
|
||||||
extern {
|
extern {
|
||||||
fn foo(f: Foo) -> Foo;
|
fn foo(f: Foo) -> Foo;
|
||||||
|
@ -8,15 +8,34 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
#[repr(packed)]
|
#[repr(packed)]
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone)]
|
||||||
struct Foo {
|
struct Foo {
|
||||||
bar: u8,
|
bar: u8,
|
||||||
baz: u64
|
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() {
|
pub fn main() {
|
||||||
let foos = [Foo { bar: 1, baz: 2 }; 10];
|
let foos = [Foo { bar: 1, baz: 2 }; 10];
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user