Skip to contentSkip to conversation

grub/head build with pre-release GCC10 ; fail @ "grub-core/disk/mdraid1x_linux.c:181:15: error: ..."

DISCUSSION PGNet Dev 5 participants 17 messages Mar 24, 2020 – Mar 26, 2020

lore
Share & tools
lore

PGNet Dev

author
#0 lore raw

 building curren head,

	git clone https://git.savannah.gnu.org/git/grub.git
	cd grub
	git describe
		grub-2.04-71-g552c9fd08

with soon(ish)-to-be-release GCC10

	gcc --version
		gcc (SUSE Linux) 10.0.1 20200320 (experimental) [revision 7d4549b2cd209eb621453ce13be7ffd84ffa720a]

config's OK

unset CC CPP
./bootstrap
./autogen.sh
./configure

build fails,

make V=1

	...
	gcc -DHAVE_CONFIG_H -I.  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE=\"grub-core/disk/mdraid1x_linux.c\" -I. -I. -I. -I. -I./include -I./include -I./grub-core/lib/libgcrypt-grub/src/  -I./grub-core/lib/minilzo -I./grub-core/lib/xzembed -I./grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -Werror  -fno-builtin -Wno-undef -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -MT grub-core/disk/libgrubmods_a-mdraid1x_linux.o -MD -MP -MF grub-core/disk/.deps-util/libgrubmods_a-mdraid1x_linux.Tpo -c -o grub-core/disk/libgrubmods_a-mdraid1x_linux.o `test -f 'grub-core/disk/mdraid1x_linux.c' || echo './'`grub-core/disk/mdraid1x_linux.c
	grub-core/disk/mdraid1x_linux.c: In function ‘grub_mdraid_detect’:
	grub-core/disk/mdraid1x_linux.c:181:15: error: array subscript <unknown> 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
	make[2]: *** [Makefile:7059: grub-core/disk/libgrubmods_a-mdraid1x_linux.o] Error 1
	make[2]: Leaving directory '/usr/local/src/grub'
	make[1]: *** [Makefile:11920: all-recursive] Error 1
	make[1]: Leaving directory '/usr/local/src/grub'
	make: *** [Makefile:3772: all] Error 2

known/expected issue? bug-worthy? or still too early for GCC10 issues?

author
#2 lore raw

Thomas,

Quoted 4 lines · Paul Menzel

Dear PGNet Dev,

Already reported and analyzed [1]. It’d be nice, if you could verify Thomas’ analysis.

not sure I'm reading your intent from your post,

Quoted 7 lines
	...
	I think that the following expression produces the same number without
	virtual access to a virtual array member:

	         (char *) &sb.dev_roles - (char *) sb
	         + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t)
	...

with this,

	diff -ur grub.ORIG/grub-core/disk/mdraid1x_linux.c grub/grub-core/disk/mdraid1x_linux.c
	--- grub.ORIG/grub-core/disk/mdraid1x_linux.c	2020-03-24 09:24:08.656640265 -0700
	+++ grub/grub-core/disk/mdraid1x_linux.c	2020-03-24 09:25:20.615871693 -0700
	@@ -178,8 +178,9 @@
		return NULL;
	 
	       if (grub_disk_read (disk, sector, 
	-			  (char *) &sb.dev_roles[grub_le_to_cpu32 (sb.dev_number)]
	-			  - (char *) &sb,
	+			  (char *) &sb.dev_roles
	+			  - (char *) sb
	+			  + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t),
				  sizeof (role), &role))
		return NULL;

I'm seeing,

	...
	gcc -DHAVE_CONFIG_H -I.  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE=\"grub-core/disk/mdraid1x_linux.c\" -I. -I. -I. -I. -I./include -I./include -I./grub-core/lib/libgcrypt-grub/src/  -I./grub-core/lib/minilzo -I./grub-core/lib/xzembed -I./grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -Werror  -fno-builtin -Wno-undef -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -MT grub-core/disk/libgrubmods_a-mdraid1x_linux.o -MD -MP -MF grub-core/disk/.deps-util/libgrubmods_a-mdraid1x_linux.Tpo -c -o grub-core/disk/libgrubmods_a-mdraid1x_linux.o `test -f 'grub-core/disk/mdraid1x_linux.c' || echo './'`grub-core/disk/mdraid1x_linux.c
	grub-core/disk/mdraid1x_linux.c: In function ‘grub_mdraid_detect’:
	grub-core/disk/mdraid1x_linux.c:183:6: error: cannot convert to a pointer type
	  183 |      + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t),
	      |      ^
	make[2]: *** [Makefile:7059: grub-core/disk/libgrubmods_a-mdraid1x_linux.o] Error 1

