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...@kellett.im>
Signed-off-by: Yauheni Kaliuta <yauheni.kali...@redhat.com>
---
 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

Reply via email to