Skip to content

Commit e6684dc

Browse files
authored
Zero out padding in custom Default trait implementations (rust-lang#2051)
* Zero out padding in custom Default trait implementations Previously, we were using `std::mem::zeroed()` which unfortunately does not necessarily zero out padding. It'd be better if the padding is zeroed out because some libraries are sensitive to non-zero'd out bytes, especially when forward/backward compatability is involved. This commit ensures all bytes are zeroed out in custom Default trait implementations.
1 parent 4116b70 commit e6684dc

File tree

206 files changed

+2114
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+2114
-419
lines changed

src/codegen/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2196,9 +2196,32 @@ impl CodeGenerator for CompInfo {
21962196

21972197
if needs_default_impl {
21982198
let prefix = ctx.trait_prefix();
2199+
let body = if ctx.options().rust_features().maybe_uninit {
2200+
quote! {
2201+
let mut s = ::#prefix::mem::MaybeUninit::<Self>::uninit();
2202+
unsafe {
2203+
::#prefix::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
2204+
s.assume_init()
2205+
}
2206+
}
2207+
} else {
2208+
quote! {
2209+
unsafe {
2210+
let mut s: Self = ::#prefix::mem::uninitialized();
2211+
::#prefix::ptr::write_bytes(&mut s, 0, 1);
2212+
s
2213+
}
2214+
}
2215+
};
2216+
// Note we use `ptr::write_bytes()` instead of `mem::zeroed()` because the latter does
2217+
// not necessarily ensure padding bytes are zeroed. Some C libraries are sensitive to
2218+
// non-zero padding bytes, especially when forwards/backwards compatability is
2219+
// involved.
21992220
result.push(quote! {
22002221
impl #generics Default for #ty_for_impl {
2201-
fn default() -> Self { unsafe { ::#prefix::mem::zeroed() } }
2222+
fn default() -> Self {
2223+
#body
2224+
}
22022225
}
22032226
});
22042227
}

tests/expectations/tests/16-byte-alignment.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
9999
}
100100
impl Default for rte_ipv4_tuple__bindgen_ty_1 {
101101
fn default() -> Self {
102-
unsafe { ::std::mem::zeroed() }
102+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
103+
unsafe {
104+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
105+
s.assume_init()
106+
}
103107
}
104108
}
105109
#[test]
@@ -143,7 +147,11 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
143147
}
144148
impl Default for rte_ipv4_tuple {
145149
fn default() -> Self {
146-
unsafe { ::std::mem::zeroed() }
150+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
151+
unsafe {
152+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
153+
s.assume_init()
154+
}
147155
}
148156
}
149157
#[repr(C)]
@@ -240,7 +248,11 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
240248
}
241249
impl Default for rte_ipv6_tuple__bindgen_ty_1 {
242250
fn default() -> Self {
243-
unsafe { ::std::mem::zeroed() }
251+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
252+
unsafe {
253+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
254+
s.assume_init()
255+
}
244256
}
245257
}
246258
#[test]
@@ -284,7 +296,11 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
284296
}
285297
impl Default for rte_ipv6_tuple {
286298
fn default() -> Self {
287-
unsafe { ::std::mem::zeroed() }
299+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
300+
unsafe {
301+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
302+
s.assume_init()
303+
}
288304
}
289305
}
290306
#[repr(C)]
@@ -333,6 +349,10 @@ fn bindgen_test_layout_rte_thash_tuple() {
333349
}
334350
impl Default for rte_thash_tuple {
335351
fn default() -> Self {
336-
unsafe { ::std::mem::zeroed() }
352+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
353+
unsafe {
354+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
355+
s.assume_init()
356+
}
337357
}
338358
}

tests/expectations/tests/16-byte-alignment_1_0.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ impl Clone for rte_thash_tuple {
390390
}
391391
impl Default for rte_thash_tuple {
392392
fn default() -> Self {
393-
unsafe { ::std::mem::zeroed() }
393+
unsafe {
394+
let mut s: Self = ::std::mem::uninitialized();
395+
::std::ptr::write_bytes(&mut s, 0, 1);
396+
s
397+
}
394398
}
395399
}

tests/expectations/tests/allowlist_basic.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ pub struct AllowlistMe_Inner<T> {
2020
}
2121
impl<T> Default for AllowlistMe_Inner<T> {
2222
fn default() -> Self {
23-
unsafe { ::std::mem::zeroed() }
23+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
24+
unsafe {
25+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
26+
s.assume_init()
27+
}
2428
}
2529
}
2630
impl<T> Default for AllowlistMe<T> {
2731
fn default() -> Self {
28-
unsafe { ::std::mem::zeroed() }
32+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
33+
unsafe {
34+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
35+
s.assume_init()
36+
}
2937
}
3038
}

tests/expectations/tests/anon-fields-prefix.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ fn bindgen_test_layout_color() {
150150
}
151151
impl Default for color {
152152
fn default() -> Self {
153-
unsafe { ::std::mem::zeroed() }
153+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
154+
unsafe {
155+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
156+
s.assume_init()
157+
}
154158
}
155159
}

tests/expectations/tests/anon_struct_in_union.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ fn bindgen_test_layout_s__bindgen_ty_1() {
7474
}
7575
impl Default for s__bindgen_ty_1 {
7676
fn default() -> Self {
77-
unsafe { ::std::mem::zeroed() }
77+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
78+
unsafe {
79+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
80+
s.assume_init()
81+
}
7882
}
7983
}
8084
#[test]
@@ -97,6 +101,10 @@ fn bindgen_test_layout_s() {
97101
}
98102
impl Default for s {
99103
fn default() -> Self {
100-
unsafe { ::std::mem::zeroed() }
104+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
105+
unsafe {
106+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
107+
s.assume_init()
108+
}
101109
}
102110
}

