From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jH0UY-0003sW-EB for mharc-grub-devel@gnu.org; Wed, 25 Mar 2020 03:30:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37454) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jH0UU-0003sJ-CP for grub-devel@gnu.org; Wed, 25 Mar 2020 03:30:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jH0UR-0002eW-53 for grub-devel@gnu.org; Wed, 25 Mar 2020 03:30:16 -0400 Received: from m4a0073g.houston.softwaregrp.com ([15.124.2.131]:55439) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1jH0UQ-0002PF-RV for grub-devel@gnu.org; Wed, 25 Mar 2020 03:30:15 -0400 Received: FROM m4a0073g.houston.softwaregrp.com (15.120.17.147) BY m4a0073g.houston.softwaregrp.com WITH ESMTP; Wed, 25 Mar 2020 07:27:41 +0000 Received: from M9W0068.microfocus.com (2002:f79:bf::f79:bf) by M4W0335.microfocus.com (2002:f78:1193::f78:1193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10; Wed, 25 Mar 2020 07:27:40 +0000 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (15.124.72.14) by M9W0068.microfocus.com (15.121.0.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10 via Frontend Transport; Wed, 25 Mar 2020 07:27:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Dwf0BeZqwuRwvwZb2q9aZmz90PU/RzN4kP3uZL7cEGwCGf+5pMsudDo1eUgCumCF4xBo4h1lIlr0LKc58pKuRA3mwW5lDY+f/NOYlr7A8rSeMcMNNEblGJU3pIz9stuie8h1DvA3/NOKGCZxAm4jPZCQSTzI0nxVyWZWCHlSeyBf6QU8sjpVB++hiL0J4DrNDbvYCqp8b0Rf3ZayKzjlk/TMn1QbrGy7vrVKWLy4tx9niFFAKVWKy8pduWToMCKRWPzhSQy93RxV3tLqAzJIYIw3XtEwBxM0j7ipvS/BAk4r5D+kUz8lnXCDZlLnyrgq2qpFGxBhxhIYXmJM4/cGEQ== 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-SenderADCheck; bh=Csc0sTLpAlc9kEA2ZfsW0RlBtEX/NihZYWsGbP0oUTk=; b=bI8vVcUCQWgC0t5OppU8V7jSWiMZQCyzBbfJ9DjCs/BTMbLBh4mFO43jKUcDcPuXFlEhZpaEVZ3mzgxU+LE4/iDZC+dCH8xXfa4qrIai6Zt/5IBwiGH/LG1Ne+/AAHOUm5AH8fyDRD+bDnGjt4pClEIaquF6pZGeLco7MVjBXWhxGXDE1o1lxTTC0/KXLUOzP4YroxrkdfKD/uBzW5mnRPIMsx3MFtiWDZCxuH6SXp/OYFYtwOBp46IHMOcv/hDSdVYz6wN10Pc+pR0p35bFztyx5hgZJRqYO6ld389FqHqaJuvwpZapNoPFN9n6DlbSTAGaSju4AZgjZ8T1OF0HKg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=MChang@suse.com; Received: from BY5PR18MB3140.namprd18.prod.outlook.com (2603:10b6:a03:1a1::17) by BY5PR18MB3186.namprd18.prod.outlook.com (2603:10b6:a03:1af::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2835.22; Wed, 25 Mar 2020 07:27:38 +0000 Received: from BY5PR18MB3140.namprd18.prod.outlook.com ([fe80::a404:f256:fbe8:c2d]) by BY5PR18MB3140.namprd18.prod.outlook.com ([fe80::a404:f256:fbe8:c2d%4]) with mapi id 15.20.2835.023; Wed, 25 Mar 2020 07:27:38 +0000 Date: Wed, 25 Mar 2020 15:27:28 +0800 From: Michael Chang To: The development of GNU GRUB CC: Subject: Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ... Message-ID: <20200325072728.GA13446@mercury> References: <9634719282783168194@scdbackup.webframe.org> Content-Type: multipart/mixed; boundary="mP3DRpeJDSE+ciuQ" Content-Disposition: inline In-Reply-To: <9634719282783168194@scdbackup.webframe.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-ClientProxiedBy: LO2P265CA0230.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:b::26) To BY5PR18MB3140.namprd18.prod.outlook.com (2603:10b6:a03:1a1::17) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from mercury (36.226.146.226) by LO2P265CA0230.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:b::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2835.20 via Frontend Transport; Wed, 25 Mar 2020 07:27:35 +0000 X-Originating-IP: [36.226.146.226] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cdaf278e-2434-4899-1852-08d7d08dfe90 X-MS-TrafficTypeDiagnostic: BY5PR18MB3186: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-Forefront-PRVS: 0353563E2B X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4636009)(136003)(366004)(396003)(346002)(376002)(39860400002)(2906002)(4326008)(66946007)(66476007)(9686003)(66576008)(81156014)(956004)(8936002)(81166006)(66556008)(86362001)(8676002)(33716001)(26005)(235185007)(5660300002)(33656002)(16526019)(186003)(9576002)(6496006)(478600001)(1076003)(44144004)(55016002)(52116002)(6666004)(966005)(316002)(6916009)(2700100001); DIR:OUT; SFP:1102; SCL:1; SRVR:BY5PR18MB3186; H:BY5PR18MB3140.namprd18.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; Received-SPF: None (protection.outlook.com: suse.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XWcdjd55yyaL8hTCStKP9GaJdCo9Fe95WPlXfpwKcHAr+xArtSXjQqO1d7hocy5dlWcEvOa97uHeKsqfUFV5twUK7O7RREn300pmP2jlt54diEDCLWslLzk+fuwpB3JXPDig5RkwmVRh0A0WMDklDf0O7WeKHLACthLuprCPD5OjYUAhZ2KHvzC/TH3tqfXID6USO+sTboVLynZV8TUHeDiFGBUgnM6wE5FtF5w8jdBRxIxwx8v6hPzQ5YeOhETr4/AiEp/pBVq40L8Ij1F3C2QAS8lBOrBZTwHWcz7pG6fuvwIQniavkI4uV0tYWnVIknazyMvgv6PYcPFBGfJaGmcTytzQMib+fmcIGxzwqDXX2gMX7IWBIsCgevU0s1m0VVyzP75OHOd5jMGfZ7q/j9e9P3MHRZ3KrAwq6nQqwzyaOuHkUtr42Izse2HZVSRMl+firQgLP0pvqjhH85NbEVFpdbeVJG6KgxRu/nkmhn+acnKdx98HdjANZNBifli9Lw4ZXUUrO9Jm3BSBLiNWm5NgArRmUq8YLlwN1zCvo33wR5kSxrvtY1f/TKF89595SteNBdBOnnXncT4p546QtQ== X-MS-Exchange-AntiSpam-MessageData: lg5CYPOYIvHcNV4az2bssktHCoKNrXEDJ5JfOEGcrfiNFsxQgvYV+HAXXJA3HvLmwxWm/sU4F3J+qCzunF3USrQScXNgkBRFM8F0Xcw+U19fqecadtcZ5CAzaEbV/dHlEGmJIAu10/MFU2lGo+11nQ== X-MS-Exchange-CrossTenant-Network-Message-Id: cdaf278e-2434-4899-1852-08d7d08dfe90 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Mar 2020 07:27:37.9285 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 856b813c-16e5-49a5-85ec-6f081e13b527 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9adCaE5S9W7TX7JDQVdGmcvHj4CYLHDNYVwSIuzGOUz08ppDkRm70HGdXvcyZ01z X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR18MB3186 X-OriginatorOrg: suse.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 15.124.2.131 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Mar 2020 07:30:20 -0000 --mP3DRpeJDSE+ciuQ Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline On Tue, Mar 24, 2020 at 06:54:43PM +0100, Thomas Schmitt wrote: > Hi, > > i wrote: > > > (char *) &sb.dev_roles - (char *) sb > > > + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t) > > PGNet Dev wrote: > > grub-core/disk/mdraid1x_linux.c:183:6: error: cannot convert to a > > pointer type > > My fault. I forgot the "&" before "sb". > > (char *) &sb.dev_roles - (char *) &sb > > I invested time in examining the C riddle, not in testing my proposal > by at least some dummy. > > Now this compiles for me without complaint by gcc -Wall > > struct { > char a_array[10]; > uint16_t dev_roles[0]; > } sb; > > printf("%u\n", (unsigned int) (((char *) &sb.dev_roles - (char *) &sb) > + 2 * sizeof(uint16_t))); > > Running this program yields 14 as result. The same as with the equivalent > of the old expression > > printf("%u\n", (unsigned int) ((char *) &sb.dev_roles[2] - (char *) &sb)); > > > > not sure I'm reading your intent from your post, > > My observation is that not "dev_roles[0]" is to blame for the warning, but > rather the computation which involves taking the address of an array > element while not a single one is allocated. > The resulting number is used as offset in a file, not in the sparsely > allocated "struct grub_raid_super_1x sb". > > My proposal is to avoid "[...]" in the course of the computation. > This should be valid for both ways to express an open ended struct: > "dev_roles[0]" and "dev_roles[]". I am also investigating the GCC 10 build problems, and my conclusion mostly coincided with yours. There we can replce the offset computation with pointer arithmetic like this. (char *) (sb.dev_roles + grub_le_to_cpu32 (sb.dev_number)) - (char *) &sb, Besides, there is also build error in zfs that I managed to come up with a patch as well. I attached both my patch here for your refernce. It would be great if you can help to test patch to solve the build problem for gcc-10 in your system or not. Thanks, Michael > > > Have a nice day :) > > Thomas > > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel --mP3DRpeJDSE+ciuQ Content-Type: text/x-patch; charset="us-ascii" Content-Disposition: attachment; filename="0001-mdraid1x_linux-Fix-gcc10-error-Werror-array-bounds.patch" >From 19f55c05ea592b1339ee0d503c86be349447553f Mon Sep 17 00:00:00 2001 From: Michael Chang Date: Wed, 25 Mar 2020 13:52:51 +0800 Subject: [PATCH 1/2] mdraid1x_linux: Fix gcc10 error -Werror=array-bounds We bumped into the build error while testing gcc-10 pre-release. ../../grub-core/disk/mdraid1x_linux.c: In function 'grub_mdraid_detect': ../../grub-core/disk/mdraid1x_linux.c:181:15: error: array subscript is outside array bounds of 'grub_uint16_t[0]' {aka 'short unsigned int[0]'} [-Werror=array-bounds] 181 | (char *) &sb.dev_roles[grub_le_to_cpu32 (sb.dev_number)] | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../grub-core/disk/mdraid1x_linux.c:98:17: note: while referencing 'dev_roles' 98 | grub_uint16_t dev_roles[0]; /* Role in array, or 0xffff for a spare, or 0xfffe for faulty. */ | ^~~~~~~~~ ../../grub-core/disk/mdraid1x_linux.c:127:33: note: defined here 'sb' 127 | struct grub_raid_super_1x sb; | ^~ cc1: all warnings being treated as errors Apparently gcc issues the warning when trying to access sb.dev_roles array's member, since it is a zero length array as the last element of struct grub_raid_super_1x that is allocated sparsely without extra chunks for the trailing bits, so the warning looks legitimate in this regard. As the whole thing here is doing offset computation, it is undue to use syntax that would imply array member access then take address from it later. Instead we could accomplish the same thing through basic array pointer arithmetic to pacify the warning. Signed-off-by: Michael Chang --- grub-core/disk/mdraid1x_linux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grub-core/disk/mdraid1x_linux.c b/grub-core/disk/mdraid1x_linux.c index 7cc80d3df..c980feba4 100644 --- a/grub-core/disk/mdraid1x_linux.c +++ b/grub-core/disk/mdraid1x_linux.c @@ -178,7 +178,7 @@ grub_mdraid_detect (grub_disk_t disk, return NULL; if (grub_disk_read (disk, sector, - (char *) &sb.dev_roles[grub_le_to_cpu32 (sb.dev_number)] + (char *) (sb.dev_roles + grub_le_to_cpu32 (sb.dev_number)) - (char *) &sb, sizeof (role), &role)) return NULL; -- 2.16.4 --mP3DRpeJDSE+ciuQ Content-Type: text/x-patch; charset="us-ascii" Content-Disposition: attachment; filename="0002-zfs-Fix-gcc10-error-Werror-zero-length-bounds.patch" >From 8666468ac1a35f0678672de5c89a3f062d1aeb39 Mon Sep 17 00:00:00 2001 From: Michael Chang Date: Wed, 25 Mar 2020 14:28:15 +0800 Subject: [PATCH 2/2] zfs: Fix gcc10 error -Werror=zero-length-bounds We bumped into the build error while testing gcc-10 pre-release. In file included from ../../include/grub/file.h:22, from ../../grub-core/fs/zfs/zfs.c:34: ../../grub-core/fs/zfs/zfs.c: In function 'zap_leaf_lookup': ../../grub-core/fs/zfs/zfs.c:2263:44: error: array subscript '' is outside the bounds of an interior zero-length array 'grub_uint16_t[0]' {aka 'short unsigned int[0]'} [-Werror=zero-length-bounds] 2263 | for (chunk = grub_zfs_to_cpu16 (l->l_hash[LEAF_HASH (blksft, h, l)], endian); ../../include/grub/types.h:241:48: note: in definition of macro 'grub_le_to_cpu16' 241 | # define grub_le_to_cpu16(x) ((grub_uint16_t) (x)) | ^ ../../grub-core/fs/zfs/zfs.c:2263:16: note: in expansion of macro 'grub_zfs_to_cpu16' 2263 | for (chunk = grub_zfs_to_cpu16 (l->l_hash[LEAF_HASH (blksft, h, l)], endian); | ^~~~~~~~~~~~~~~~~ In file included from ../../grub-core/fs/zfs/zfs.c:48: ../../include/grub/zfs/zap_leaf.h:72:16: note: while referencing 'l_hash' 72 | grub_uint16_t l_hash[0]; | ^~~~~~ Here I'd like to quote from the gcc document [1] which seems to be best to explain what is going on here. "Declaring zero-length arrays in other contexts, including as interior members of structure objects or as non-member objects, is discouraged. Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed." The l_hash[0] is apparnetly an interior member to the enclosed structure while l_entries[0] is the trailing member. And the offending code tries to access members in l_hash[0] array that triggers the diagnose. Given that the l_entries[0] is used to get proper alignment to access leaf chunks, we can accomplish the same thing through the ALIGN_UP macro thus eliminating l_entries[0] from the structure. In this way we can pacify the warning as l_hash[0] now becomes the last member to the enclosed structure. [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html Signed-off-by: Michael Chang --- grub-core/fs/zfs/zfs.c | 7 ++++--- include/grub/zfs/zap_leaf.h | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c index 2f72e42bf..f38f5b102 100644 --- a/grub-core/fs/zfs/zfs.c +++ b/grub-core/fs/zfs/zfs.c @@ -141,9 +141,10 @@ ZAP_LEAF_NUMCHUNKS (int bs) static inline zap_leaf_chunk_t * ZAP_LEAF_CHUNK (zap_leaf_phys_t *l, int bs, int idx) { - return &((zap_leaf_chunk_t *) (l->l_entries - + (ZAP_LEAF_HASH_NUMENTRIES(bs) * 2) - / sizeof (grub_properly_aligned_t)))[idx]; + grub_properly_aligned_t *l_entries; + + l_entries = (grub_properly_aligned_t *) ALIGN_UP((grub_addr_t)l->l_hash, sizeof (grub_properly_aligned_t)); + return &((zap_leaf_chunk_t *) (l_entries + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx]; } static inline struct zap_leaf_entry * diff --git a/include/grub/zfs/zap_leaf.h b/include/grub/zfs/zap_leaf.h index 95c67dcba..11447c166 100644 --- a/include/grub/zfs/zap_leaf.h +++ b/include/grub/zfs/zap_leaf.h @@ -70,7 +70,6 @@ typedef struct zap_leaf_phys { */ grub_uint16_t l_hash[0]; - grub_properly_aligned_t l_entries[0]; } zap_leaf_phys_t; typedef union zap_leaf_chunk { -- 2.16.4 --mP3DRpeJDSE+ciuQ--