From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 55D5B3857352 for ; Fri, 3 Mar 2023 00:03:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 55D5B3857352 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 322K49F4008976; Fri, 3 Mar 2023 00:03:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=corp-2022-7-12; bh=2yU1huizLzlyYrb50/qU6cSYNdh/H/Bn2vlindcwhv8=; b=BouzHwtsR091hNU+KBPDtXTIFTv2+VHPw3kRo0EBXti6YHLfo80wgRg5Dfc3stRJXw67 T4eWw9Afc+0j10/2bXvMwBHVdsKdPhOI4hxolrbfiMLClYhMPpEM4tD72WYdLd8ZPkKR YoXEdGaEnvMrzJB3x60m3okLDGkMYz3huszsdtrrMf1W2FY6xLWwPJfnWumSOSzv2Xml lRwx9ZHnTtwKCiMoSdCbdYwTPNtow1VfwIBvifda1v3589QC6z74cNcEBtm5LmrvJ874 GFQL7+/PX+saziVlPZ8impugd8fQNtceNK+1LF/0nP5ASqyC3do32TcO7L/bg2Zksm60 7w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nyba2db1c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 03 Mar 2023 00:03:19 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 322MZoht000598; Fri, 3 Mar 2023 00:03:18 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ny8sasmx6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 03 Mar 2023 00:03:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W4bh8MCvcnQVOPllUn2T/HF8amEinhtRhuYj72xtykqGQ06G+IgQL4vapCYiq3f9imA/qnWhooYzj5LqdAr/VQqZ1vTxn6KAa4sEDH0qpHnku33RdoPUosYfJ/InoEX4fI+F0ORaQW9njtzH+Ky0V8uUlsfZNw754BwI4HaoyjkUtzQAvDZ21A2YjsR9PbG6fmmsK9rQxzjuKpjbHFio43MKp5ZB+vQqjdm/vHUZik/YRvSExqE/mxwBols3w8Ymrqn17wPDpmzgb3mreGkByb+h/BecdG39FcczSwO7M0V733WCTEo+v7LNSUI+WyOMoHeTN6HLJ98/l33yLU2edQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2yU1huizLzlyYrb50/qU6cSYNdh/H/Bn2vlindcwhv8=; b=iYFIynyxZgXxIc5hzyjiwHuE17j+K/cDgY+VOHHZ868kbX4kyAXch6U8FklKzbYhIA/PVfulgqHrxyo5atz7UGWBIpBWBbS5EFhZFK67DyXnuEUje3NyEzFT8ZrhrxadbQDmM+BjTnIeB48+sg/3cRxTIB4eEAeAaZ6dSUvkqnoRysZ62FHHzuyqI4ZaXCPErDfYYPaim/YE6UtfeBOd/VB//RhTHLXFH6y+FYXzaLC9SSSUMZemzI4MD47dLWR4eXonrBtghGg2CJNtJn3UL4VcY0OmjgBmg3dNc5pcOYhICW3lvTcqP/9+rhAdtS1c0o70qjyCw5nnBCIRlj6jPg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2yU1huizLzlyYrb50/qU6cSYNdh/H/Bn2vlindcwhv8=; b=P9IldBnbMFCm8vlaQEkBXd4zliA3mgrfR+78dDk1QimaXjkUK+L2Ey+TndompIUMDezVrU2FiMRBRzLxkLONbws0+/ZIh1L7qn2ulS7wK81rBB75lAIQi/Ue1Po2yVUySG+p40sT1oTcHKkXxV6/7uxHfnyPiw4IvjM+obhT9yI= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by DS0PR10MB7511.namprd10.prod.outlook.com (2603:10b6:8:164::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.18; Fri, 3 Mar 2023 00:03:16 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%6]) with mapi id 15.20.6156.018; Fri, 3 Mar 2023 00:03:16 +0000 From: Qing Zhao To: Joseph Myers , Richard Biener CC: "siddhesh@gotplt.org" , "keescook@chromium.org" , "gcc-patches@gcc.gnu.org" Subject: Re: [V4][PATCH 2/2] Update documentation to clarify a GCC extension Thread-Topic: [V4][PATCH 2/2] Update documentation to clarify a GCC extension Thread-Index: AQHZSH7CijhbeEp2qUeX/bcpl7xP4q7oNh4A Date: Fri, 3 Mar 2023 00:03:16 +0000 Message-ID: References: <20230224183505.4112295-1-qing.zhao@oracle.com> <20230224183505.4112295-3-qing.zhao@oracle.com> In-Reply-To: <20230224183505.4112295-3-qing.zhao@oracle.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3696.120.41.1.2) x-ms-publictraffictype: Email x-ms-traffictypediagnostic: CH2PR10MB4344:EE_|DS0PR10MB7511:EE_ x-ms-office365-filtering-correlation-id: 2047ca35-9837-4283-6338-08db1b7ab089 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: YCt+Fw4A2QWfrcxPFNns4aZBS6tdbPwDaWrX63aNcys74dQV561Oa7PTTnA0Y5nIkF2V+o4p9I0A8bWtVYNLfbXIf9dbXiP+4zU2Y7pi8mjuNhTlDEgz65nsMLSOsef28TVqy70Lu4U1zU8bIXU1vh4LybrWWdmKGKo6EowkB6rJryRRiKsQhuY1VlDftMkYxV5xFQOJOWqtZnBuFqzrAy+KAzOOpBUHS+EYFOCUi21rFyPf2j4+imIPjJohEE8oSV6RH1dUOZHJ3+Vf0k+Wl4XkVfwUUtiQRFz7fsuRRGosL+bfNzlZ1492Fv+1Laki7o5OP/z6xnoqfzbPp5FSjNbQ/3KSFqJ0mXFAwj+ZM7zKL+OdFQaC267M6FpaADdDz00sIM50EQAnpAaasVQl3/ji/oYqmERj1JvQHsutjbmWh0txxyUdEMdG0DqXk8Fqo4YMrzmThiO9q9ScilhVYFLP01HkojJMj+RiYIlHwwlTVNK3zie+3JxRUGxoMG4aVFp1TzGvPSgsF9HvUXXYHlRFDdYbFYYCe85zREsk2YLQtwjE6MM/88T3bNQSFLpEl89Tg5AUQP5y0J5lrku1L5982zdnoH1zpSXZWoGFyr9g1+kPvNqfJJoQBe9FGqJcOqUgSwwR/RKnqW8ixOpmpTsTwPKb+LeWeMlUGvdYmfo0WWsrzVWd3QzjZIsseLaSpuD37sCRRA2cgp5PdOGgd85/OGmihbqyGEqchwES8A0= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR10MB4344.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(39860400002)(366004)(136003)(396003)(346002)(376002)(451199018)(122000001)(86362001)(33656002)(36756003)(38070700005)(110136005)(54906003)(38100700002)(64756008)(8676002)(76116006)(66446008)(66946007)(66476007)(91956017)(316002)(66556008)(4326008)(2616005)(478600001)(6486002)(6506007)(26005)(71200400001)(53546011)(186003)(6512007)(44832011)(5660300002)(2906002)(84970400001)(83380400001)(41300700001)(8936002)(45980500001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?DT9CnFiVJd1c4TGTFXvggK8Tl0xjoEZTvSpeTHZivp8Qc/2jfy0OewemHVCX?= =?us-ascii?Q?MmTzOtVNaJzZYagMgogFAKM0sZA4S3U1boPxruaDBsSlUhYGldCXVZBvkl1l?= =?us-ascii?Q?Pnlx8MWwl6gV8RPYpv5cm9nLnsKxRSENOK0yCd85DB0lsrV6Am3Amy9WQ1WL?= =?us-ascii?Q?Mq6TbQQs+wwP2Nz/KXDZaXnNVgluxWn4T2yWser5zQIXIqHqMBFMNVW5/5az?= =?us-ascii?Q?Y3SguS8ZT/XzD7ETKvmCBh1BStaCJM0FJWda52HYTSBNQOStXZAOYQZMYl/R?= =?us-ascii?Q?ecJ6NKSAnlfGlF7r5hNnxLerEGEbqM2YiT8Ne5QFfr2CuomgQae4t0Mp8y0t?= =?us-ascii?Q?QI+zX0JY6ni+Jf/ljjYpU3bQYHBla9TSyxBxNS0Yp9i9OSxTyX6lHnxRrosl?= =?us-ascii?Q?6CpJAhaqAASuU7I2QZLfvaBFj+L1aSrUyfM6tQos2DgRr2nmktJyPAoelgia?= =?us-ascii?Q?IUs4HDUoTbiXIrTjbH8ZugVnZ9m1n7RxaJ+i5EjxViUBjSpciP5M+NdCSeQh?= =?us-ascii?Q?h256rodLQ8NRlKN/4rdBvSn++UmmEUpcKYR7GuRwCpHuQSlrmb/NltENTWMd?= =?us-ascii?Q?3dFlLSwb9o5myLerEZYv3RibttjWTWEyvk9bKhuQVWWuwHLkIu61mr/XEp83?= =?us-ascii?Q?dsee+HHhwPJ9yKYZUx6jG8uRr5ERgy9dKyCNj3e4suXLqp7Ge07/IC01abFN?= =?us-ascii?Q?OdBuWDtsHrUGxh4kH5r6Wmq89FBHj1yIe2U9VmysZCVTuGVepZ7o/gBOrf5T?= =?us-ascii?Q?96lA15N+RJzQu3lYbKMziPBnoHm9w6UebjrFSVUdoomTv+GOMBByjPPwVrlW?= =?us-ascii?Q?j6bmOlEMfRwUDuhezAtDcYu0Ih5D7MCCxgeMrtSvRXv4YX1LBCM6Mvky9dR7?= =?us-ascii?Q?UH9eGDmwtkmw99swssoZYvYmPir5cfQJviYQRtnz4wHG2PVLQ90lJLAtr0S4?= =?us-ascii?Q?xa+vd2pPvj3Tv7k999K1Q3rWeFbjBEVyLAVFcJBUlebABka0AYLRbo7z2LIE?= =?us-ascii?Q?dxEv+iUisOls3QzEhORboCphBFIO1RBdlhC2ua/S2jYGiJImnaiF3Ll8+cCt?= =?us-ascii?Q?B5fnulC8D27dylElGaXRrPvYkHR+OHhy2ZCyFe6KDNGaVCrtRFI6qTMFa4Bo?= =?us-ascii?Q?Nlx5MydLtBp0w0N/ECMNSJQC1d0fdGBxEg8GPgvk0ymU6jR83owCaYfmGAvM?= =?us-ascii?Q?Kk/+RJWCrDqcDUo66JkzxykCA8HuT8yx4DVgJjLwi1PnHBPeUP2qztf44igE?= =?us-ascii?Q?43hZeMaGqorHkHLUn5qugul3UXbSgLl9CkKocUQoH014undPSiKB556M9UOd?= =?us-ascii?Q?ftQ/sZK/2nz39MnzP6rjZYWrF2epPwrCYVOEDU6oUQ65ZP8mUTFB2pxv90Ud?= =?us-ascii?Q?LXgLYJbczIeDMbPiM+BwKOLb7zV9Aw2xP8sfh3U9d1RPn6FhaF9/VKnl5Mk1?= =?us-ascii?Q?02QlI0xanfhP9ekeNEhd8hbCwkK2OVmgVRyGyFPlvLu0K9NzZzHAfld6vQ6X?= =?us-ascii?Q?riAlCea6Uj5UT1vTteRwndLk7cRa1wbNkd4zFkFaeXMwG6IwvapX0oz5jAIE?= =?us-ascii?Q?4wo7h221JxjQmGmq0c2g9HJSERD8Fs1hILNWgDpXGyLE6HJFLdzL2Scl+IMH?= =?us-ascii?Q?Zw=3D=3D?= Content-Type: text/plain; charset="us-ascii" Content-ID: <6DC3E0DE5912734E985A60CC699BFA15@namprd10.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: tJGaPaT3NHsmr45MK9R6HMRNs5RwNSsSBTBBtTkS+MDG2RV8PNBbbnpUNI6usFeM7wl2S0+CFnxT0pAwKsj7n2RoUJukcdiSJ42IBLmW2s3/tb8vu1hx2ZnHW+rFz0o7W7AF5AWdPj6CMqFOHL+RVwmFCEo8E6lmZmjBrE0Ydzp20b/iI5LBnJpy30apTLLgbVYhqu8UsCgXyJhGyRRh06Z+18nPge1Kv1wvqf2mF4Z2GEHF6N37ne+NQ6a470byi4l5Z1a1jq8Y3DZA6N9EBp81I/2jvWz/m1d6d96t+YSHU3T+TnTtVtfPsXQJu6cGQ8Jh0EpCcVq2ONi71Abhe6g6q3LNER3podAvNwiyujdnTFwSgL9ui160ZH685DEoLnAXYPgEmKnKylkkKgMtSNXfoKH0l7SKlG7PUNZM3dQGhU0CQs8Pkka8T8d4JdQOFWq0mybTLp30wV2HRi0J8iPBl51wySE8xZ0XZNnLXHwtwtQZiJ5NJZi3qNiG1evJDUwy1oOCx5uj6tBl7ysnsmV9TYAx5DhVi1aNSB+m5wz9ZoA5mbqmXq5Bj/lYFvjt2Dri1etRQke7NuB0tYex60YniT5oxD3m/sJLePLe/z+O5syYo1tmRBmZKXLQE/AeNYvNpTj6VtZs36AKtSZdAvYpw04eS+1IueK2nm/umU4xl1lYJYdXqZGcCv96onxw4+0X/u3C6rJEZjpRDF77uL1iXkk/CWxBkK1cp6nnjkgNSpIgx+TPKKTooD9qfXAsdOicBxSD4SFQUVNPx70fpBw5v1Vn8IhCezHzfLMw4LtVxFajlUremTfkkNEnfOt+cyas02JbfWAIu998lKC1ZIaqy9nsyCD86lJ3QXQ+us1JpvGfNSX9tDyeBxbj0kXW X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2047ca35-9837-4283-6338-08db1b7ab089 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2023 00:03:16.4498 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: q0VO33xdqZyj2ADoiLxyeRUzJJKnH/kQkRSP8Xlq0iqBaToGgnPXIXu7rG4tSlIeEFvobQjJ3oE9oE8UVOopGg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB7511 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-02_15,2023-03-02_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxlogscore=999 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2303020207 X-Proofpoint-GUID: er5yRr7-5TJr4uMYvPQyga3LgN2MMCF_ X-Proofpoint-ORIG-GUID: er5yRr7-5TJr4uMYvPQyga3LgN2MMCF_ X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: Ping. Qing > On Feb 24, 2023, at 1:35 PM, Qing Zhao wrote: >=20 > on a structure with a C99 flexible array member being nested in > another structure. >=20 > "GCC extension accepts a structure containing an ISO C99 "flexible array > member", or a union containing such a structure (possibly recursively) > to be a member of a structure. >=20 > There are two situations: >=20 > * The structure with a C99 flexible array member is the last field of > another structure, for example: >=20 > struct flex { int length; char data[]; }; > union union_flex { int others; struct flex f; }; >=20 > struct out_flex_struct { int m; struct flex flex_data; }; > struct out_flex_union { int n; union union_flex flex_data; }; >=20 > In the above, both 'out_flex_struct.flex_data.data[]' and > 'out_flex_union.flex_data.f.data[]' are considered as flexible > arrays too. >=20 > * The structure with a C99 flexible array member is the middle field > of another structure, for example: >=20 > struct flex { int length; char data[]; }; >=20 > struct mid_flex { int m; struct flex flex_data; int n; }; >=20 > In the above, 'mid_flex.flex_data.data[]' is allowed to be extended > flexibly to the padding. E.g, up to 4 elements. >=20 > However, relying on space in struct padding is a bad programming > practice, compilers do not handle such extension consistently, Any > code relying on this behavior should be modified to ensure that > flexible array members only end up at the ends of structures. >=20 > Please use warning option '-Wgnu-variable-sized-type-not-at-end' to > identify all such cases in the source code and modify them. This > extension will be deprecated from gcc in the next release. > " >=20 > gcc/c-family/ChangeLog: >=20 > * c.opt: New option -Wgnu-variable-sized-type-not-at-end. >=20 > gcc/c/ChangeLog: >=20 > * c-decl.cc (finish_struct): Issue warnings for new option. >=20 > gcc/ChangeLog: >=20 > * doc/extend.texi: Document GCC extension on a structure containing > a flexible array member to be a member of another structure. >=20 > gcc/testsuite/ChangeLog: >=20 > * gcc.dg/variable-sized-type-flex-array.c: New test. > --- > gcc/c-family/c.opt | 5 ++ > gcc/c/c-decl.cc | 7 +++ > gcc/doc/extend.texi | 48 ++++++++++++++++++- > .../gcc.dg/variable-sized-type-flex-array.c | 31 ++++++++++++ > 4 files changed, 90 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c >=20 > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > index 3333cddeece..660ac07f3d4 100644 > --- a/gcc/c-family/c.opt > +++ b/gcc/c-family/c.opt > @@ -737,6 +737,11 @@ Wformat-truncation=3D > C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trun= c) Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=3D, warn_format >=3D= 1, 0) IntegerRange(0, 2) > Warn about calls to snprintf and similar functions that truncate output. >=20 > +Wgnu-variable-sized-type-not-at-end > +C C++ Var(warn_variable_sized_type_not_at_end) Warning > +Warn about structures or unions with C99 flexible array members are not > +at the end of a structure. > + > Wif-not-aligned > C ObjC C++ ObjC++ Var(warn_if_not_aligned) Init(1) Warning > Warn when the field in a struct is not aligned. > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc > index f589a2f5192..c5b54f07965 100644 > --- a/gcc/c/c-decl.cc > +++ b/gcc/c/c-decl.cc > @@ -9296,6 +9296,13 @@ finish_struct (location_t loc, tree t, tree fieldl= ist, tree attributes, > && is_last_field) > TYPE_INCLUDE_FLEXARRAY (t) =3D true; >=20 > + if (warn_variable_sized_type_not_at_end > + && !is_last_field > + && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x))) > + warning_at (DECL_SOURCE_LOCATION (x), > + OPT_Wgnu_variable_sized_type_not_at_end, > + "variable sized type not at the end of a struct"); > + > if (DECL_NAME (x) > || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) > saw_named_field =3D true; > diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi > index c1122916255..e278148c332 100644 > --- a/gcc/doc/extend.texi > +++ b/gcc/doc/extend.texi > @@ -1748,7 +1748,53 @@ Flexible array members may only appear as the last= member of a > A structure containing a flexible array member, or a union containing > such a structure (possibly recursively), may not be a member of a > structure or an element of an array. (However, these uses are > -permitted by GCC as extensions.) > +permitted by GCC as extensions, see details below.) > +@end itemize > + > +GCC extension accepts a structure containing an ISO C99 @dfn{flexible ar= ray > +member}, or a union containing such a structure (possibly recursively) > +to be a member of a structure. > + > +There are two situations: > + > +@itemize @bullet > +@item > +The structure with a C99 flexible array member is the last field of anot= her > +structure, for example: > + > +@smallexample > +struct flex @{ int length; char data[]; @}; > +union union_flex @{ int others; struct flex f; @}; > + > +struct out_flex_struct @{ int m; struct flex flex_data; @}; > +struct out_flex_union @{ int n; union union_flex flex_data; @}; > +@end smallexample > + > +In the above, both @code{out_flex_struct.flex_data.data[]} and > +@code{out_flex_union.flex_data.f.data[]} are considered as flexible arra= ys too. > + > + > +@item > +The structure with a C99 flexible array member is the middle field of an= other > +structure, for example: > + > +@smallexample > +struct flex @{ int length; char data[]; @}; > + > +struct mid_flex @{ int m; struct flex flex_data; int n; @}; > +@end smallexample > + > +In the above, @code{mid_flex.flex_data.data[]} is allowed to be extended > +flexibly to the padding. E.g, up to 4 elements. > + > +However, relying on space in struct padding is a bad programming practic= e, > +compilers do not handle such extension consistently, Any code relying on > +this behavior should be modified to ensure that flexible array members > +only end up at the ends of structures. > + > +Please use warning option @option{-Wgnu-variable-sized-type-not-at-end}= to > +identify all such cases in the source code and modify them. This extens= ion > +will be deprecated from gcc in the next release. > @end itemize >=20 > Non-empty initialization of zero-length > diff --git a/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c b/gcc/= testsuite/gcc.dg/variable-sized-type-flex-array.c > new file mode 100644 > index 00000000000..e3f65c5ed07 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c > @@ -0,0 +1,31 @@ > +/* Test for -Wgnu-variable-sized-type-not-at-end on structure/union with= =20 > + C99 flexible array members being embedded into another structure. */ > +/* { dg-do compile } */ > +/* { dg-options "-Wgnu-variable-sized-type-not-at-end" } */ > + > +struct flex { int n; int data[]; }; > +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "va= riable sized type not at the end of a struct" } */ > +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning = "variable sized type not at the end of a struct" } */ > +/* since the warning has been issued for out_flex_mid, no need to > + issue warning again when it is included in another structure/union. = */ > +struct outer_flex_mid { struct out_flex_mid out_flex_data; int p; }; /* = { dg-bogus "variable sized type not at the end of a struct" } */ > +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus = "variable sized type not at the end of a struct" } */ > + > + > +struct flex0 { int n; int data[0]; }; > +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "= variable sized type not at the end of a struct" } */ > +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus = "variable sized type not at the end of a struct" } */ > +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /= * { dg-bogus "variable sized type not at the end of a struct" } */ > +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogu= s "variable sized type not at the end of a struct" } */ > + > +struct flex1 { int n; int data[1]; }; > +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "= variable sized type not at the end of a struct" } */ > +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "= variable sized type not at the end of a struct" } */=20 > +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /= * { dg-bogus "variable sized type not at the end of a struct" } */ > +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogu= s "variable sized type not at the end of a struct" } */ > + > +struct flexn { int n; int data[8]; };=20 > +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "= variable sized type not at the end of a struct" } */ > +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"v= ariable sized type not at the end of a struct" } */=20 > +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /= * { dg-bogus"variable sized type not at the end of a struct" } */ > +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogu= s "variable sized type not at the end of a struct" } */ > --=20 > 2.31.1 >=20