From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DF9F53856DD7; Fri, 10 Jun 2022 20:15:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF9F53856DD7 From: "qinzhao at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/101836] __builtin_object_size(P->M, 1) where M is an array and the last member of a struct fails Date: Fri, 10 Jun 2022 20:15:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: qinzhao at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: qinzhao at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Jun 2022 20:15:19 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101836 --- 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_t= ree ptr, else if (var !=3D pt_var && TREE_CODE (pt_var) =3D=3D MEM_REF) { tree v =3D 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 inste= ad - of flexible array member. */ + /* For &X->fld, compute object size if fld isn't a flexible a= rray + member. */ while (v && v !=3D 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))) =3D=3D RECORD_TYPE) { - tree fld_chain =3D DECL_CHAIN (TREE_OPERAND (v, 1)); - for (; fld_chain; fld_chain =3D DECL_CHAIN (fld_cha= in)) - if (TREE_CODE (fld_chain) =3D=3D FIELD_DECL) - break; - - if (fld_chain) + bool is_flexible_array =3D false; + /* Set for accesses to special trailing arrays. */ + special_array_member sam{ }; + + tree refsize =3D 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 =3D=3D special_array_member::trail_0 + || sam =3D=3D special_array_member::trail_1 + || flexible_array_type_p (TREE_TYPE (TREE_OPERA= ND (v,0)))) + is_flexible_array =3D true; + + if (!is_flexible_array) { v =3D NULL_TREE;=