]> gcc.gnu.org Git - gcc.git/commit
c++: Fix handling of decls with flexible array members initialized with side-effects...
authorJakub Jelinek <jakub@redhat.com>
Wed, 15 Sep 2021 20:21:17 +0000 (22:21 +0200)
committerJakub Jelinek <jakub@redhat.com>
Wed, 15 Sep 2021 20:21:17 +0000 (22:21 +0200)
commite5d1af8a07ae9fcc40ea5c781c3ad46d20ea12a6
treed7fcafbe23d57f089ee8933f3d7ca0dd9f98fdd7
parent2ab5c3d5457f0d480c6423ee7ac52ce1f98592c9
c++: Fix handling of decls with flexible array members initialized with side-effects [PR88578]

> > Note, if the flexible array member is initialized only with non-constant
> > initializers, we have a worse bug that this patch doesn't solve, the
> > splitting of initializers into constant and dynamic initialization removes
> > the initializer and we don't have just wrong DECL_*SIZE, but nothing is
> > emitted when emitting those vars into assembly either and so the dynamic
> > initialization clobbers other vars that may overlap the variable.
> > I think we need keep an empty CONSTRUCTOR elt in DECL_INITIAL for the
> > flexible array member in that case.
>
> Makes sense.

So, the following patch fixes that.

The typeck2.c change makes sure we keep those CONSTRUCTORs around (although
they should be empty because all their elts had side-effects/was
non-constant if it was removed earlier), and the varasm.c change is to avoid
ICEs on those as well as ICEs on other flex array members that had some
initializers without side-effects, but not on the last array element.

The code was already asserting that the (index of the last elt in the
CONSTRUCTOR + 1) times elt size is equal to TYPE_SIZE_UNIT of the local->val
type, which is true for C flex arrays or for C++ if they don't have any
side-effects or the last elt doesn't have side-effects, this patch changes
that to assertion that the TYPE_SIZE_UNIT is greater than equal to the
offset of the end of last element in the CONSTRUCTOR and uses TYPE_SIZE_UNIT
(int_size_in_bytes) in the code later on.

2021-09-15  Jakub Jelinek  <jakub@redhat.com>

PR c++/88578
PR c++/102295
gcc/
* varasm.c (output_constructor_regular_field): Instead of assertion
that array_size_for_constructor result is equal to size of
TREE_TYPE (local->val) in bytes, assert that the type size is greater
or equal to array_size_for_constructor result and use type size as
fieldsize.
gcc/cp/
* typeck2.c (split_nonconstant_init_1): Don't throw away empty
initializers of flexible array members if they have non-zero type
size.
gcc/testsuite/
* g++.dg/ext/flexary39.C: New test.
* g++.dg/ext/flexary40.C: New test.
gcc/cp/typeck2.c
gcc/testsuite/g++.dg/ext/flexary39.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/flexary40.C [new file with mode: 0644]
gcc/varasm.c
This page took 0.064972 seconds and 6 git commands to generate.