On Tue, Jan 22, 2019 at 06:18:28PM -0700, Martin Sebor wrote: > PS In GCC 10, unless there is an important use case that escapes > me, I think GCC should warn for zero-length non-member array > objects, or perhaps even for internal struct members (those followed > by another member). Not to avoid these sorts of bugs but because > they seem too dangerous to use safely.
No, we shouldn't warn. > --- gcc/gimple-fold.c (revision 268086) > +++ gcc/gimple-fold.c (working copy) > @@ -6715,12 +6715,14 @@ fold_array_ctor_reference (tree type, tree ctor, > elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); > > /* When TYPE is non-null, verify that it specifies a constant-sized > - accessed not larger than size of array element. */ > - if (type > - && (!TYPE_SIZE_UNIT (type) > - || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST > - || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type)) > - || elt_size == 0)) > + accessed not larger than size of array element. Avoid using zero > + ELT_SIZE, the result of an empty initializer for a zero-length > + array. */ The comment is misleading, there are many reasons why you could have zero elt_size, C struct S {}, or int x[10][0], etc. Just don't change anything in the comment at all, or say Punt if element size is zero to avoid division by zero. or something similar. > + if (elt_size == 0 > + || (type > + && (!TYPE_SIZE_UNIT (type) > + || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST > + || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type))))) > return NULL_TREE; Otherwise LGTM. If you could fix also the two comments a few lines above this: /* Static constructors for variably sized objects makes no sense. */ to /* Static constructors for variably sized objects make no sense. */ it would be appreciated. Jakub