my patch attempt wrong? or still needs tweaks?

author
#3 lore raw

my bad re: patch. pebkac (looking UP might help :-/ )

with

	diff -ur grub.ORIG/grub-core/disk/mdraid1x_linux.c grub/grub-core/disk/mdraid1x_linux.c
	--- grub.ORIG/grub-core/disk/mdraid1x_linux.c	2020-03-24 09:24:08.656640265 -0700
	+++ grub/grub-core/disk/mdraid1x_linux.c	2020-03-24 09:58:27.638619782 -0700
	@@ -179,7 +179,8 @@
	 
	       if (grub_disk_read (disk, sector, 
				  (char *) &sb.dev_roles[grub_le_to_cpu32 (sb.dev_number)]
	-			  - (char *) &sb,
	+			  ((char *) &sb.dev_roles - (char *) sb)
	+			  + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t),
				  sizeof (role), &role))
		return NULL;

gcc10 build returns,

	...
	make
		...
		grub-core/disk/mdraid1x_linux.c: In function ‘grub_mdraid_detect’:
		grub-core/disk/mdraid1x_linux.c:182:6: error: cannot convert to a pointer type
		  182 |      ((char *) &sb.dev_roles - (char *) sb)
		      |      ^
		grub-core/disk/mdraid1x_linux.c:181:16: error: called object is not a function or function pointer
		  181 |      (char *) &sb.dev_roles[grub_le_to_cpu32 (sb.dev_number)]
		      |                ^~
		make[2]: *** [Makefile:7059: grub-core/disk/libgrubmods_a-mdraid1x_linux.o] Error 1
		make[2]: Leaving directory '/usr/local/src/grub'
		make[1]: *** [Makefile:11920: all-recursive] Error 1
		make[1]: Leaving directory '/usr/local/src/grub'
		make: *** [Makefile:3772: all] Error 2
#4 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to PGNet Dev

Hi,

Quoted 2 lines · i wrote:
Quoted 2 lines

(char *) &sb.dev_roles - (char *) sb
+ grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t)

Quoted 2 lines · PGNet Dev

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));

Quoted 1 line

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[]".

Have a nice day :)

Thomas

#5 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Quoted 41 lines · Thomas Schmitt

Hi,

Quoted 2 lines · i wrote:
Quoted 2 lines

(char *) &sb.dev_roles - (char *) sb
+ grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t)

Quoted 2 lines · PGNet Dev

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));

Quoted 1 line

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".

Quoted 3 lines

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

Quoted 11 lines

Have a nice day :)

Thomas

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

From 19f55c05ea592b1339ee0d503c86be349447553f Mon Sep 17 00:00:00 2001
From: Michael Chang <[email protected]>
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 <unknown> 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.

---
 grub-core/disk/mdraid1x_linux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/mdraid1x_linux.c +1 -1
index 7cc80d3df..c980feba4 100644
@@ -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;
Signature · 1 line
2.16.4

From 8666468ac1a35f0678672de5c89a3f062d1aeb39 Mon Sep 17 00:00:00 2001
From: Michael Chang <[email protected]>
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 '<unknown>' 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

---
 grub-core/fs/zfs/zfs.c      | 7 ++++---
 include/grub/zfs/zap_leaf.h | 1 -
 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/fs/zfs/zfs.c +4 -3
index 2f72e42bf..f38f5b102 100644
@@ -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 *
include/grub/zfs/zap_leaf.h +0 -1
index 95c67dcba..11447c166 100644
@@ -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 {
Signature · 1 line
2.16.4
#6 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Hi,

Michael Chang wrote in patch 2/2:

Quoted 6 lines

../../grub-core/fs/zfs/zfs.c:2263:44: error: array subscript '<unknown>' is outside the bounds of an interior zero-length array 'grub_uint16_t[0]' {aka 'short unsigned int[0]'} [-Werror=zero-length-bounds]
[...]
The l_hash[0] is apparnetly an interior member to the enclosed structure [...] the l_entries[0] is used to get proper alignment to access leaf chunks

That's what i call a dirty hack.

I wonder what gcc would say to a union of [0]-sized arrays as last member of the struct type:

  struct typedef struct zap_leaf_phys {
        ...
        union {
                grub_uint16_t l_hash[0];
                grub_properly_aligned_t l_entries[0];
        } l_;
  } zap_leaf_phys_t;

Mine likes such a gesture, but it is way behind in respect to modern bitrot.

So in the end, your patch looks like the solid and unambiguous way to implement what is desired.

Quoted 2 lines

It would be great if you can help to test patch to solve the build problem for gcc-10 in your system or not.

Due to lack of modernity i can only contribute statements that the concepts and motivations of your two patches look good to me.

(I also lack the occasion to test the two use cases.
Just lurking here for grub-mkrescue issues, where i provide the last stage of packing up the ISO image.)

Have a nice day :)

