From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from buffalo.tulip.relay.mailchannels.net (buffalo.tulip.relay.mailchannels.net [23.83.218.24]) by sourceware.org (Postfix) with ESMTPS id 10F173858D33 for ; Wed, 1 Feb 2023 16:48:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 10F173858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 17B23540D5A; Wed, 1 Feb 2023 16:48:43 +0000 (UTC) Received: from pdx1-sub0-mail-a305.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 81E905414AF; Wed, 1 Feb 2023 16:48:42 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1675270122; a=rsa-sha256; cv=none; b=+Wv+JsrBID9EQcI7sA7wUZ+p+b0wrkXHz2RXxdNl8p7RrGYZdhOjHa/pbR2+4Eq/Nqdgdj KQbfTAYYeU/8QqsUoWBss44bfTpk/ovYVoWpcSXEbTddAOeDPlWfSTyWSfEzbdxWFiRkDt zttQ/h0QOnROjdPmmlR5pgK0z1m93XrFcZ6xgKPJ/pDvDF5XnuCcAwu6boTi/SWnO9rAJY QIoIPgaSotsc9kG8Cs5bOUbb98CaIrvxRWOQWpcocPP91UyKPCc3WYWw2FRFnCXxDyjyn1 PLV67lOvQREW6gg60Ufw6RN2gSmtvXnFPaAifTCCRk7QDvrVWJwr0sfrjoIisw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1675270122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=raiyxlv8sHUaA8IPt6kfER8nL+6SsvOu2f6BIOePYVo=; b=znd+dWlRTDacP6LEGJnoQfHnk3g/L0RLznG49MtJlWKqoqSw59VzhBqeisyx5qcjBgnHAH +9HNPGYbtYiKShwd3a8+/FAokXhuW5ibgsF6/TiSw7362eRketnvOSkFYZ9kIrXSWM2Ug8 6T5YxT6Vpl4qqcWq+MLf77e4OkKkmsbLTrncJgWkNb6abVYtt3DEOwH7c7cLtlZIR671rF qmTY69xPfLsjGOYSJVOkIFs0ta4Zd1zUjMufy6eicj/uG0Ni18IsuCcnfe5uFM7A1gkixI nFajO4mcmtd9rNs6IGg68T8/5VGo/1N9P86WVYVs3cHw2+wdRXwXKyWqwMu8mw== ARC-Authentication-Results: i=1; rspamd-544f66f495-rm5jj; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Squirrel-Cellar: 3bfe6c1579a19de9_1675270122927_3503329881 X-MC-Loop-Signature: 1675270122927:1328965644 X-MC-Ingress-Time: 1675270122927 Received: from pdx1-sub0-mail-a305.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.24.83 (trex/6.7.1); Wed, 01 Feb 2023 16:48:42 +0000 Received: from [192.168.0.182] (bras-vprn-toroon4834w-lp130-07-174-93-43-36.dsl.bell.ca [174.93.43.36]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305.dreamhost.com (Postfix) with ESMTPSA id 4P6SY539g5zS4; Wed, 1 Feb 2023 08:48:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1675270122; bh=raiyxlv8sHUaA8IPt6kfER8nL+6SsvOu2f6BIOePYVo=; h=Date:Subject:To:Cc:From:Content-Type:Content-Transfer-Encoding; b=PD/WTsyCY9G/+A0uFl+xPxVRUxaaFRBpdUpk+WWzYqcvxp4UZ26v3p9FFEzb4xUl4 ZrslbQ65Xah78Sm+4TP1ylqR7et4xSGo++F02zf4RXslA4P9aWCddhKZ+wfSpeg4/w 97mP4/OYH+uhkrW+SnDLv1ReiyR//zXHExBL39TFaZ4yezLo1LQMG4ya/r9c71GbCK tFNJgCfDodZWV7Cs/AAm0aT9oy7L3neiRDaYOYXwG2XGqxsfso6tG3TZnPgXKw/WoX g16R7DrKHnf9sYi1zaq7SB2HaYUTQmR9v5c8I+TvFP+DCKjP//AQ3Vbd1KAGVu0f5/ ZhAZ2Wk4H2Blw== Message-ID: <371928ba-f81e-c1ef-70d8-2a2e29abc69c@gotplt.org> Date: Wed, 1 Feb 2023 11:48:39 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH 1/2] Handle component_ref to a structre/union field including flexible array member [PR101832] Content-Language: en-US To: Qing Zhao , rguenther@suse.de Cc: gcc-patches@gcc.gnu.org, keescook@chromium.org References: <20230131141140.3610133-1-qing.zhao@oracle.com> <20230131141140.3610133-2-qing.zhao@oracle.com> From: Siddhesh Poyarekar In-Reply-To: <20230131141140.3610133-2-qing.zhao@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3037.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-01-31 09:11, Qing Zhao wrote: > GCC extension accepts the case when a struct with a flexible array member > is embedded into another struct (possibly recursively). > __builtin_object_size should treat such struct as flexible size per > -fstrict-flex-arrays. > > PR tree-optimization/101832 > > gcc/ChangeLog: > > PR tree-optimization/101832 > * tree-object-size.cc (flexible_size_type_p): New function. > (addr_object_size): Handle structure/union type when it has > flexible size. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/101832 > * gcc.dg/builtin-object-size-pr101832-2.c: New test. > * gcc.dg/builtin-object-size-pr101832-3.c: New test. > * gcc.dg/builtin-object-size-pr101832-4.c: New test. > * gcc.dg/builtin-object-size-pr101832.c: New test. > --- > .../gcc.dg/builtin-object-size-pr101832-2.c | 135 ++++++++++++++++++ > .../gcc.dg/builtin-object-size-pr101832-3.c | 135 ++++++++++++++++++ > .../gcc.dg/builtin-object-size-pr101832-4.c | 135 ++++++++++++++++++ > .../gcc.dg/builtin-object-size-pr101832.c | 119 +++++++++++++++ > gcc/tree-object-size.cc | 115 +++++++++++---- > 5 files changed, 611 insertions(+), 28 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832-2.c > create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832-3.c > create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832-4.c > create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c > > diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-2.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-2.c > new file mode 100644 > index 00000000000..f38babc5415 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-2.c > @@ -0,0 +1,135 @@ > +/* PR 101832: > + GCC extension accepts the case when a struct with a flexible array member > + is embedded into another struct (possibly recursively). > + __builtin_object_size will treat such struct as flexible size per > + -fstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-arrays=1" } */ > + > +#include > + > +unsigned n_fails = 0; > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else {\ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + n_fails++; \ I just pushed my testsuite fix, so you could use the macros in gcc.dg/builtin-object-size-common.h instead of accounting this yourself. Also if you use __builtin_printf, you won't have to include stdio.h. Thanks, Sid > + } \ > +} while (0); > + > +struct A { > + int n; > + char data[];/* Content following header */ > +}; > + > +struct B { > + int m; > + struct A a; > +}; > + > +struct C { > + int q; > + struct B b; > +}; > + > +struct A0 { > + int n; > + char data[0];/* Content following header */ > +}; > + > +struct B0 { > + int m; > + struct A0 a; > +}; > + > +struct C0 { > + int q; > + struct B0 b; > +}; > + > +struct A1 { > + int n; > + char data[1];/* Content following header */ > +}; > + > +struct B1 { > + int m; > + struct A1 a; > +}; > + > +struct C1 { > + int q; > + struct B1 b; > +}; > + > +struct An { > + int n; > + char data[8];/* Content following header */ > +}; > + > +struct Bn { > + int m; > + struct An a; > +}; > + > +struct Cn { > + int q; > + struct Bn b; > +}; > + > +volatile void *magic1, *magic2; > + > +int main(int argc, char *argv[]) > +{ > + struct B *outer; > + struct C *outest; > + > + /* Make sure optimization can't find some other object size. */ > + outer = (void *)magic1; > + outest = (void *)magic2; > + > + expect(__builtin_object_size(&outer->a, 1), -1); > + expect(__builtin_object_size(&outest->b, 1), -1); > + expect(__builtin_object_size(&outest->b.a, 1), -1); > + > + struct B0 *outer0; > + struct C0 *outest0; > + > + /* Make sure optimization can't find some other object size. */ > + outer0 = (void *)magic1; > + outest0 = (void *)magic2; > + > + expect(__builtin_object_size(&outer0->a, 1), -1); > + expect(__builtin_object_size(&outest0->b, 1), -1); > + expect(__builtin_object_size(&outest0->b.a, 1), -1); > + > + struct B1 *outer1; > + struct C1 *outest1; > + > + /* Make sure optimization can't find some other object size. */ > + outer1 = (void *)magic1; > + outest1 = (void *)magic2; > + > + expect(__builtin_object_size(&outer1->a, 1), -1); > + expect(__builtin_object_size(&outest1->b, 1), -1); > + expect(__builtin_object_size(&outest1->b.a, 1), -1); > + > + struct Bn *outern; > + struct Cn *outestn; > + > + /* Make sure optimization can't find some other object size. */ > + outern = (void *)magic1; > + outestn = (void *)magic2; > + > + expect(__builtin_object_size(&outern->a, 1), sizeof(outern->a)); > + expect(__builtin_object_size(&outestn->b, 1), sizeof(outestn->b)); > + expect(__builtin_object_size(&outestn->b.a, 1), sizeof(outestn->b.a)); > + > + if (n_fails > 0) > + __builtin_abort (); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-3.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-3.c > new file mode 100644 > index 00000000000..aaae99b8d67 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-3.c > @@ -0,0 +1,135 @@ > +/* PR 101832: > + GCC extension accepts the case when a struct with a flexible array member > + is embedded into another struct (possibly recursively). > + __builtin_object_size will treat such struct as flexible size per > + -fstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-arrays=2" } */ > + > +#include > + > +unsigned n_fails = 0; > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else {\ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + n_fails++; \ > + } \ > +} while (0); > + > +struct A { > + int n; > + char data[];/* Content following header */ > +}; > + > +struct B { > + int m; > + struct A a; > +}; > + > +struct C { > + int q; > + struct B b; > +}; > + > +struct A0 { > + int n; > + char data[0];/* Content following header */ > +}; > + > +struct B0 { > + int m; > + struct A0 a; > +}; > + > +struct C0 { > + int q; > + struct B0 b; > +}; > + > +struct A1 { > + int n; > + char data[1];/* Content following header */ > +}; > + > +struct B1 { > + int m; > + struct A1 a; > +}; > + > +struct C1 { > + int q; > + struct B1 b; > +}; > + > +struct An { > + int n; > + char data[8];/* Content following header */ > +}; > + > +struct Bn { > + int m; > + struct An a; > +}; > + > +struct Cn { > + int q; > + struct Bn b; > +}; > + > +volatile void *magic1, *magic2; > + > +int main(int argc, char *argv[]) > +{ > + struct B *outer; > + struct C *outest; > + > + /* Make sure optimization can't find some other object size. */ > + outer = (void *)magic1; > + outest = (void *)magic2; > + > + expect(__builtin_object_size(&outer->a, 1), -1); > + expect(__builtin_object_size(&outest->b, 1), -1); > + expect(__builtin_object_size(&outest->b.a, 1), -1); > + > + struct B0 *outer0; > + struct C0 *outest0; > + > + /* Make sure optimization can't find some other object size. */ > + outer0 = (void *)magic1; > + outest0 = (void *)magic2; > + > + expect(__builtin_object_size(&outer0->a, 1), -1); > + expect(__builtin_object_size(&outest0->b, 1), -1); > + expect(__builtin_object_size(&outest0->b.a, 1), -1); > + > + struct B1 *outer1; > + struct C1 *outest1; > + > + /* Make sure optimization can't find some other object size. */ > + outer1 = (void *)magic1; > + outest1 = (void *)magic2; > + > + expect(__builtin_object_size(&outer1->a, 1), sizeof(outer1->a)); > + expect(__builtin_object_size(&outest1->b, 1), sizeof(outest1->b)); > + expect(__builtin_object_size(&outest1->b.a, 1), sizeof(outest1->b.a)); > + > + struct Bn *outern; > + struct Cn *outestn; > + > + /* Make sure optimization can't find some other object size. */ > + outern = (void *)magic1; > + outestn = (void *)magic2; > + > + expect(__builtin_object_size(&outern->a, 1), sizeof(outern->a)); > + expect(__builtin_object_size(&outestn->b, 1), sizeof(outestn->b)); > + expect(__builtin_object_size(&outestn->b.a, 1), sizeof(outestn->b.a)); > + > + if (n_fails > 0) > + __builtin_abort (); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-4.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-4.c > new file mode 100644 > index 00000000000..424264e2acd > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832-4.c > @@ -0,0 +1,135 @@ > +/* PR 101832: > + GCC extension accepts the case when a struct with a flexible array member > + is embedded into another struct (possibly recursively). > + __builtin_object_size will treat such struct as flexible size per > + -fstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2 -fstrict-flex-arrays=3" } */ > + > +#include > + > +unsigned n_fails = 0; > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else {\ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + n_fails++; \ > + } \ > +} while (0); > + > +struct A { > + int n; > + char data[];/* Content following header */ > +}; > + > +struct B { > + int m; > + struct A a; > +}; > + > +struct C { > + int q; > + struct B b; > +}; > + > +struct A0 { > + int n; > + char data[0];/* Content following header */ > +}; > + > +struct B0 { > + int m; > + struct A0 a; > +}; > + > +struct C0 { > + int q; > + struct B0 b; > +}; > + > +struct A1 { > + int n; > + char data[1];/* Content following header */ > +}; > + > +struct B1 { > + int m; > + struct A1 a; > +}; > + > +struct C1 { > + int q; > + struct B1 b; > +}; > + > +struct An { > + int n; > + char data[8];/* Content following header */ > +}; > + > +struct Bn { > + int m; > + struct An a; > +}; > + > +struct Cn { > + int q; > + struct Bn b; > +}; > + > +volatile void *magic1, *magic2; > + > +int main(int argc, char *argv[]) > +{ > + struct B *outer; > + struct C *outest; > + > + /* Make sure optimization can't find some other object size. */ > + outer = (void *)magic1; > + outest = (void *)magic2; > + > + expect(__builtin_object_size(&outer->a, 1), -1); > + expect(__builtin_object_size(&outest->b, 1), -1); > + expect(__builtin_object_size(&outest->b.a, 1), -1); > + > + struct B0 *outer0; > + struct C0 *outest0; > + > + /* Make sure optimization can't find some other object size. */ > + outer0 = (void *)magic1; > + outest0 = (void *)magic2; > + > + expect(__builtin_object_size(&outer0->a, 1), sizeof(outer0->a)); > + expect(__builtin_object_size(&outest0->b, 1), sizeof(outest0->b)); > + expect(__builtin_object_size(&outest0->b.a, 1), sizeof(outest0->b.a)); > + > + struct B1 *outer1; > + struct C1 *outest1; > + > + /* Make sure optimization can't find some other object size. */ > + outer1 = (void *)magic1; > + outest1 = (void *)magic2; > + > + expect(__builtin_object_size(&outer1->a, 1), sizeof(outer1->a)); > + expect(__builtin_object_size(&outest1->b, 1), sizeof(outest1->b)); > + expect(__builtin_object_size(&outest1->b.a, 1), sizeof(outest1->b.a)); > + > + struct Bn *outern; > + struct Cn *outestn; > + > + /* Make sure optimization can't find some other object size. */ > + outern = (void *)magic1; > + outestn = (void *)magic2; > + > + expect(__builtin_object_size(&outern->a, 1), sizeof(outern->a)); > + expect(__builtin_object_size(&outestn->b, 1), sizeof(outestn->b)); > + expect(__builtin_object_size(&outestn->b.a, 1), sizeof(outestn->b.a)); > + > + if (n_fails > 0) > + __builtin_abort (); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c > new file mode 100644 > index 00000000000..8ed6980edf0 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c > @@ -0,0 +1,119 @@ > +/* PR 101832: > + GCC extension accepts the case when a struct with a flexible array member > + is embedded into another struct (possibly recursively). > + __builtin_object_size will treat such struct as flexible size per > + -fstrict-flex-arrays. */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +#include > + > +unsigned n_fails = 0; > + > +#define expect(p, _v) do { \ > + size_t v = _v; \ > + if (p == v) \ > + printf("ok: %s == %zd\n", #p, p); \ > + else {\ > + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ > + n_fails++; \ > + } \ > +} while (0); > + > +struct A { > + int n; > + char data[];/* Content following header */ > +}; > + > +struct B { > + int m; > + struct A a; > +}; > + > +struct C { > + int q; > + struct B b; > +}; > + > +struct A0 { > + int n; > + char data[0];/* Content following header */ > +}; > + > +struct B0 { > + int m; > + struct A0 a; > +}; > + > +struct C0 { > + int q; > + struct B0 b; > +}; > + > +struct A1 { > + int n; > + char data[1];/* Content following header */ > +}; > + > +struct B1 { > + int m; > + struct A1 a; > +}; > + > +struct C1 { > + int q; > + struct B1 b; > +}; > + > +struct An { > + int n; > + char data[8];/* Content following header */ > +}; > + > +struct Bn { > + int m; > + struct An a; > +}; > + > +struct Cn { > + int q; > + struct Bn b; > +}; > + > +volatile void *magic1, *magic2; > + > +int main(int argc, char *argv[]) > +{ > + struct B *outer = (void *)magic1; > + struct C *outest = (void *)magic2; > + > + expect(__builtin_object_size(&outer->a, 1), -1); > + expect(__builtin_object_size(&outest->b, 1), -1); > + expect(__builtin_object_size(&outest->b.a, 1), -1); > + > + struct B0 *outer0 = (void *)magic1; > + struct C0 *outest0 = (void *)magic2; > + > + expect(__builtin_object_size(&outer0->a, 1), -1); > + expect(__builtin_object_size(&outest0->b, 1), -1); > + expect(__builtin_object_size(&outest0->b.a, 1), -1); > + > + struct B1 *outer1 = (void *)magic1; > + struct C1 *outest1 = (void *)magic2; > + > + expect(__builtin_object_size(&outer1->a, 1), -1); > + expect(__builtin_object_size(&outest1->b, 1), -1); > + expect(__builtin_object_size(&outest1->b.a, 1), -1); > + > + struct Bn *outern = (void *)magic1; > + struct Cn *outestn = (void *)magic2; > + > + expect(__builtin_object_size(&outern->a, 1), -1); > + expect(__builtin_object_size(&outestn->b, 1), -1); > + expect(__builtin_object_size(&outestn->b.a, 1), -1); > + > + if (n_fails > 0) > + __builtin_abort (); > + > + return 0; > +} > diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc > index 9a936a91983..56b78ca2a8c 100644 > --- a/gcc/tree-object-size.cc > +++ b/gcc/tree-object-size.cc > @@ -500,6 +500,42 @@ decl_init_size (tree decl, bool min) > return size; > } > > +/* Determine whether TYPE is a structure with a flexible array member > + per -fstrict-flex-array or a union containing such a structure > + (possibly recursively). */ > +static bool > +flexible_size_type_p (const_tree type) > +{ > + tree x = NULL_TREE; > + tree last = NULL_TREE; > + switch (TREE_CODE (type)) > + { > + case RECORD_TYPE: > + for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x)) > + if (TREE_CODE (x) == FIELD_DECL) > + last = x; > + if (last == NULL_TREE) > + return false; > + if (TREE_CODE (TREE_TYPE (last)) == ARRAY_TYPE > + && !DECL_NOT_FLEXARRAY (last)) > + return true; > + else if (TREE_CODE (TREE_TYPE (last)) == RECORD_TYPE > + || TREE_CODE (TREE_TYPE (last)) == UNION_TYPE) > + return flexible_size_type_p (TREE_TYPE (last)); > + return false; > + case UNION_TYPE: > + for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x)) > + { > + if (TREE_CODE (x) == FIELD_DECL > + && flexible_array_type_p (TREE_TYPE (x))) > + return true; > + } > + return false; > + default: > + return false; > + } > +} > + > /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR. > OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. > If unknown, return size_unknown (object_size_type). */ > @@ -633,45 +669,68 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > v = NULL_TREE; > break; > case COMPONENT_REF: > - if (TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) > + /* When the ref is not to an array, a record or a union, it > + will not have flexible size, compute the object size > + directly. */ > + if ((TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) > + && (TREE_CODE (TREE_TYPE (v)) != RECORD_TYPE) > + && (TREE_CODE (TREE_TYPE (v)) != UNION_TYPE)) > { > v = NULL_TREE; > break; > } > - is_flexible_array_mem_ref = array_ref_flexible_size_p (v); > - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > - != UNION_TYPE > - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > - != QUAL_UNION_TYPE) > - break; > - else > - v = TREE_OPERAND (v, 0); > - if (TREE_CODE (v) == COMPONENT_REF > - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > - == RECORD_TYPE) > + /* if the record or union does not have flexible size > + compute the object size directly. */ > + if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE > + || TREE_CODE (TREE_TYPE (v)) == UNION_TYPE) > { > - /* compute object size only if v is not a > - flexible array member. */ > - if (!is_flexible_array_mem_ref) > + if (!flexible_size_type_p (TREE_TYPE (v))) > { > v = NULL_TREE; > break; > } > - v = TREE_OPERAND (v, 0); > + else > + v = TREE_OPERAND (v, 0); > } > - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > - != UNION_TYPE > - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > - != QUAL_UNION_TYPE) > - break; > - else > - v = TREE_OPERAND (v, 0); > - if (v != pt_var) > - v = NULL_TREE; > else > - v = pt_var; > + { > + /* Now the ref is to an array type. */ > + is_flexible_array_mem_ref > + = array_ref_flexible_size_p (v); > + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > + != UNION_TYPE > + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > + != QUAL_UNION_TYPE) > + break; > + else > + v = TREE_OPERAND (v, 0); > + if (TREE_CODE (v) == COMPONENT_REF > + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > + == RECORD_TYPE) > + { > + /* compute object size only if v is not a > + flexible array member. */ > + if (!is_flexible_array_mem_ref) > + { > + v = NULL_TREE; > + break; > + } > + v = TREE_OPERAND (v, 0); > + } > + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) > + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > + != UNION_TYPE > + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) > + != QUAL_UNION_TYPE) > + break; > + else > + v = TREE_OPERAND (v, 0); > + if (v != pt_var) > + v = NULL_TREE; > + else > + v = pt_var; > + } > break; > default: > v = pt_var;