From patchwork Tue May 30 18:30:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1787854 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=aaspOr7C; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QW1F15XwMz20Pc for ; Wed, 31 May 2023 04:31:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C25143857735 for ; Tue, 30 May 2023 18:31:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C25143857735 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685471475; bh=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=aaspOr7Czjm/ZuF+B55+B5ULwneyGgAw/YuR02XkI1YqiW9VGzt4+EqhBgo6wadPE 3P+VZt4D1oauCtvoWcBcSTHeJ2m4+0K/1XkytuUlOXtpSeKHVOgRFiSJO0+utIup+f grrKcJ4DRQO/5cxrHqoISIGcxOs80hUCr4/J6XuI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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 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 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 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> 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: xB7ppOuS5swqbKJ7+tBGcoXF31kD2fnpwn8v2LTlTpRkqBg/ORFp1CR5nrnl2Kz0twqVFL5Bt7vvQEZzmiVqxRjA/pGIJSLy8DpTr7p/mGOB3vNXqvbQJeQjHV3/ooTZfcXiW9f/MZK9yUviONdac75xsG7Qmj/dKAPdEinvCvBG/2nYRRtnOIoF6y/giAZFJloMBiv5Y5AHHrisQv6DomhNLtLIQOgFn9BkcfItjObR+NmLyo9gJPhDjEJo1DUukY6XJ8nxXv2+NZZ0bkqQ/go78vaZs+ydYg0x+lOBbBHT8b6eV0MP8LTO3Deq4sANCa5cQLkTVBMwueOdp/tNLSIx17bsdZvQf0zAgZsiuI61F+MWX7TMl+mvVySFJBlutpSi9TZb41gFhkjGVHSkZO5/T+oQnI2OdY7UwWvQ7MwhfNL9i8x7zkMZ8/BoMVKZ4x9cTT+hE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N/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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" } */