Thomas

author
#8 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to Michael Chang
Quoted 2 lines · Michael Chang

It would be great if you can help to test patch to solve the build problem for gcc-10 in your system or not.

with your patches, no immediate mdraid1x or zfs build errors one does surface, now, for ntfscomp ...

gcc --version

	gcc (SUSE Linux) 10.0.1 20200320 (experimental) [revision 7d4549b2cd209eb621453ce13be7ffd84ffa720a]

cd grub
git log -n1

      1 commit 552c9fd08122a3036c724ce96dfe68aa2f75705f (HEAD -> master, origin/master, origin/HEAD)
      2 Author: Patrick Steinhardt <[email protected]>
      3 Date:   Sat Mar 7 17:29:09 2020 +0100

patch -p1 < /tmp/grub_patches/0001-mdraid1x_linux-Fix-gcc10-error-Werror-array-bounds.patch
patching file grub-core/disk/mdraid1x_linux.c
patch -p1 < /tmp/grub_patches/0002-zfs-Fix-gcc10-error-Werror-zero-length-bounds.patch
patching file grub-core/fs/zfs/zfs.c
patching file include/grub/zfs/zap_leaf.h

unset CC CPP
./bootstrap
./autogen.sh
./configure
make V=1 -j${CORES}

	...
	make[2]: Entering directory '/usr/local/src/grub'
	gcc -DHAVE_CONFIG_H -I.  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE=\"grub-core/fs/ntfscomp.c\" -I. -I. -I. -I. -I./include -I./include -I./grub-core/lib/libgcrypt-grub/src/  -I./grub-core/lib/minilzo -I./grub-core/lib/xzembed -I./grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -Werror  -fno-builtin -Wno-undef -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -MT grub-core/fs/libgrubmods_a-ntfscomp.o -MD -MP -MF grub-core/fs/.deps-util/libgrubmods_a-ntfscomp.Tpo -c -o grub-core/fs/libgrubmods_a-ntfscomp.o `test -f 'grub-core/fs/ntfscomp.c' || echo './'`grub-core/fs/ntfscomp.c
	grub-core/fs/ntfscomp.c: In function ‘read_block’:
	grub-core/fs/ntfscomp.c:82:11: error: ‘flg’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
	   82 |       if (flg & 0x8000)
	      |           ^~~
	grub-core/fs/ntfscomp.c:74:17: note: ‘flg’ was declared here
	   74 |   grub_uint16_t flg, cnt;
	      |                 ^~~
	cc1: all warnings being treated as errors
	make[2]: *** [Makefile:7647: grub-core/fs/libgrubmods_a-ntfscomp.o] Error 1
	make[2]: Leaving directory '/usr/local/src/grub'
	make[1]: *** [Makefile:11920: all-recursive] Error 1
	make[1]: Leaving directory '/usr/local/src/grub'
	make: *** [Makefile:3772: all] Error 2
#9 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Dear PGNet Dev,

Am 25.03.20 um 16:35 schrieb PGNet Dev:

[…]

Quoted 2 lines

with your patches, no immediate mdraid1x or zfs build errors one does surface, now, for ntfscomp ...

[…]

Thanks, but please follow the mailing list netiquette, and start a new discussion thread with a descriptive subject line.

Kind regards,

Paul

author
#10 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Quoted 1 line · Paul Menzel

Thanks, but please follow the mailing list netiquette

I was responding to a question asked by a developer in THIS thread

#11 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Dear PGNet Dev,

Am 25.03.20 um 16:54 schrieb PGNet Dev:

Quoted 4 lines · Paul Menzel
Quoted 1 line

Thanks, but please follow the mailing list netiquette

I was responding to a question asked by a developer in THIS thread

