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 D17263857707 for ; Tue, 30 May 2023 18:30:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D17263857707 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 (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34UE4jMo021703; Tue, 30 May 2023 18:30:48 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=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; b=hjUteIJwc4WTow2g6r/l2VJRYWdIYAxQWLYayiHlXjqv1FvZ49dWir0xWB26bGJ8ACq+ chPyh9yojRY2MKxrjQNUdfuXhecWLMmdlyh3TCACgObksjla9kUqvTbhrx3Lv7S7uc67 BgVhFoT2HJf6R24N09d2YiQhIy78kGYXG7D6GBYFd4dc40HZnS+XNf7uhC0A725C1SfG oK0Hd75xxekz1pmmd2kijGe1IKVCFPDgx5QeIAUUz5HfwPJxw8H3os+h9P8R5+VAeFex VENfZphDF/drv6L/ZWnIKe3w64a7jbHW6V974ZCtBn4wzc2iEYQ2GDPIiTsYXFH+2lTD 3A== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qvhjh3gqh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:48 +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 34UI0uJt026089; Tue, 30 May 2023 18:30:46 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2040.outbound.protection.outlook.com [104.47.73.40]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qu8aatq7d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZkcxFG/rS0I7EtvJvzHZMS4yGPzW7zQ8vwDA+oSir53bdadqjQX9jJVdOYdyHAs7Vg+rrhPBWsC80dSIUIZkYLyQel7fKug/5J5qf3HPS2KYaOATlSS0uCrjhhOOuXz90DCJK7es1GGESURODLKaE2oEJK1WGD6B8F0zbAXd1f5QCTmA5yyf4P5gywGpLmHr0laLB6fJf2JOUPuu5GuzBpvyAK+/lSxDNIhusydXklXjxhnfuJOT/Fp+aYvC29lfcjbFSkWzwQ3f9g60A3d5XVRhWGS5X5ygfe0/9mPbFd7ivDLwIMztDmVVWMmCpys5o1cqXhmdKIUjIy24R2OBww== 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=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; b=KZlkgaUvj0wW+8tQdNdF8b2pvrPpyY0+bzafNCz9l3KpBEZuUStnMdQ9vQLObjzj2Eth5UvR46GM9k+xmsQIJE0f9SPM8JdRKHP2VMYm5CWTFEgJHxDBhRb2uCC50JtVhs1p/2O/D2c4yx3xEOEctbqch2MRJ4bWZrvTZG7DXMPuzTeQlWISk1fTfcPo+3q9+oU3alB/jDt7M4LiuTI8Bm7rWeMFXVbypEefu3H3J8mPd7WBmorDJuO0eKgIvp8OutTBMFi3pg/+OJFdHqkHwRkKLuHWxV5bvcFpS4+PBWGRZVBSm4oSSmZ58t/eSyTiz0t+JVoS1M7TZWDHpbfYcw== 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=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; b=aFImwJeF81w0MCq5wPu1EzqvUCC5Bm+hyJNo9PemeutzMirDo5NEopImeYESE5hrjNEESW0ISUW0W/g8g9GSDUHdkldbNFwvH94qG9ThWu+SHB3tBz/aI9alrsE7VK15YTxG6SldcVXBwrrbAh7jkkQksORXTljXYXc5hrwD/p8= Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by PH7PR10MB7033.namprd10.prod.outlook.com (2603:10b6:510:276::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.23; Tue, 30 May 2023 18:30:43 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4%4]) with mapi id 15.20.6433.022; Tue, 30 May 2023 18:30:43 +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: [V9][PATCH 2/2] Update documentation to clarify a GCC extension [PR77650] Date: Tue, 30 May 2023 18:30:29 +0000 Message-Id: <20230530183029.1156862-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230530183029.1156862-1-qing.zhao@oracle.com> References: <20230530183029.1156862-1-qing.zhao@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: DM6PR21CA0016.namprd21.prod.outlook.com (2603:10b6:5:174::26) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|PH7PR10MB7033:EE_ X-MS-Office365-Filtering-Correlation-Id: dd611da6-9871-4aa5-c507-08db613bfa26 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A0Dgq9Lbzl92ERa/Y+vJ5jVtrvmDMH7Y8nnjZ52dzx2Mc7QB0Wd0HsA6WYTudHEX1+o50+U6/Vm9xH2iyXbBhmDExS5R95bzTLzA9UYSLFFgnC9tBxoJ80RA2HPQ2i7l3gO1bDeAwxCp27Ca54eQlLwLHfYNNRFgE6z0zk2n3VP6GOORa05NXa+hiQtx8YWlZ5qU7QPpYy3E3OmIS3a+knBQvqKGtfAIr1yGiioabiRBEZd931cT/IML4LqwZtVOhAsarUyMkzOkyC4YiAOgWBvgI6PIJdevHZ3yjtRPSzKKJBMuGp6KCo2LaY1Y9IHlwNJ4K5T+UxkZc3Ps4qjtq+yxtZ7sG2JZgFcxpLr2nN+4r4J7qAvSCUOxzMVUf+lc8HRC9a5JUBoQN723XDT8OMFCZXp66ybzxKEumZpt8GuWCC7OkzQzqosP1U5n6xzM9fIaQ2Eyi0r4R/JMRac1egQDu1JeddHE6EUp3CBUAUJCQz/nBx+4cHaVewRWaUgpjqsrYm1HkzLbpX8voPaMqzOnC2XZdyYNJx1WXtFKcOQ= 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)(39860400002)(366004)(396003)(346002)(136003)(376002)(451199021)(478600001)(38100700002)(66946007)(66476007)(66556008)(6486002)(41300700001)(84970400001)(8676002)(8936002)(5660300002)(86362001)(107886003)(44832011)(26005)(2906002)(1076003)(6506007)(186003)(6512007)(83380400001)(4326008)(6666004)(316002)(2616005)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xB7ppOuS5swqbKJ7+tBGcoXF31kD2fnpwn8v2LTlTpRkqBg/ORFp1CR5nrnl?= =?us-ascii?Q?2Kz0twqVFL5Bt7vvQEZzmiVqxRjA/pGIJSLy8DpTr7p/mGOB3vNXqvbQJeQj?= =?us-ascii?Q?HV3/ooTZfcXiW9f/MZK9yUviONdac75xsG7Qmj/dKAPdEinvCvBG/2nYRRtn?= =?us-ascii?Q?OIoF6y/giAZFJloMBiv5Y5AHHrisQv6DomhNLtLIQOgFn9BkcfItjObR+NmL?= =?us-ascii?Q?yo9gJPhDjEJo1DUukY6XJ8nxXv2+NZZ0bkqQ/go78vaZs+ydYg0x+lOBbBHT?= =?us-ascii?Q?8b6eV0MP8LTO3Deq4sANCa5cQLkTVBMwueOdp/tNLSIx17bsdZvQf0zAgZsi?= =?us-ascii?Q?uI61F+MWX7TMl+mvVySFJBlutpSi9TZb41gFhkjGVHSkZO5/T+oQnI2OdY7U?= =?us-ascii?Q?wWvQ7MwhfNL9i8x7zkMZ8/BoMVKZ4x9cTT+hEhjSl1En2TQb8QsOyILgihIO?= =?us-ascii?Q?YQ4cGMtiWcr8LL8d8bstLp5e690TCRFR4bf1KIrALU1FPiO3Fj5aY7V+NdOg?= =?us-ascii?Q?K4iP0/7K32rQeOu6nMth8OvhKnETzEuY6AUhuRxcsT9SH5R4EUSl/Q30krno?= =?us-ascii?Q?2iAE7D7BpwtFqBv7EynWPvEvuPKsz1FCkbnoE9UkA/dM4Rf34eP7bcye4iiF?= =?us-ascii?Q?PeG8XQ8HLEQRI31f3AeEwK8eOMH8o8M1mLgo2gJ1ibM7Ttz4ANiNXRepNeUq?= =?us-ascii?Q?mCj+L1L/bEelkfz2/5tK7IAjjdjNXiOAuZE5ECdX+768Yse6vNCKK70XmakI?= =?us-ascii?Q?jpu2g6meK8cJbbLX26krLudQAxIr5qoP2EfR2vnHchCOtgEctZV+V6QnEJh1?= =?us-ascii?Q?tJQWZBc1zgtjDOM8fmvJzymFS386VIj4/O7oTWybfGh76ydo66LfEznP1zWK?= =?us-ascii?Q?u9yDkdgiaYOhcInLglUkA+u9T0fUu6Cem/X0Q+4eXV1FWWqRVfCxQImJrf5/?= =?us-ascii?Q?kNJGLYsMECnA7+p6GcYcHdoVy6WeRAtMHBsWNaJ21wer3lSAKZDVFeOJjqL8?= =?us-ascii?Q?yGcezk0oji/fVNpgFPlIP6ZfjcWgdIlN5leAesWpwe0Gj+Ls/5TlFPTJ5/Pk?= =?us-ascii?Q?a9NZtcidPIkJQa1QAUgTB4LnpYxwB8JxRn13yPwRgvR7yy1Prh7Kjj86HWle?= =?us-ascii?Q?CJx7UBFY67hnVRz6YEzKS7PotfGu/l3f+O0/3CS2EN1kKfSsXsmbq/rgCPvt?= =?us-ascii?Q?Zz3vt2D8cBBcMQl22qriDX+tGEEjA4cvyqoF7zKDNFg9ctFaGh9k7dCmBn2b?= =?us-ascii?Q?EAmZotkI3MJ+MX49Yj38AP8d0jzcgZ7vzWZ1+XtfqrUYePUpiNnNWOGPvYx+?= =?us-ascii?Q?Yw324Z45A3gyaqb0dmyn7714zedd6tYr058EJs6Yx6+1eLJ5B4ajA744B6o0?= =?us-ascii?Q?6A11jkdhPQmNKeq1NDeiyIQTAGmtrSkm979Abl7fVSNUblIUnsxm5tGnWzeU?= =?us-ascii?Q?sjioKZrP/vvTLKqSs7Ll3W/lVvllbhBHgSwJZl50Dlvz930xIgLNsUbEGeDy?= =?us-ascii?Q?5VXS5ESFcGi+gj/in46Pz127p3Y4RjnlEVjVIJqYYWzxz7HJx3oJJ5HP++pe?= =?us-ascii?Q?qzeY5BgX2aw78cvdR+doyITvJzoY1Ox27L1zFLEo?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?us-ascii?Q?QcG79RJL0QDoGbtiJmezHXZNo4y6DjPECJg0qEiF9vMTWv4kZ7MqLR81JLcD?= =?us-ascii?Q?1QvXrWDBKs/O4GNM4noFwtZtRLtq8jtd1lV1ntoB4XsQPXoKySyOsOZR8jXc?= =?us-ascii?Q?QVliqNOoxkCZ+xBleLlISLuz8eQcqJurDQ5XGYczs/slXgnw5VZBNbHy5+S8?= =?us-ascii?Q?FHkqtDD40QD5hPplSgwHRIlQ5hDtvCtoFzGAju7N9GojVpPXoMlLdU/XSbUx?= =?us-ascii?Q?ds1x2A945KBmnmu8Ooi4NW0NOeyZlUa3HPgZ7FT2TkzPibDWJj9lDP1XSWBG?= =?us-ascii?Q?85iyQF45vZMRBNvJpNj0mnZbQQslqaWbpCLOKJ/cwaOkLHpRaXW1Y4kuFwEk?= =?us-ascii?Q?8EvUePXKS8kqCgmEcHUvBFpIW5B7937EjNWNhf7o6n7xPxIHy5UPCjOKO8Ff?= =?us-ascii?Q?PpHQi3v1omx/mQzXUdFG2Vta1H92WK00HEOWa43APRTqGi1hOB+P3ea83Vo8?= =?us-ascii?Q?0e1sTu5TW1RWFhMh0azvFCiG1er4mYJc/93bobgJHk9V4fIu6PwR1riV5SUb?= =?us-ascii?Q?ztfyCJYPhfK7YBBKXlBDfTfkVzYCSsjDnTn/2cEV8fx9U6XbHplhft+nfKNJ?= =?us-ascii?Q?wVksJ3VCWNvaUaoa0Cn7jOrhDfCFn/8TCFIm3jiftByXHWG3PYBzh9FH2Qsw?= =?us-ascii?Q?oWxvrOnBbwPTcyVLmtfSa/tcPrnbPpetrLhtc059GNyqnDHNykj5He8P2zEr?= =?us-ascii?Q?a9ZeJuSAmRxYlNuXr/IGvjZlFO0OBk0Nv6lGs8Kb/NRLCtpNSsnaKY1bc0yh?= =?us-ascii?Q?y75fKy84ADGYcVPWummXKHr+ES6Uuk53jwdrNK3rnONEsQpJA6AtJeI+ZsUL?= =?us-ascii?Q?YuBjsnf9yL0WoEPkVT8c/nR6XLXvvVwcnXlSL4NJkW/kWUdL6Y8R6c99nddI?= =?us-ascii?Q?Dvy9zV2qJF1xZnjo+OUsan35jZqeNn28JsPwu/lE31N/z5xnSj1kdqJirsOg?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: dd611da6-9871-4aa5-c507-08db613bfa26 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2023 18:30:43.2734 (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: 4gFELrH/g6Y0QEEXMz2jV4CzCokzFLTB4csKm/JgcUbkU1Xul5aYQUBvuNzKm/ZrUTYOqB/ksjqzWHmNre+GUg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB7033 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-30_14,2023-05-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 spamscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305300148 X-Proofpoint-GUID: a1QGZhtYJAyJLxx0A44FIBRTFR3ls1NG X-Proofpoint-ORIG-GUID: a1QGZhtYJAyJLxx0A44FIBRTFR3ls1NG 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_H5,RCVD_IN_MSPIKE_WL,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: Joseph, could you please review this patch and see whether it's Okay for commit now? thanks a lot for all your comments and suggestions for this patch. Qing. ====================================== 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 extension is now deprecated. " 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 0c718151f6d..a1a8e9bd341 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9293,6 +9293,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 ed8b9c8a87b..aa1ecb6968e 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 extension +is now deprecated. @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