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 9673C3858416 for ; Thu, 25 May 2023 01:23:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9673C3858416 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 34P19O9M030851; Thu, 25 May 2023 01:23:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-03-30; bh=6bbxc4c1jouaMm52h8ANLMdugUP0IkQ0//pJ6002zB4=; b=zGbSyTjCHfGUrX/uL1GiP64Kd53LpesY7WFcO2Kuwz3eQAMvX5jfUQoLESg2+ncPk6Ur lRVr019lctniNHoGQBVM9BlR9tFcvVyq+tPKb585rJCoA0x5ntaQbVHrknHaJxOBbOFm FEoycRNkQLjmdO+mp8LiXvzhqobVb0Xklb3OaYmqlwviClALgNvf3bPh3LkptMhQamD3 /NFR+6IGoX21w+yidQNln+A6DWFzncEQapEmF1Eaklcox7tiRWggLuDEU0j4ydrOAuK/ 63vtoiMZX2eOMLnZvDbm67TlETTaK1L8eshsMZNFv4hchHJlKYtDP5uORdH8aMn1fZPy Pg== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qswrt00hw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 01:23:28 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34P0JO1e023798; Thu, 25 May 2023 01:23:28 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2102.outbound.protection.outlook.com [104.47.55.102]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk8wgt74-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 01:23:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k53RQgoF0IFFfdWxOJdI26hzP8ViphwpvsKwI5/Enn+5psKzog/oNHwTcH5Quje4D/UcsOUVhQ6DZP5yQqnZ2E5j+piddVggbGeW1wigE5Y1J5cu66tFB52ZN3VfPh7QK3P9F8CsvmY8bzF7EYq+kAuhFD6Goy4fYm5e6ZnAxSqHFkW7d1TxXOa67BwGYFtx9XusizI1qRzgZPrZ7de7WsZjR+gLHBnqaXFwERhaWnYD4H8NFroDbs0+qL/MMw6kdfTpn6zMXJA1aMGN8uIvI+v0kT5C63+rX+N7JipRJpFbMX1I35/Wmbxy5JfuQkrIBcvW9tWbGF27jyg5Ut0LFw== 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=6bbxc4c1jouaMm52h8ANLMdugUP0IkQ0//pJ6002zB4=; b=E+q4L+n9Rs5VMGkFlOWgJlKvy/e29YSMfv9YO74FSisDYwSSARHKhHJ/c7I2ahrWQfBIzZDatBKti9HjQJF2I+s12v+s/ya6edfTL8lBpebgwcPCh4KqZBD6vaXZkQTZrOxtVbD4m2uaIA51ocjxN4uoMTQFjkYLxoo3icFc7oRBvpRL/o0blnouyud+yU6WhmP5Blv6jmhEl2wiy8ybK/cPe84E5sfwMPcZ5pBenlTyzoA3egWN0eI25iuuKZT1LCTb1jzW8I8d0lJuowR1ir1Ua6tO+R+FWx+38JF85/kXvekjX+lRXMlG5wsfUQISy75Mm8IWW3klEHmC6RCXlg== 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=6bbxc4c1jouaMm52h8ANLMdugUP0IkQ0//pJ6002zB4=; b=ugoRJAUESbuFYnDXCpuLq+1uefs0phJ5XRILuB21+Tw/h8y9IA/w052Yr9UFrITx8aQBDjaUxSOtMRSkbiSJPjtwOsCRzoqei6R0Auy93Sd/v+5bU92EVSLik4Ez0pzxuRSo+HGfNsgdRC6hjV66ZYYGz+Yk9inkBFTnJ3xXD4o= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BLAPR10MB5235.namprd10.prod.outlook.com (2603:10b6:208:325::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.28; Thu, 25 May 2023 01:23:07 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa%7]) with mapi id 15.20.6411.028; Thu, 25 May 2023 01:23:06 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, Qing Zhao Subject: [PATCH 2/2] Update documentation to clarify a GCC extension [PR77650] Date: Thu, 25 May 2023 01:22:55 +0000 Message-Id: <20230525012255.2807393-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525012255.2807393-1-qing.zhao@oracle.com> References: <20230525012255.2807393-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SN6PR04CA0090.namprd04.prod.outlook.com (2603:10b6:805:f2::31) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|BLAPR10MB5235:EE_ X-MS-Office365-Filtering-Correlation-Id: d5043a5a-23ba-4e34-30b6-08db5cbe9803 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DZiD20+sF+jO6FyJMWL+tSSZNzYt3X2Heoj6J1vHQvkiTUs0GGBHJkc9mp7FtxSQccrKUR/Vz9s5lSrvxpVb88hMaDH1Bw31b9KUY3oUF6psveHKmzW6y8nMgapuLRmCTfIDdt8QO7TW8bQlL6HC5H9xZdTofywIdZBzTCQg6XZ9hiFVjSu5MKC2jmyxfavIX85QNtOFEbJ+r4GnRuf4hh820WtK2vgMUwo/WndwYdxrlTh45QVSBMlmQvKY09vtDkFlJpeNBXT4EIBuBIQkJ7xiGFOtqJHEabE+FdopTujYZb89mn1LryeJPdTj3PXc6NfvFR0wbTiSQ6nTM11Qxj6V+qAp70MFBiXgsQ5MbZF964ek0bfiBkzUX+M89dJv0r3YrwDqlg21kRLHfO3c+OcVkrKY9v5OQlR3kQ9SNG0zBp2KO8WRqJf+1ZFCEti2Gmz7fBGKXTISGkXO2A+rpLsEWWQRMcV+T0sdBbGveq4y18LlN3zjTXxYkZ62xTdKr/DrujoYm2aT5peOh6gZfNUaHEZSqxf5YDzda8NoaPo= 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:(13230028)(396003)(366004)(39860400002)(346002)(376002)(136003)(451199021)(66946007)(66476007)(4326008)(66556008)(6486002)(41300700001)(6666004)(84970400001)(316002)(5660300002)(478600001)(8936002)(8676002)(86362001)(38100700002)(107886003)(44832011)(6506007)(6512007)(1076003)(26005)(186003)(2906002)(83380400001)(2616005)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?OQ6OHXC1rj8u2DQncnpVY1MWJfQL/fEqcr2fVibqSo6XqjqNGbz61Kz6kapa?= =?us-ascii?Q?K1mO9fbT2ci4RWJG9gnSlmH8Yc2XsD3DwroCjMh8bOnPewMUFWmBUGVgZfge?= =?us-ascii?Q?ETBKiAm04aRmCGyBA6W8txx7rqhDVH/w0uI+/jJzn64Ro3A3Z678gaZiVXDC?= =?us-ascii?Q?FCVDFkC+IRqdwhTF6pQpFpKj3Iz9ROLu8oY4GeRvew5KzwwDui9lbC7SOIVT?= =?us-ascii?Q?0e2ymVbDPNlOPBTQ/2Q9MxZzmTMj6nVIr14AD34ECmDDp0GynZcFikv+2mLF?= =?us-ascii?Q?XIoJnSSQFFoqSOAjVDX2Qcyru7eXjWe2CqbI7Ys4maEFohoLv/Mrbn3bdZWA?= =?us-ascii?Q?4QMtLJbR4+nBu0lA7tkjn42o6wV1SItrPHzG9JfRxSu3KAb81AKWxMU5Pk6B?= =?us-ascii?Q?j2L6gP2vtbLIcRiZOgx1K88QzaNGaIwjWc0CVseGEfCSd9ICA8/SpQew05FV?= =?us-ascii?Q?uMG1XM/hmkE7Yr1mHuQJSRDRBvPOQ2V6CWhk/iukyGKS/FDjrh5j1EnVBKLI?= =?us-ascii?Q?YWRA24Vm+yLEzKW24qvqyNR7bp4/yN+wvxYbfF6BVd2Fbd3OE8Qj/kDFrOe2?= =?us-ascii?Q?cHxeguOpWkJEoBx0rAU4O0ZMvqu4jGKwryEtTIDPGYq5kgS3Jtx8Pm5EwT3+?= =?us-ascii?Q?BrDy1hL85J2Y0t6GkfpIGaEWB1tigV5H6rM8AI4UZgo2NNXXUCS+/AybsZAn?= =?us-ascii?Q?iqi4IPEijkVifBI4bk0xKcLaZqRM74QHfKtpH3fRFYVRGiHwrEv1bkMzHdap?= =?us-ascii?Q?DjZPA0x/w3MPNcA5xFzR5eVL/yPOe2pwMbQWszKgidPEezWeUw9CCJV9kcON?= =?us-ascii?Q?t8DsdttyjanRHPCXiQv23ozRnsTS8q8oeOLJPLdQ1+AYagrX0eM33MsyqFN6?= =?us-ascii?Q?z0JprAOfHtBn/ufM8PaN+YXOOyxC9Gwh7VaOtTOwq2oFKCkonXV96q/sOAsK?= =?us-ascii?Q?RvNdf1y1Sv2ztSZdMlKoqybDqJYVzoKbnGUJC3iYO2pDJ7STyP55cKRG1trY?= =?us-ascii?Q?zanpnEGcO6cvKl2Wwk3Zu4Vqmnz+9wIt7Xshrswrv2cbYDDIoFFJp19VqclA?= =?us-ascii?Q?3yYY91ykwBOPHSiXEc8CLeZY6ysshn+7T6YM5pd3Rm02yEXYBxYlA07Mm9Py?= =?us-ascii?Q?w+I3vU+qcY1l0uh6vbJ1E8aZM7xJE41dSF2linF3aeBvhTOqCfSLK1/p0Ff5?= =?us-ascii?Q?RObrT4u+4ynJUepPKN0iguaz+oqvGtMYJ4nLPmagR4tZ4i0ZNJL8W3zCOlwH?= =?us-ascii?Q?nLL2hShz1Lb1rOJfuWc1CUbvC2N5MeVcJHsW5R/vqdmb1o6T98LIvb4DISXl?= =?us-ascii?Q?9ovjQmagOeJbajCli8V5IfDg6T/9a98cIiQTdFlxaj49egjGkA7xJHEpCsVa?= =?us-ascii?Q?lV8mv7fbYHXpyfCK0nuw9TSwxfumUMhtxzVD3CseE3NOYRBfPAoZY6l0yvwe?= =?us-ascii?Q?/4HsNVQeIqjdEnpWwUkbLknKMHeby16oe7F0AFOuoFM9nzKJcykkNVjHdPii?= =?us-ascii?Q?XBL72q+OFzzFsWyMOCXiyfj9/G4Ex/lvLotGT0rP1P3WPRHUeGqCCaPxvtw6?= =?us-ascii?Q?tWIELZ3aeEPRM5Hr7z356YBiV8b1Z8YGNfpVtZpT?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?us-ascii?Q?DCa/6cW4dxtWSZU+hraVKcs0n4GWoxNcnRfvc9ueWyORpkS/LOuI5o6muAFh?= =?us-ascii?Q?y9HkWwa7OFr0Lhv2gfbKbEtTB/Ld8qFwnNarRT865e0aBZbFVbaf7wvWcLoG?= =?us-ascii?Q?oDwNnSENs8X80AbRjQN0QNBRdAskF+rinVxygleQCgT+uZA5wpAjyy/3hZSY?= =?us-ascii?Q?1iCqRgLAJOaOs44DE1zfyBqWQDwVRmNBJuAfHIiynvE4bOa+E+QWz9yu7blG?= =?us-ascii?Q?X9X+Astejxd+GRAga1FJ+p0pXhQMahw8KLAV8Y0IbsI+F/XxAyyzYyixuV4a?= =?us-ascii?Q?3b+HZMhPN52GOkzkfylsxJjkysTTqTv6HS77GzFwNnMK1u9eEVVaUbnrk1+u?= =?us-ascii?Q?ChuNkOi936Meny090dRZ7BfiRyRrMT/b0HoFuNKXQ73/LKFoaOE9BJ+CKcTk?= =?us-ascii?Q?a7X4/MBKKY5o93rv07S+pTPbe9RR+1UdubbrSVRbMg57qggENTJ/Due86NBK?= =?us-ascii?Q?B9h3LbpJZGqfbPAE9zTVrwcCswOqOXYvVSGFF7rFOBZOnuj223LYVOvyrG/Q?= =?us-ascii?Q?LNq0dMftjWNpPbI/HQSHmccAfjAtnrLs67Dd8o9BuUYTK4nD68oPuxyDR6QE?= =?us-ascii?Q?D7X8LJ1OOGS3tbOeyXIICtKajoz+L0lFCyZ1earM59QCASXRWRVX2Tw3HgCX?= =?us-ascii?Q?srabRXJC6CYFxI1io61A6bSLCKhRhbGiLfslP3VAc3tgTVahF0Vu4aYJ3dqH?= =?us-ascii?Q?rOdXfSNzprGdaTuFKXKrrFnyn53bjFOweIBf2XP2Tq41CZsmm8/eI1pclJLN?= =?us-ascii?Q?uWTIFZ3sAex0LSTsXnVc/IBmay2d4ls4evquPzyKBfPnW3C6M0TW3fIz9TIW?= =?us-ascii?Q?VyVvyJ408zNnxHVe8ktfT3So9utNjfg2rE+akUMN6bnWpNT/0YXP+akvS0LG?= =?us-ascii?Q?yQwe/TIfJs+YoL9OokyeWWayQAZzE1lxzX9xBAHnFLpG0Mkrq3kiMM4eMFgv?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d5043a5a-23ba-4e34-30b6-08db5cbe9803 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 01:23:06.8645 (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: gsxGfLe1d87oKOwW2Q8ryc0Jn/wTIO0kibBv5DTIoAf18s14emvh+f2QtRNGsZYKnPXQHKZwpcIWmOhj5h99EQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5235 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-24_17,2023-05-24_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305250011 X-Proofpoint-GUID: 76qp2w7-PMF3zUuM0bXQFeNJ95Gr_7_u X-Proofpoint-ORIG-GUID: 76qp2w7-PMF3zUuM0bXQFeNJ95Gr_7_u 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,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 a structure with a C99 flexible array member being nested in another structure. "The 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. There are two situations: * A structure containing a C99 flexible array member, or a union containing such a structure, is the last field of another structure, for example: 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; }; In the above, both 'out_flex_struct.flex_data.data[]' and 'out_flex_union.flex_data.f.data[]' are considered as flexible arrays too. * A structure containing a C99 flexible array member, or a union containing such a structure, is not the last field of another structure, for example: struct flex { int length; char data[]; }; struct mid_flex { int m; struct flex flex_data; int n; }; In the above, accessing a member of the array 'mid_flex.flex_data.data[]' might have undefined behavior. Compilers do not handle such a case consistently, Any code relying on this case should be modified to ensure that flexible array members only end up at the ends of structures. Please use the warning option '-Wflex-array-member-not-at-end' to identify all such cases in the source code and modify them. This warning will be on by default starting from GCC 15. " gcc/c-family/ChangeLog: * c.opt: New option -Wflex-array-member-not-at-end. gcc/c/ChangeLog: * c-decl.cc (finish_struct): Issue warnings for new option. gcc/ChangeLog: * doc/extend.texi: Document GCC extension on a structure containing a flexible array member to be a member of another structure. gcc/testsuite/ChangeLog: * gcc.dg/variable-sized-type-flex-array.c: New test. --- gcc/c-family/c.opt | 5 +++ gcc/c/c-decl.cc | 9 ++++ gcc/doc/extend.texi | 44 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 +++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 3333cddeece..c26d9801b63 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -737,6 +737,11 @@ Wformat-truncation= C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trunc) Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=, warn_format >= 1, 0) IntegerRange(0, 2) Warn about calls to snprintf and similar functions that truncate output. +Wflex-array-member-not-at-end +C C++ Var(warn_flex_array_member_not_at_end) Warning +Warn when a structure containing a C99 flexible array member as the last +field is not at the end of another 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 e14f514cb6e..ecd10ebb69c 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9278,6 +9278,15 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, TYPE_INCLUDES_FLEXARRAY (t) = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)); + if (warn_flex_array_member_not_at_end + && !is_last_field + && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)) + && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x))) + warning_at (DECL_SOURCE_LOCATION (x), + OPT_Wflex_array_member_not_at_end, + "structure containing a flexible array member" + " is not at the end of another structure"); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index f9d13b495ad..17ef80e75cc 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1751,7 +1751,49 @@ 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 + +The GCC extension accepts a structure containing an ISO C99 @dfn{flexible array +member}, or a union containing such a structure (possibly recursively) +to be a member of a structure. + +There are two situations: + +@itemize @bullet +@item +A structure containing a C99 flexible array member, or a union containing +such a structure, is the last field of another 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 arrays too. + +@item +A structure containing a C99 flexible array member, or a union containing +such a structure, is not the last field of another 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, accessing a member of the array @code{mid_flex.flex_data.data[]} +might have undefined behavior. Compilers do not handle such a case +consistently. Any code relying on this case should be modified to ensure +that flexible array members only end up at the ends of structures. + +Please use the warning option @option{-Wflex-array-member-not-at-end} to +identify all such cases in the source code and modify them. This warning +will be on by default starting from GCC 15. @end itemize 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..3924937bad4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c @@ -0,0 +1,31 @@ +/* Test for -Wflex-array-member-not-at-end on structure/union with + C99 flexible array members being embedded into another structure. */ +/* { dg-do compile } */ +/* { dg-options "-Wflex-array-member-not-at-end" } */ + +struct flex { int n; int data[]; }; +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning "structure containing a flexible array member is not at the end of another structure" } */ +/* 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 "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + + +struct flex0 { int n; int data[0]; }; +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flex1 { int n; int data[1]; }; +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flexn { int n; int data[8]; }; +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ -- 2.31.1