From patchwork Fri Feb 24 18:35:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 61414 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1075624wrd; Fri, 24 Feb 2023 10:36:57 -0800 (PST) X-Google-Smtp-Source: AK7set8k55tOTejshPJfYUHdPCGbr+tfrlExJpNq6JQL/mN8ZHfTEbw7T10QkeSk9TlXtTMWuLwW X-Received: by 2002:a17:906:9692:b0:885:a62c:5a5c with SMTP id w18-20020a170906969200b00885a62c5a5cmr16964395ejx.46.1677263817070; Fri, 24 Feb 2023 10:36:57 -0800 (PST) Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id fr22-20020a170906891600b008dc4b2079f5si6731273ejc.398.2023.02.24.10.36.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 10:36:57 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="BQK1TT/H"; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 23485384DD27 for ; Fri, 24 Feb 2023 18:36:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 23485384DD27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677263778; bh=exjmrdkb75D3MPmpyV4rZPVI4B9athxBtCJP8SkDdks=; 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=BQK1TT/Hlpb2R8yYw4CqKFRvoHzzuE/xFaDAMjvDjzXh6VXql6RWinss+zSeI46UQ Irri+BIJjEYU8ZS2P/MhMLXpevTNVtYv5nXyBWXx3ejBSp7IsPYAyNxMzCuTQ/+4Zr kBbhbMdh61St7+r4Ky+U8EhIX4kGYkQfHwf6kz3g= 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 A3CD9385B53C for ; Fri, 24 Feb 2023 18:35:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A3CD9385B53C Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31OHiJs8020513; Fri, 24 Feb 2023 18:35:28 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ntpjadqck-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Feb 2023 18:35:28 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31OHVAph031608; Fri, 24 Feb 2023 18:35:27 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3ntn49yfbw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 24 Feb 2023 18:35:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kQZZZz8sh2E1ak3hDwhDvyEnQ8V3hTxKYXGOnkw4/5Z8qrCmCIBBwjTZpBD9Elxk6e55/bgrP3JVG2frt4fYct9POBnf08Y5hkkw5VHPlC10RZ3TgcoYed3c0YdE1wUlbkCB/DKsKmKVDrxjKGZ9VN6ZR3SQrSUMTWC64Q7Ez/oh/VqXogKTuRNLRs7wRepW7+FYr+WlYxaaZ8NBcAzDJgljYyxczPzP35+uWVPP6R7YpbAxPLfqpceMr/CawFyGB13YVuogaSe0nXBMXFF2YjBfwXL6B08luIUPTge7fkPevsyYBPVOqFLuRnWolf/45P7au4BUf2fVaYHZ4PbhQw== 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=exjmrdkb75D3MPmpyV4rZPVI4B9athxBtCJP8SkDdks=; b=cJ4NrdaOW8NQCUrN10/T4zBR7Z8Q/edxXu2wSZln9BqtJkM9aJpXdVjJXSIE22joAiPa0xLmT0DjjXHYzvZ7G+4w+gsYPLRdoquUUzH69EK6MlhsOfHP5NbWS6PKEu7MX9X8z9wmE34kGE+kyGjizBMN7L/gZYyHQz9PsMDVxVy9butoyO6jP4r9BJT6ErIY/7wNVCxMYvu30h1D/HXYg6HOdRbNrjVgYXdSgcQyzlhyV9XI4mN3k2B485mFgifWqzo2Y02s/8lPeZgklUaIrNYKOM+EakHCQ3Bn9HsvAxYfJu0AzyULtBShX9XsRn9G8sn2WGE4aHUNm+4kNI0qnQ== 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 SN7PR10MB7004.namprd10.prod.outlook.com (2603:10b6:806:328::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.11; Fri, 24 Feb 2023 18:35:24 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%7]) with mapi id 15.20.6156.007; Fri, 24 Feb 2023 18:35:24 +0000 To: joseph@codesourcery.com, rguenther@suse.de Cc: siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [V4][PATCH 2/2] Update documentation to clarify a GCC extension Date: Fri, 24 Feb 2023 18:35:05 +0000 Message-Id: <20230224183505.4112295-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230224183505.4112295-1-qing.zhao@oracle.com> References: <20230224183505.4112295-1-qing.zhao@oracle.com> X-ClientProxiedBy: AS4P191CA0030.EURP191.PROD.OUTLOOK.COM (2603:10a6:20b:5d9::16) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|SN7PR10MB7004:EE_ X-MS-Office365-Filtering-Correlation-Id: 2db99ddb-0504-4817-b4cb-08db1695e46f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3h2WyvSv1hTrp1+Z1FL/g2Oz5/75nh9diDxmypR6Cb1oKLIXbthLeM+BfP5Ak5iMs/KPqJbDf/YHXjkI8pmHORm9Z2MGfxl05SsPKwAuLM87hA0AB1ua+Y+O75lSjjmkMya+23OPKfzY8xbLLGM5Fi3SysgrVW4iwttyGzQ//frVtdDnZFBZIOG2PD1NsyGk6AcPboI8NPUdoUkTDYa+EDSxxW9CPZHwD713qG28LKd7LatEOgrtLz5Hz1nGIB7IX6cg8Wm1PyK/5Mi5zq+KnKxix8EIUJthJEI5wn5G67az2fjV+fYFSq6qaqiIxo3zPJK8SS/kPuVVqszEa14I22VbmHhW4XiRsM1y2lncl8gLQldPcf8JSK5YEQ4xmMHjFF5OWDQnwI20nYXNrzwTpPR77C0CzCHE+arLxxi7DugrhHlxabC2xmwK0ui20i9wRrhr/TZ59tqOS+CBQnrHMlbcWM2iWuLVTXKSt2NmGAatG72F+2ggrZd7sfdDXgIKppqtlb37Hhf7FFmge5UZ01jATMopOJkETvmDCxeD0raiow5JIsjeoEMWGzb4sTDQ7D8lnKb6gCGUXzDBRZTylqTyOhNBAeOc8Bhk2SC2u5uZdx7a1j5Su+cCNGTemb2E 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)(136003)(366004)(346002)(39860400002)(396003)(376002)(451199018)(86362001)(107886003)(478600001)(6486002)(6666004)(2616005)(1076003)(186003)(6506007)(26005)(6512007)(36756003)(83380400001)(38100700002)(41300700001)(66476007)(2906002)(4326008)(66556008)(66946007)(8676002)(5660300002)(44832011)(8936002)(84970400001)(316002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 41k0df6XB6us2fuRH85d0ywj8cUn8W5gTP365d44AMlj+k/dAIzJAMbqKO1YlQD2O448+EntCtAKgz6nMlVDTmRRYYfxmcIWJ5k/lvzeaSRG3h8YPWgwQ9DUmLofbzefuSLvJRJq9Ydl2V4SGYBHS5yIm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Zm9l3/bhVgzOnKNe7s33FwwKzBT7sfaqmcile0xwunmfmX3tx9sErtJqJ/66QUFeqMz9HMl01P1UB0Qx9WVZMA7LPB/dP/gQLahFZMvPRJG/2PQYnCJ5trv+aN4iVUqhqvB6X1edxUSto2lkb/W9hrETWAtXyzqhf89D8FxBvKe3nkCcO5Y8cRuWTHaC5NShCfKyPp4F2OKH3fOX4y6TcJxlXYKacn/Hb86ksAhBlbvzJmsDgS/FBXZtH X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2db99ddb-0504-4817-b4cb-08db1695e46f X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Feb 2023 18:35:24.3720 (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: f1TMcUmg4aHhAfDQsA/+djw8DudgjvS3Un+uqUuZ7CKrPDqSBSJOtcgX0/OeRjX3eyg8H2GJq2rmer8ji9F+ZQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR10MB7004 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-24_14,2023-02-24_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 spamscore=0 adultscore=0 bulkscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302240146 X-Proofpoint-ORIG-GUID: K-krQQUDUagH5koq5shIoRrbAB0vgyos X-Proofpoint-GUID: K-krQQUDUagH5koq5shIoRrbAB0vgyos 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 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758738584262880673?= X-GMAIL-MSGID: =?utf-8?q?1758738584262880673?= on a structure with a C99 flexible array member being nested in another structure. "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: * The structure with a C99 flexible array member 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. * 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, '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 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: * c.opt: New option -Wgnu-variable-sized-type-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 | 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 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= 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 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 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 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 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 +The structure with a C99 flexible array member 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 +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{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 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" } */