https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836

--- Comment #15 from qinzhao at gcc dot gnu.org ---
the following patch will fix the issue with this testing case:

[opc@qinzhao-ol8u3-x86 gcc]$ git diff
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index 5ca87ae3504..7df092346b9 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -604,9 +604,8 @@ addr_object_size (struct object_size_info *osi, const_tree
ptr,
          else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF)
            {
              tree v = var;
-             /* For &X->fld, compute object size only if fld isn't the last
-                field, as struct { int i; char c[1]; } is often used instead
-                of flexible array member.  */
+             /* For &X->fld, compute object size if fld isn't a flexible array
+                member.  */
              while (v && v != pt_var)
                switch (TREE_CODE (v))
                  {
@@ -645,12 +644,19 @@ addr_object_size (struct object_size_info *osi,
const_tree ptr,
                        && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
                           == RECORD_TYPE)
                      {
-                       tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
-                       for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
-                         if (TREE_CODE (fld_chain) == FIELD_DECL)
-                           break;
-
-                       if (fld_chain)
+                       bool is_flexible_array = false;
+                       /* Set for accesses to special trailing arrays.  */
+                       special_array_member sam{ };
+
+                       tree refsize = component_ref_size (v, &sam);
+                       /* if the array is a special trailing array, don't
compute
+                        * its size, otherwise, treat it as a normal array.  */
+                       if (sam == special_array_member::trail_0
+                           || sam == special_array_member::trail_1
+                           || flexible_array_type_p (TREE_TYPE (TREE_OPERAND
(v,0))))
+                         is_flexible_array = true;
+
+                       if (!is_flexible_array)
                          {
                            v = NULL_TREE;

Reply via email to