[RFC PATCH] gcc-9: workaround array bounds warning on boot_params cleaning

From: Yauheni Kaliuta
Date: Tue Aug 06 2019 - 12:30:42 EST


The code is supposed to clear several fields in the structure with
memset() and it produces the warning:

In file included from arch/x86/kernel/head64.c:17:
In function âmemsetâ,
inlined from âsanitize_boot_paramsâ at ./arch/x86/include/asm/bootparam_utils.h:40:3,
inlined from âcopy_bootdataâ at arch/x86/kernel/head64.c:391:2:
./include/linux/string.h:344:9: warning: â__builtin_memsetâ offset [197, 448] from the object at âboot_paramsâ is out of the bounds of referenced subobject âext_ramdisk_imageâ with type âunsigned intâ at offset 192 [-Warray-bounds]
344 | return __builtin_memset(p, c, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

since gcc is aware of the field sizes of the structure.

Blind gcc treating the structure as a byte array and calculate
positions with offsetof().

Suggested-by: Ed Kellett <e@xxxxxxxxxx>
Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@xxxxxxxxxx>
---
arch/x86/include/asm/bootparam_utils.h | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/arch/x86/include/asm/bootparam_utils.h b/arch/x86/include/asm/bootparam_utils.h
index 101eb944f13c..16e567a08b54 100644
--- a/arch/x86/include/asm/bootparam_utils.h
+++ b/arch/x86/include/asm/bootparam_utils.h
@@ -37,12 +37,14 @@ static void sanitize_boot_params(struct boot_params *boot_params)
if (boot_params->sentinel) {
/* fields in boot_params are left uninitialized, clear them */
boot_params->acpi_rsdp_addr = 0;
- memset(&boot_params->ext_ramdisk_image, 0,
- (char *)&boot_params->efi_info -
- (char *)&boot_params->ext_ramdisk_image);
- memset(&boot_params->kbd_status, 0,
- (char *)&boot_params->hdr -
- (char *)&boot_params->kbd_status);
+ memset((u8 *)boot_params + offsetof(struct boot_params, ext_ramdisk_image),
+ 0,
+ offsetof(struct boot_params, efi_info) -
+ offsetof(struct boot_params, ext_ramdisk_image));
+ memset((u8 *)boot_params + offsetof(struct boot_params, _pad7[0]),
+ 0,
+ offsetof(struct boot_params, edd_mbr_sig_buffer[0]) -
+ offsetof(struct boot_params, _pad7[0]));
memset(&boot_params->_pad7[0], 0,
(char *)&boot_params->edd_mbr_sig_buffer[0] -
(char *)&boot_params->_pad7[0]);
--
2.22.0