aboutsummaryrefslogtreecommitdiff
path: root/gcc/stor-layout.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2018-02-13 21:23:58 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2018-02-13 21:23:58 +0100
commit201d17c0d835112bdea5408e6eff4e250f3c238a (patch)
tree92d22bae34f8e96ab555625eb004d08a97d5a9d6 /gcc/stor-layout.c
parent9aa484f5a9c74509ecf9722a518b962f0634dad2 (diff)
downloadgcc-201d17c0d835112bdea5408e6eff4e250f3c238a.zip
gcc-201d17c0d835112bdea5408e6eff4e250f3c238a.tar.gz
gcc-201d17c0d835112bdea5408e6eff4e250f3c238a.tar.bz2
re PR c/82210 (Having _Alignas in a struct with VLAs causes writing to one array to overwrite another)
PR c/82210 * stor-layout.c (place_field): For variable length fields, adjust offset_align afterwards not just based on the field's alignment, but also on the size. * gcc.c-torture/execute/pr82210.c: New test. From-SVN: r257635
Diffstat (limited to 'gcc/stor-layout.c')
-rw-r--r--gcc/stor-layout.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/gcc/stor-layout.c b/gcc/stor-layout.c
index 8c415ebb..5fdf81a 100644
--- a/gcc/stor-layout.c
+++ b/gcc/stor-layout.c
@@ -1622,6 +1622,30 @@ place_field (record_layout_info rli, tree field)
= size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
rli->bitpos = bitsize_zero_node;
rli->offset_align = MIN (rli->offset_align, desired_align);
+
+ if (!multiple_of_p (bitsizetype, DECL_SIZE (field),
+ bitsize_int (rli->offset_align)))
+ {
+ tree type = strip_array_types (TREE_TYPE (field));
+ /* The above adjusts offset_align just based on the start of the
+ field. The field might not have a size that is a multiple of
+ that offset_align though. If the field is an array of fixed
+ sized elements, assume there can be any multiple of those
+ sizes. If it is a variable length aggregate or array of
+ variable length aggregates, assume worst that the end is
+ just BITS_PER_UNIT aligned. */
+ if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
+ {
+ if (TREE_INT_CST_LOW (TYPE_SIZE (type)))
+ {
+ unsigned HOST_WIDE_INT sz
+ = least_bit_hwi (TREE_INT_CST_LOW (TYPE_SIZE (type)));
+ rli->offset_align = MIN (rli->offset_align, sz);
+ }
+ }
+ else
+ rli->offset_align = MIN (rli->offset_align, BITS_PER_UNIT);
+ }
}
else if (targetm.ms_bitfield_layout_p (rli->t))
{