tests/expectations/tests/anon_union.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ pub union TErrorResult__bindgen_ty_1 {
3838
}
3939
impl Default for TErrorResult__bindgen_ty_1 {
4040
fn default() -> Self {
41-
unsafe { ::std::mem::zeroed() }
41+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
42+
unsafe {
43+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
44+
s.assume_init()
45+
}
4246
}
4347
}
4448
impl Default for TErrorResult {
4549
fn default() -> Self {
46-
unsafe { ::std::mem::zeroed() }
50+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
51+
unsafe {
52+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
53+
s.assume_init()
54+
}
4755
}
4856
}
4957
#[repr(C)]
@@ -65,7 +73,11 @@ fn bindgen_test_layout_ErrorResult() {
6573
}
6674
impl Default for ErrorResult {
6775
fn default() -> Self {
68-
unsafe { ::std::mem::zeroed() }
76+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
77+
unsafe {
78+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
79+
s.assume_init()
80+
}
6981
}
7082
}
7183
#[test]

tests/expectations/tests/anon_union_1_0.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ pub struct TErrorResult__bindgen_ty_1 {
8383
}
8484
impl Default for TErrorResult {
8585
fn default() -> Self {
86-
unsafe { ::std::mem::zeroed() }
86+
unsafe {
87+
let mut s: Self = ::std::mem::uninitialized();
88+
::std::ptr::write_bytes(&mut s, 0, 1);
89+
s
90+
}
8791
}
8892
}
8993
#[repr(C)]
@@ -111,7 +115,11 @@ impl Clone for ErrorResult {
111115
}
112116
impl Default for ErrorResult {
113117
fn default() -> Self {
114-
unsafe { ::std::mem::zeroed() }
118+
unsafe {
119+
let mut s: Self = ::std::mem::uninitialized();
120+
::std::ptr::write_bytes(&mut s, 0, 1);
121+
s
122+
}
115123
}
116124
}
117125
#[test]

tests/expectations/tests/anonymous-template-types.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ pub struct Foo<T> {
1313
}
1414
impl<T> Default for Foo<T> {
1515
fn default() -> Self {
16-
unsafe { ::std::mem::zeroed() }
16+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
17+
unsafe {
18+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
19+
s.assume_init()
20+
}
1721
}
1822
}
1923
#[repr(C)]
@@ -29,7 +33,11 @@ pub struct Quux<V> {
2933
}
3034
impl<V> Default for Quux<V> {
3135
fn default() -> Self {
32-
unsafe { ::std::mem::zeroed() }
36+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
37+
unsafe {
38+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
39+
s.assume_init()
40+
}
3341
}
3442
}
3543
#[repr(C)]

tests/expectations/tests/bad-namespace-parenthood-inheritance.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ pub struct std_char_traits {
1212
}
1313
impl Default for std_char_traits {
1414
fn default() -> Self {
15-
unsafe { ::std::mem::zeroed() }
15+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
16+
unsafe {
17+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
18+
s.assume_init()
19+
}
1620
}
1721
}
1822
#[repr(C)]

tests/expectations/tests/bitfield_align_2.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ fn bindgen_test_layout_TaggedPtr() {
121121
}
122122
impl Default for TaggedPtr {
123123
fn default() -> Self {
124-
unsafe { ::std::mem::zeroed() }
124+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
125+
unsafe {
126+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
127+
s.assume_init()
128+
}
125129
}
126130
}
127131
impl TaggedPtr {

tests/expectations/tests/blocklist-and-impl-debug.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ fn bindgen_test_layout_ShouldManuallyImplDebug() {
4040
}
4141
impl Default for ShouldManuallyImplDebug {
4242
fn default() -> Self {
43-
unsafe { ::std::mem::zeroed() }
43+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
44+
unsafe {
45+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
46+
s.assume_init()
47+
}
4448
}
4549
}
4650
impl ::std::fmt::Debug for ShouldManuallyImplDebug {

tests/expectations/tests/blocks-signature.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ fn bindgen_test_layout_contains_block_pointers() {
7676
}
7777
impl Default for contains_block_pointers {
7878
fn default() -> Self {
79-
unsafe { ::std::mem::zeroed() }
79+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
80+
unsafe {
81+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
82+
s.assume_init()
83+
}
8084
}
8185
}
8286
pub type _bindgen_ty_id_33 = *const ::block::Block<(), ()>;

tests/expectations/tests/blocks.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ fn bindgen_test_layout_contains_block_pointers() {
7575
}
7676
impl Default for contains_block_pointers {
7777
fn default() -> Self {
78-
unsafe { ::std::mem::zeroed() }
78+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
79+
unsafe {
80+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
81+
s.assume_init()
82+
}
7983
}
8084
}

tests/expectations/tests/c_naming.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ fn bindgen_test_layout_union_b() {
7575
}
7676
impl Default for union_b {
7777
fn default() -> Self {
78-
unsafe { ::std::mem::zeroed() }
78+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
79+
unsafe {
80+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
81+
s.assume_init()
82+
}
7983
}
8084
}
8185
pub type b = union_b;

0 commit comments

Comments
 (0)