Skip to content

Commit 854359d

Browse files
author
Benjamin Lerman
committed
Fix union layout when it contains 0 sized array.
Fix #3068
1 parent 20aa65a commit 854359d

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

bindgen-tests/tests/expectations/tests/union_with_zero_sized_array.rs

+68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
union U {
2+
char d0[0];
3+
char d1[1];
4+
char d2[2];
5+
};

bindgen/codegen/struct_layout.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ impl<'a> StructLayoutTracker<'a> {
143143

144144
self.latest_offset += self.padding_bytes(layout) + layout.size;
145145
self.latest_field_layout = Some(layout);
146-
self.max_field_align = cmp::max(self.max_field_align, layout.align);
146+
self.max_field_align =
147+
cmp::max(self.max_field_align, layout.align);
147148
}
148149
}
149150

@@ -266,7 +267,11 @@ impl<'a> StructLayoutTracker<'a> {
266267
}
267268
};
268269

269-
self.latest_offset += field_layout.size;
270+
if !is_union {
271+
self.latest_offset += field_layout.size;
272+
} else {
273+
self.latest_offset = cmp::max(self.latest_offset, field_layout.size);
274+
}
270275
self.latest_field_layout = Some(field_layout);
271276
self.max_field_align =
272277
cmp::max(self.max_field_align, field_layout.align);

0 commit comments

Comments
 (0)