From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id E1C8B385840D for ; Sat, 11 Feb 2023 00:50:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1C8B385840D 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 (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31B0OGQ3011924; Sat, 11 Feb 2023 00:50:29 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-2022-7-12; bh=SNbA6qj+TAiFvaLAfRhaymPb4qFnVyLkPHkNsrfdqAY=; b=FOUdNuSRrLDpaREXDMO7KIlMIT+ADvGfZBcTIJqINrsE8B2nxrRow+uvkvWf1oqvBEjz YcRtImsjYUMb7HqMfz+bvTmOHGwWWNL8DSpgftaSnveSuyLZcvgd6sb/aJBkTzcQCA1N HBx8pFqSMq6BzJqbaBdVRmmmBFdzc9u55Ufw3VbSwZKU25sttVJ/gitp9fwcGuvsez46 1dK1Y4K1TuIj8JKQ4sUuM+GZwrSdv6AHHrwit0FDAeHtEAemo0tKusbyrUbO5GnC1k5Y 1wtpmwM7gCMYtXsJBcybk7dhda9VcthREVdrJwjepczc8JiR+cXhHN7Tm6njHVyJEHGh 1Q== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nhdy1ep5v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:29 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31AMGWap036215; Sat, 11 Feb 2023 00:50:27 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2107.outbound.protection.outlook.com [104.47.58.107]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3nhdtha15s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GB7lxWwhTHt9bwBWxfPeJKtkqf+Z6bdtDtwqWy1jy/8VmtZw8fScfRTxdp3C19BRCzwo6zgtln+2W1u7DC59poXU86Ksb2mRSIZz+LrV+jLUJUB9u8V4ekvtpkgZIMqrhS7Zzfjyn/7IP5rLDIX9y8aYKEkrGmt2CatwIgeaWh8znxYXVdj9T7Bw0UhK4kmw4NFC0kU4aA+/M9tBptL07+9bgawGzmedxOx4PSc4hEWbo5p8T5WNPZEJUGU8dexFY+OfG6iydRV+gHWdi5Jvxy/3thkKwI5sbFXW1EOU4o6CJ1s2c2rTXdheSepyWW9cRiJ3GZSw1LwlV/BtxFkYGg== 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=SNbA6qj+TAiFvaLAfRhaymPb4qFnVyLkPHkNsrfdqAY=; b=KauQKGcPwjDdo2wF2CY0ZhdKH8+cTfL/hQeKd0Q96tn5AnyoeqEDZMLBHZUrswyj7M8E5IkgaaNZ8DHAlasm9UvwK1v9yWTaXIpEjvC50RbdSyWy+ODyhRe1qLrWwXPN+lJ5GI8xw+B8gk9xaGTiS4bQIddx7O4eKkBCYUyIAQ7OgjrrdzRd+jxJN+4c+TJVEVhIGprixAKPpqEWjBkLccIAw6LKLXzaGiWtwbqn1MizXTU1KKLhD06ucR/hGU6sViBOmkwiUKtxQeQAg4VXOZMvNhnMxD2LbC+Lh0D02ZujaLsToSdMSHTwjZLKb42s+e+2vDwv557fdc29kvx2qw== 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=SNbA6qj+TAiFvaLAfRhaymPb4qFnVyLkPHkNsrfdqAY=; b=XzbTT2vHu6iaGTWt0oZQxuRgaqvUaQjdhEY8xNBUBuC2ADS8+gxIipopY+JIixM/4xNZ63XFgGR6hL+yBG3AJ8IbBz74CqRsGmlNRGVIzH0V5S+qNdFbNVmWHeOyliM2X2FMGeQar7q++P5aS2EpJkZ7eGK8BLHxTBoM1csDw1w= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by DS0PR10MB6176.namprd10.prod.outlook.com (2603:10b6:8:c0::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.7; Sat, 11 Feb 2023 00:50:25 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%5]) with mapi id 15.20.6111.007; Sat, 11 Feb 2023 00:50:25 +0000 From: Qing Zhao To: joseph@codesourcery.com, rguenther@suse.de Cc: siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [v3][PATCH 2/2] Update documentation to clarify a GCC extension (PR77650) Date: Sat, 11 Feb 2023 00:50:13 +0000 Message-Id: <20230211005013.789161-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230211005013.789161-1-qing.zhao@oracle.com> References: <20230211005013.789161-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SN7P222CA0009.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::34) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|DS0PR10MB6176:EE_ X-MS-Office365-Filtering-Correlation-Id: 3da5f4c4-cbf0-416f-44d0-08db0bc9f68b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bIlbZLMhHt020KofNZtQSlex/JSBC3lBN8MkEuN22G/CiduC/jAoUIzBwyQvxwjxTsDWfFITSv+KCuvSyFhFreqymEL4BHX7vswbkaVFtxBPFLT9I5H9lSokjwsP9SmHahpKmoxrDPDjcI3oSPMfaiRpP/MSKAltCLQvW/q98abDY4BJlQxJdQs4OeK7l5M9v4O6KNG0818Vkbe7SMwc+fae17Akdk4OUQZnOiyS7HQceOL1ctdKPSKexK3GYDaDq8186cl069ihnIXcPMIqrz5IM1K+dVHQvAOVX7Oi50z1+0p3Gz4d52hGvpZflRsl3hxn3AhI9Lplkr8mi/xb61Z8SMSI7F1SB0Aud1TvugxyJ4UnLk/bQ86/TRHOMLNKNvTxkzWFnXb2BdlohPbQt5p9Fo2XnX7KzIrA/QaYVZIJR2iIZuCHRbWi4ekuGoBQmthpP5z512hvS+Qr1i7tzswNR0mMwHgBjRt5mn/0OewargVTa7W1GDvc6T0xcqbuzEl1v7o4uv2L3jVBK2JNf1qydowD+yWrr6zFGDf2C8KfplmTt02w50CUb0CMGU6UztkuJthDW2rMy0dyplLsBUM3oWMivNEymUt/id8Tm/86cJGjPgkMy3Y1NKWvK+3JSN+3FGG+5dL+gOIn5a9sDuypflJSk3NC4CyZnyyGMDE= 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)(366004)(39860400002)(136003)(346002)(396003)(376002)(451199018)(86362001)(8676002)(36756003)(44832011)(8936002)(41300700001)(5660300002)(84970400001)(4326008)(66476007)(66556008)(66946007)(38100700002)(316002)(83380400001)(6486002)(478600001)(2616005)(2906002)(26005)(1076003)(186003)(107886003)(6666004)(6512007)(6506007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?5tGCqh5s0IKPtT4lED8SrEwP0OtAFUZidIr5zYNk81h7ABTxOTJS/4xxPt/q?= =?us-ascii?Q?zU1ro6B1rnyKB8UnnpcxQFYEFbWudP6s6JnQb8d21zL0KjMnxcIILtbgfYmj?= =?us-ascii?Q?1HX18+cM6QRGRuDC1DBUBMblxRBmwMsNVm/wTtFfa213y8M4g9EislgUMcjd?= =?us-ascii?Q?2L71z8u9YW4OOycK3yAAyeyyE81BjVAZbTt+BIJ8VMoqqPPBtWahorv7DJx0?= =?us-ascii?Q?MMYkHrmnWHW4gJ1CLc/DOx82ZT/iu+gG+8YTMwEa+2fyF5jTgy6FNkzxGw8n?= =?us-ascii?Q?iA1ajMJTAxM95dNTrA3j4IxrAtsELse+D1rhYA3TzJ8Q3QoCfV2h4N+9q30f?= =?us-ascii?Q?j99ehxu0YYx/3qQ9m0Ipy0KR30ZCNWfmH9IMBeW4rVqY5xjf548H+qjCsq4W?= =?us-ascii?Q?yl0VZZuVRJ53wRFQ9txb2QcyMp9SqexpZCBcl2lafbCCvJCnKU9/16hSvza7?= =?us-ascii?Q?VlbsuCA1HByLnVQkV6JJEUAq8dQF0eXN9J+IDiHlsCsndYti9g34OTpnhWSL?= =?us-ascii?Q?sB3MZHBF+dQui6xTmnCn+B/Je9iqAQkKi8D9onXeKST7BeDCX/jnnMN8e7SR?= =?us-ascii?Q?v4O1+2w5YsrZcq8/eZyKRr0BlkNK9yi65jvg6ObrGzlVdiwFvnbjmpmSrPoq?= =?us-ascii?Q?hgTRRqseySKFgnxIX+hZ7a30TDtEflgUNvQNt3zmq2FQy8TDnOf3vvuOhjZl?= =?us-ascii?Q?1rhXuniDZMrNH+jAbqU83yVLwIL92KEKVf88i8CdAlAxRC0M6QFoUr4RsB1Y?= =?us-ascii?Q?GxWstgRRYcLiqTrxx2TawmmKNJhUljcB5F+TXGyKlBmtobJoi+Iyi28/GQpc?= =?us-ascii?Q?2yGVIsu43qg41KpDEEk0DdkI2S0hIAJPeCgl+PaQocsQe9ZRURJOQv49pRdP?= =?us-ascii?Q?haWtfR11wr7ueqQuWdAdfDobJvZQCP7JZlKtactcXqDUrpZ5Qk/c8nVjvncs?= =?us-ascii?Q?hzGY9EbVNH13bqIHV8ytNXi4LtYeR0lHVlZ+IvK5wNBYoXJqEjHnfk9X4Ke6?= =?us-ascii?Q?3sBDVA6ucximPQB+VVaLhGLCyDfSDvxmSgAFvcGUjMLOHA6pkY37BNs69hfB?= =?us-ascii?Q?M8SHmcOOvnjzPnJGazV8BHEX/rX8onv9zIw1pO4+YEan2tZ+dJ8PUEiIjoOT?= =?us-ascii?Q?AWn2rxoiKQSBVCC3/EMKIU6AaYbW3F6C6kp41GMCOmGg+MVAb4MaCRdBZ12z?= =?us-ascii?Q?7uR8Wr3Tbbd+mIlx05/fTbIUFfBytGo2lcHnkOLV5MRhEoPTo1j5sKhrhKCa?= =?us-ascii?Q?g+3FpIQ+qSXP6v0L8QfQK4E2I1yBWrzqweZ8uh8nNH7RrO2Gd1d5RiH15U6l?= =?us-ascii?Q?DuoAwacU2w49rpX7s/YHglIogmqx33Z0OLuZh9R0qkSbMmwQiwvNpm410039?= =?us-ascii?Q?jUOX5sZboMEiMG1ND1Su5C8T75H+x2dzX1AhWEkCK9ERqB9cinKBO161HpVh?= =?us-ascii?Q?/OA2lMMeDdhQkpv3AzeX6knfg3StR3GFe7QvZJez/MeGoDOVUhSm/1BGbVy1?= =?us-ascii?Q?RweEV+8vLzT3/u9ONt0gS7zEFEfHu57Fh7feXH0533+Sp2c0LhnD3HoaD/8C?= =?us-ascii?Q?rnMN455UPyd2LuGoIecQ4R08F4usLRLg7aPusI2p?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: gwgb3v+pnV6efID3IJPdg4W3zpix4flS4eaQD2vJX/5JU59HULoJdP4Pk2kb2OVWIG4FNygmz9izwsmTX0GaJHh/1MMxAou2x4XtOQvWkT2pf+QbA4n7cs197TznB1mr6mN359ejHhpuInb7wQ/pI1kmx+Fw5pCeGTuGv/e16POArpllG2hCNXQrVn+LRT5OcsBLkB3AWNemilXMHgWAk5H8ahxAXlx3sIyI1hsl8PS3+kUOm8VfwS0p8ygsmBPoCFt5nVrUE5naEyTDPQEyCHR455e7ZWM16Unq7cfnvFkcVKExD8Kc3+wU3pcbyuOxcjl7FRs3sIIZFGNn2+bsDyYQ1qbAByYIPoJM8ZfzZOKKdU3onBN68BPk/KCVn3bNZyI5JMVyCB4NdkpQg5rF48tW1XyagO3fUHNrAK/MQfaRWPRRftUfSKRLhaHLY0EF905u7X5x9kpXvUwoU2W2wHIPNtIlRCAbk2iOeVomOH7ejeDf7sBY+IEg//Mqqb36jhUA6tt7UlDdw8hFPcZX5wMsZvPha44qLD9ElgHSwUCxPVq3GDNEcpnaIknv8qzZq5ro5WOJfOiri4CqpLQUO6YxRKlGzgQ4T56V5+UIW7U3PvxqVwbl83ElJp8YCLk8CRy5JjTZceeajBVtSx2he/8hTQFjDjyPTt1LHcVtyy34zAsCMCDDZFwJ0owW8/yaFrR9Bawzc5TLWLQuco+CfJSi8CBynuUvUCww+k9IueT9Vn5f3Bnv81lFj2oBpSZn6mX1qeuFQ4Zz4OOo402Y7Nx7wNmCb34ZaOdfmi4q5E7/bM0uU3pnEfITaCddzgGHiLaFeE89/rsO5xYwf4W6ROpEHINZ4AVrpnTbOoBdVO+zjNKgV2TVTes86GC//d2g X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3da5f4c4-cbf0-416f-44d0-08db0bc9f68b X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Feb 2023 00:50:25.7407 (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: 7s8eG/Xb2qcUQ55D9cDkZrlszPA50ybSb7qEeChvM8yAVaSkdMea/VofTbSGLEGLOawTGdIsL8yZJpTLcXQMqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6176 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-10_17,2023-02-09_03,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 mlxscore=0 phishscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302110004 X-Proofpoint-GUID: C-kL9A_nBQoYbYMJRZywnfLY5nTy6nTc X-Proofpoint-ORIG-GUID: C-kL9A_nBQoYbYMJRZywnfLY5nTy6nTc 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: on structure with C99 flexible array member being nested in another structure. This is also fixed PR77650. " GCC extension accepts a structure containing a ISO C99 "flexible array member", or a union containing such a structure (possibly recursively) to be a member of a structure. There are three situations: * The structure with a C99 flexible array member is the last field of another structure, for example: struct flex { int length; char data[]; }; struct out_flex { int m; struct flex flex_data; }; In the above, 'flex_data.data[]' is considered as a flexible array too. * The structure with a C99 flexible array member is the field of another union, for example: struct flex1 { int length1; char data1[]; } struct flex2 { int length2; char data2[]; } union out_flex { struct flex1 flex_data1; struct flex2 flex_data2; } In the above, 'flex_data1.data1[]' or 'flex_data2.data2[]' is considered as flexible arrays too. * The structure with a C99 flexible array member is the middle 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, '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 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. 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. " gcc/c-family/ChangeLog: PR c/77650 * c.opt: New option -Wgnu-variable-sized-type-not-at-end. gcc/c/ChangeLog: PR c/77650 * c-decl.cc (finish_struct): Issue warnings for new option. gcc/ChangeLog: PR c/77650 * doc/extend.texi: Document GCC extension on a structure containing a flexible array member to be a member of another structure. gcc/testsuite/ChangeLog: PR c/77650 * 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 | 58 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 ++++++++++ 4 files changed, 100 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 c0fea56a8f5..fd720538800 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. +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 741a37560b0..041df4355da 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9289,6 +9289,13 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, && is_last_field) TYPE_INCLUDE_FLEXARRAY (t) = true; + 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 = true; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 5a026c4b48c..737228b35ac 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1748,7 +1748,63 @@ 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 a ISO C99 @dfn{flexible array +member}, or a union containing such a structure (possibly recursively) +to be a member of a structure. + +There are three situations: + +@itemize @bullet +@item +The structure with a C99 flexible array member is the last field of another +structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; + +struct out_flex @{ int m; struct flex flex_data; @}; +@end smallexample + +In the above, @code{flex_data.data[]} is considered as a flexible array too. + +@item +The structure with a C99 flexible array member is the field of +another union, for example: + +@smallexample +struct flex1 @{ int length1; char data1[]; @} +struct flex2 @{ int length2; char data2[]; @} + +union out_flex @{ struct flex1 flex_data1; struct flex2 flex_data2; @} +@end smallexample + +In the above, @code{flex_data1.data1[]} or @code{flex_data2.data2[]} +is considered as flexible arrays too. + +@item +The structure with a C99 flexible array member is the middle 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, @code{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 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. + +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 extension +will be deprecated from gcc in the next release. @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..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 + 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 "variable 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-bogus "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" } */ +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-bogus "variable sized type not at the end of a struct" } */ + +struct flexn { int n; int data[8]; }; +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"variable sized type not at the end of a struct" } */ +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-bogus "variable sized type not at the end of a struct" } */ -- 2.31.1