]> git.ipfire.org Git - thirdparty/grub.git/commit
build: Fix -Werror=array-bounds array subscript 0 is outside array bounds
authorMichael Chang <mchang@suse.com>
Mon, 28 Mar 2022 07:00:53 +0000 (15:00 +0800)
committerDaniel Kiper <daniel.kiper@oracle.com>
Wed, 20 Apr 2022 16:27:52 +0000 (18:27 +0200)
commitacffb81485e35e1f28152949a1c6e1d4dbf5172e
tree90bb54eaf97ce8f6112d431f65818cff84fe4117
parentbe8eb0eed69f8bc9ac20837eae58e55218011880
build: Fix -Werror=array-bounds array subscript 0 is outside array bounds

The GRUB is failing to build with GCC-12 in many places like this:

  In function 'init_cbfsdisk',
      inlined from 'grub_mod_init' at ../../grub-core/fs/cbfs.c:391:3:
  ../../grub-core/fs/cbfs.c:345:7: error: array subscript 0 is outside array bounds of 'grub_uint32_t[0]' {aka 'unsigned int[]'} [-Werror=array-bounds]
    345 |   ptr = *(grub_uint32_t *) 0xfffffffc;
        |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is caused by GCC regression in 11/12 [1]. In a nut shell, the
warning is about detected invalid accesses at non-zero offsets to NULL
pointers. Since hardwired constant address is treated as NULL plus an
offset in the same underlying code, the warning is therefore triggered.

Instead of inserting #pragma all over the places where literal pointers
are accessed to avoid diagnosing array-bounds, we can try to borrow the
idea from Linux kernel that the absolute_pointer() macro [2][3] is used
to disconnect a pointer using literal address from it's original object,
hence GCC won't be able to make assumptions on the boundary while doing
pointer arithmetic. With that we can greatly reduce the code we have to
cover up by making initial literal pointer assignment to use the new
wrapper but not having to track everywhere literal pointers are
accessed. This also makes code looks cleaner.

Please note the grub_absolute_pointer() macro requires to be invoked in
a function as long as it is compound expression. Some global variables
with literal pointers has been changed to local ones in order to use
grub_absolute_pointer() to initialize it. The shuffling is basically done
in a selective and careful way that the variable's scope doesn't matter
being local or global, for example, the global variable must not get
modified at run time throughout. For the record, here's the list of
global variables got shuffled in this patch:

  grub-core/commands/i386/pc/drivemap.c:int13slot
  grub-core/term/i386/pc/console.c:bios_data_area
  grub-core/term/ns8250.c:serial_hw_io_addr

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578
[2] https://elixir.bootlin.com/linux/v5.16.14/source/include/linux/compiler.h#L180
[3] https://elixir.bootlin.com/linux/v5.16.14/source/include/linux/compiler-gcc.h#L31

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
18 files changed:
grub-core/bus/cs5536.c
grub-core/commands/acpi.c
grub-core/commands/efi/loadbios.c
grub-core/commands/i386/pc/drivemap.c
grub-core/commands/i386/pc/sendkey.c
grub-core/disk/i386/pc/biosdisk.c
grub-core/fs/cbfs.c
grub-core/kern/i386/pc/acpi.c
grub-core/kern/i386/pc/mmap.c
grub-core/loader/i386/multiboot_mbi.c
grub-core/loader/multiboot_mbi2.c
grub-core/mmap/i386/pc/mmap.c
grub-core/net/drivers/i386/pc/pxe.c
grub-core/term/i386/pc/console.c
grub-core/term/i386/pc/vga_text.c
grub-core/term/ns8250.c
grub-core/video/i386/pc/vbe.c
include/grub/types.h