Yes, the question was, if the reported(?) build issues were fixed, and you replied to them.

But, you new build error, which I quoted, should be discussed and fixed in a separate thread with the correct descriptive subject line.

Kind regards,

Paul

Reviewed-by
#14 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to Michael Chang
Quoted 98 lines · Michael Chang
Quoted 41 lines · Thomas Schmitt

Hi,

Quoted 2 lines · i wrote:
Quoted 2 lines

(char *) &sb.dev_roles - (char *) sb
+ grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t)

Quoted 2 lines · PGNet Dev

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));

Quoted 1 line

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".

Quoted 3 lines

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

Quoted 7 lines

Have a nice day :)

Thomas

Quoted 6 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

From 19f55c05ea592b1339ee0d503c86be349447553f Mon Sep 17 00:00:00 2001

From: Michael Chang <[email protected]>
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 <unknown> 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.

Quoted 6 lines

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.

Quoted 75 lines
---
 grub-core/disk/mdraid1x_linux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/mdraid1x_linux.c +1 -2
index 7cc80d3df..c980feba4 100644
@@ -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

Quoted 1 line

From 8666468ac1a35f0678672de5c89a3f062d1aeb39 Mon Sep 17 00:00:00 2001

From: Michael Chang <[email protected]>
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 '<unknown>' 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

---
 grub-core/fs/zfs/zfs.c      | 7 ++++---
 include/grub/zfs/zap_leaf.h | 1 -
 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/fs/zfs/zfs.c +0 -0
index 2f72e42bf..f38f5b102 100644

grub-core/fs/zfs/zfs.c ZAP_LEAF_NUMCHUNKS (int bs) · line 147

Quoted 10 lines
grub-core/fs/zfs/zfs.c +4 -3
@@ -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];

Why are you skipping "* 2) / sizeof (grub_properly_aligned_t)" here?

And could you add following excerpt from [1] to the commit message:
Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type. The alignment of a zero-length array is the same as the alignment of its elements.

Quoted 16 lines
grub-core/fs/zfs/zfs.c +0 -0
 }
 
 static inline struct zap_leaf_entry *
include/grub/zfs/zap_leaf.h +0 -2
index 95c67dcba..11447c166 100644
@@ -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 {
--
Quoted 2 lines

2.16.4

May I ask you to repost the patches in the proper format and CC all people involved in the discussion?

Daniel

#7 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to Thomas Schmitt
Quoted 15 lines · Thomas Schmitt

Hi,

Michael Chang wrote in patch 2/2:

Quoted 6 lines

../../grub-core/fs/zfs/zfs.c:2263:44: error: array subscript '<unknown>' is outside the bounds of an interior zero-length array 'grub_uint16_t[0]' {aka 'short unsigned int[0]'} [-Werror=zero-length-bounds]
[...]
The l_hash[0] is apparnetly an interior member to the enclosed structure [...] the l_entries[0] is used to get proper alignment to access leaf chunks

That's what i call a dirty hack.

I wonder what gcc would say to a union of [0]-sized arrays as last member of the struct type:

Quoted 7 lines
  struct typedef struct zap_leaf_phys {
        ...
        union {
                grub_uint16_t l_hash[0];
                grub_properly_aligned_t l_entries[0];
        } l_;
  } zap_leaf_phys_t;

As far as I know the union will store different types in same memory location, thus the allocation is determined by the largest member in the union, so does the padding has to meet the alignment requirement by the largest member.

So in this case l_hash[0] (2 bytes) will be aligned to grub_properly_aligned_t (8 bytes) and for the result is desired or not really depends on what you want to achieve.

Quoted 2 lines

Mine likes such a gesture, but it is way behind in respect to modern bitrot.

Admittedly that would produce much more readable code, but it is just not work for the extraordinary case here because of specific requirment to the alignment.

Quoted 9 lines

So in the end, your patch looks like the solid and unambiguous way to implement what is desired.

Quoted 2 lines

It would be great if you can help to test patch to solve the build problem for gcc-10 in your system or not.

Quoted 6 lines

Due to lack of modernity i can only contribute statements that the concepts and motivations of your two patches look good to me.

(I also lack the occasion to test the two use cases.
Just lurking here for grub-mkrescue issues, where i provide the last stage of packing up the ISO image.)

The grub-mkrescue is a gem, it never disappoints me whenever I need it. :)

Thanks a lot for your feedback.

Michael

Quoted 11 lines

