[Bug middle-end/101836] __builtin_object_size(P->M, 1) where M is an array and the last member of a struct fails

qinzhao at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Jun 8 14:09:50 GMT 2022


--- Comment #12 from qinzhao at gcc dot gnu.org ---
In the current tree-object-size.cc, "addr_object_size", it's clearly state the

 607               /* For &X->fld, compute object size only if fld isn't the
 608                  field, as struct { int i; char c[1]; } is often used
 609                  of flexible array member.  */

and these part of codes were added back to 2009 with commit

it's reasonable to add a new option -fstrict-flex-arrays to remove the
"trailing array is a flex array" assumptions in current GCC. 

and the following utility routine that is added in tree.[h|cc] in 2020 can be
used to identify whether a trailing array member reference is a flexible array
or not:

/* Describes a "special" array member due to which component_ref_size
   returns null.  */
enum struct special_array_member
   none,      /* Not a special array member.  */
   int_0,     /* Interior array member with size zero.  */
   trail_0,   /* Trailing array member with size zero.  */
   trail_1    /* Trailing array member with one element.  */

/* Determines the size of the member referenced by the COMPONENT_REF
   REF, using its initializer expression if necessary in order to
   determine the size of an initialized flexible array member.
   If non-null, set *ARK when REF refers to an interior zero-length
   array or a trailing one-element array.
   Returns the size as sizetype (which might be zero for an object
   with an uninitialized flexible array member) or null if the size
   cannot be determined.  */

component_ref_size (tree ref, special_array_member *sam /* = NULL */)

More information about the Gcc-bugs mailing list