Have a nice day :)

Thomas

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to PGNet Dev
Quoted 5 lines · PGNet Dev
Quoted 2 lines · Michael Chang

It would be great if you can help to test patch to solve the build problem for gcc-10 in your system or not.

with your patches, no immediate mdraid1x or zfs build errors

Thanks a lot for your test and validation.

Quoted 1 line

one does surface, now, for ntfscomp ...

I am also using openSUSE, somehow I didn't have the ntfscomp build error on my gcc-10 build test. :(

Would you mind to start a new thread for it ?

Thanks,
Michael

Quoted 37 lines

gcc --version

	gcc (SUSE Linux) 10.0.1 20200320 (experimental) [revision 7d4549b2cd209eb621453ce13be7ffd84ffa720a]

cd grub
git log -n1

      1 commit 552c9fd08122a3036c724ce96dfe68aa2f75705f (HEAD -> master, origin/master, origin/HEAD)
      2 Author: Patrick Steinhardt <[email protected]>
      3 Date:   Sat Mar 7 17:29:09 2020 +0100

patch -p1 < /tmp/grub_patches/0001-mdraid1x_linux-Fix-gcc10-error-Werror-array-bounds.patch
patching file grub-core/disk/mdraid1x_linux.c
patch -p1 < /tmp/grub_patches/0002-zfs-Fix-gcc10-error-Werror-zero-length-bounds.patch
patching file grub-core/fs/zfs/zfs.c
patching file include/grub/zfs/zap_leaf.h

unset CC CPP
./bootstrap
./autogen.sh
./configure
make V=1 -j${CORES}

	...
	make[2]: Entering directory '/usr/local/src/grub'
	gcc -DHAVE_CONFIG_H -I.  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE=\"grub-core/fs/ntfscomp.c\" -I. -I. -I. -I. -I./include -I./include -I./grub-core/lib/libgcrypt-grub/src/  -I./grub-core/lib/minilzo -I./grub-core/lib/xzembed -I./grub-core/lib/zstd -DMINILZO_HAVE_CONFIG_H -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -Werror  -fno-builtin -Wno-undef -O3 -Wall -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fmessage-length=0 -grecord-gcc-switches -march=native -mtune=native -MT grub-core/fs/libgrubmods_a-ntfscomp.o -MD -MP -MF grub-core/fs/.deps-util/libgrubmods_a-ntfscomp.Tpo -c -o grub-core/fs/libgrubmods_a-ntfscomp.o `test -f 'grub-core/fs/ntfscomp.c' || echo './'`grub-core/fs/ntfscomp.c
	grub-core/fs/ntfscomp.c: In function ‘read_block’:
	grub-core/fs/ntfscomp.c:82:11: error: ‘flg’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
	   82 |       if (flg & 0x8000)
	      |           ^~~
	grub-core/fs/ntfscomp.c:74:17: note: ‘flg’ was declared here
	   74 |   grub_uint16_t flg, cnt;
	      |                 ^~~
	cc1: all warnings being treated as errors
	make[2]: *** [Makefile:7647: grub-core/fs/libgrubmods_a-ntfscomp.o] Error 1
	make[2]: Leaving directory '/usr/local/src/grub'
	make[1]: *** [Makefile:11920: all-recursive] Error 1
	make[1]: Leaving directory '/usr/local/src/grub'
	make: *** [Makefile:3772: all] Error 2
author
#13 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Quoted 6 lines · Michael Chang
Quoted 1 line

with your patches, no immediate mdraid1x or zfs build errors

Thanks a lot for your test and validation.

Quoted 1 line

one does surface, now, for ntfscomp ...

Quoted 7 lines

I am also using openSUSE, somehow I didn't have the ntfscomp build error on my gcc-10 build test. :(

Would you mind to start a new thread for it ?

Thanks,
Michael

done

--> https://lists.gnu.org/archive/html/grub-devel/2020-03/msg00240.html

o/

#15 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

In reply to Daniel Kiper
Quoted 2 lines · Daniel Kiper
Quoted 1 line · Michael Chang

On Tue, Mar 24, 2020 at 06:54:43PM +0100, Thomas Schmitt wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 1 line

From 8666468ac1a35f0678672de5c89a3f062d1aeb39 Mon Sep 17 00:00:00 2001

From: Michael Chang <[email protected]>
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 '<unknown>' 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

---
 grub-core/fs/zfs/zfs.c      | 7 ++++---
 include/grub/zfs/zap_leaf.h | 1 -
 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/fs/zfs/zfs.c +0 -1
index 2f72e42bf..f38f5b102 100644
@@ -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
Quoted 8 lines

grub-core/fs/zfs/zfs.c ZAP_LEAF_NUMCHUNKS (int bs) · line 147

Quoted 6 lines
grub-core/fs/zfs/zfs.c +4 -2
-				 + (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];

Why are you skipping "* 2) / sizeof (grub_properly_aligned_t)" here?

It is based on this comment before the function.

"The chunks start immediately after the hash table. The end of the hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a chunk_t."

I suppose the magic number "2" could be "sizeof (l->l_hash[0])", so the computatio1n is likely to get number of entries for l->l_entries[] from which we can take address as the chunk start. And since it is indexed by type grub_properly_aligned_t, the alignment is automagically satisfied.

Quoted 8 lines

And could you add following excerpt from [1] to the commit message:
Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type. The alignment of a zero-length array is the same as the alignment of its elements.

OK. I will do so.

Quoted 15 lines
Quoted 15 lines
grub-core/fs/zfs/zfs.c +0 -0
 }
 
 static inline struct zap_leaf_entry *
include/grub/zfs/zap_leaf.h +0 -1
index 95c67dcba..11447c166 100644
@@ -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;
 
Quoted 7 lines
Quoted 4 lines
include/grub/zfs/zap_leaf.h +0 -1
 typedef union zap_leaf_chunk {
--

2.16.4

May I ask you to repost the patches in the proper format and CC all people involved in the discussion?

OK. I will.

Thanks,
Michael

Quoted 7 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#16 lore raw

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

Quoted 64 lines · Michael Chang
Quoted 2 lines · Daniel Kiper
Quoted 1 line · Michael Chang

On Tue, Mar 24, 2020 at 06:54:43PM +0100, Thomas Schmitt wrote:

[snip]

Quoted 58 lines
Quoted 58 lines
Quoted 1 line

From 8666468ac1a35f0678672de5c89a3f062d1aeb39 Mon Sep 17 00:00:00 2001

From: Michael Chang <[email protected]>
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 '<unknown>' 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

---
 grub-core/fs/zfs/zfs.c      | 7 ++++---
 include/grub/zfs/zap_leaf.h | 1 -
 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/fs/zfs/zfs.c +0 -1
index 2f72e42bf..f38f5b102 100644
@@ -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
Quoted 8 lines
Quoted 8 lines

grub-core/fs/zfs/zfs.c ZAP_LEAF_NUMCHUNKS (int bs) · line 147

Quoted 6 lines
grub-core/fs/zfs/zfs.c +4 -2
-				 + (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];

Why are you skipping "* 2) / sizeof (grub_properly_aligned_t)" here?

Ah, indeed I had mistake here. It has to take "HASH_NUMENTRIES * 2) / sizeof (grub_properly_aligned_t)" for computing the entry number of l_entries from given HASH_NUMENTRIES.

I will fix that and repost patches.

Thanks,
Michael

Quoted 59 lines

It is based on this comment before the function.

"The chunks start immediately after the hash table. The end of the hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a chunk_t."

I suppose the magic number "2" could be "sizeof (l->l_hash[0])", so the computatio1n is likely to get number of entries for l->l_entries[] from which we can take address as the chunk start. And since it is indexed by type grub_properly_aligned_t, the alignment is automagically satisfied.

Quoted 8 lines

And could you add following excerpt from [1] to the commit message:
Although the size of a zero-length array is zero, an array member of this kind may increase the size of the enclosing type as a result of tail padding. The offset of a zero-length array member from the beginning of the enclosing structure is the same as the offset of an array with one or more elements of the same type. The alignment of a zero-length array is the same as the alignment of its elements.

OK. I will do so.

Quoted 15 lines
Quoted 15 lines
grub-core/fs/zfs/zfs.c +0 -0
 }
 
 static inline struct zap_leaf_entry *
include/grub/zfs/zap_leaf.h +0 -1
index 95c67dcba..11447c166 100644
@@ -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;
 
Quoted 7 lines
Quoted 4 lines
include/grub/zfs/zap_leaf.h +0 -1
 typedef union zap_leaf_chunk {
--

2.16.4

May I ask you to repost the patches in the proper format and CC all people involved in the discussion?

OK. I will.

Thanks,
Michael

Quoted 7 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel