All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member
@ 2019-04-11  9:14 Michael Chang
  2019-04-11  9:14 ` [PATCH v2 1/8] cpio: disable gcc9 -Waddress-of-packed-member Michael Chang
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

This patch set attempts to resolve the build failure in openSUSE build
service equipped with new gcc 9 compiler, which has added a new warning
flag -Waddress-of-packed-member.

The new warning performs the check for taking the address of packed
member directly to a pointer variable with higher alignment requirement
and the outcome is risky to memory alignment fault on some architecture
when deferencing it.

Please help to review.

Thanks.

v2: Disable -Werror=address-of-packed-member if its diagnose turns out
to be false positive.

Michael Chang (8):
  cpio: disable gcc9 -Waddress-of-packed-member
  jfs: disable gcc9 -Waddress-of-packed-member
  hfs: fix gcc9 error -Waddress-of-packed-member
  hfsplus: fix gcc9 error with -Waddress-of-packed-member
  acpi: fix gcc9 error -Waddress-of-packed-member
  usbtest: disable gcc9 -Waddress-of-packed-member
  chainloader: fix gcc9 error -Waddress-of-packed-member
  efi: fix gcc9 error -Waddress-of-packed-member

 grub-core/commands/usbtest.c       |  9 ++++++
 grub-core/fs/cpio_common.c         |  9 ++++++
 grub-core/fs/hfsplus.c             | 57 ++++++++++++++++++++++++++------------
 grub-core/fs/jfs.c                 |  7 +++++
 grub-core/kern/efi/efi.c           | 27 ++++++++++++++++--
 grub-core/loader/efi/chainloader.c | 12 ++++++--
 include/grub/acpi.h                |  2 +-
 include/grub/hfs.h                 |  2 +-
 8 files changed, 100 insertions(+), 25 deletions(-)

-- 
2.16.4



^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2 1/8] cpio: disable gcc9 -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 2/8] jfs: " Michael Chang
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

Disable the -Wadress-of-packaed-member diagnostic for the
grub_cpio_find_file function since the result is found to be false
postive. Any pointers to member of the 'struct head hd' is aligned even
if the structure is packed without paddings.

[   59s] In file included from ../grub-core/fs/cpio.c:51:
[   59s] ../grub-core/fs/cpio_common.c: In function 'grub_cpio_find_file':
[   59s] ../grub-core/fs/cpio_common.c:58:31: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    58 |   data->size = read_number (hd.filesize, ARRAY_SIZE (hd.filesize));
[   59s]       |                             ~~^~~~~~~~~
[   59s] ../grub-core/fs/cpio_common.c:60:29: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    60 |     *mtime = read_number (hd.mtime, ARRAY_SIZE (hd.mtime));
[   59s]       |                           ~~^~~~~~
[   59s] ../grub-core/fs/cpio_common.c:61:28: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    61 |   modeval = read_number (hd.mode, ARRAY_SIZE (hd.mode));
[   59s]       |                          ~~^~~~~
[   59s] ../grub-core/fs/cpio_common.c:62:29: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    62 |   namesize = read_number (hd.namesize, ARRAY_SIZE (hd.namesize));
[   59s]       |                           ~~^~~~~~~~~
[   59s] In file included from ../grub-core/fs/cpio_be.c:51:
[   59s] ../grub-core/fs/cpio_common.c: In function 'grub_cpio_find_file':
[   59s] ../grub-core/fs/cpio_common.c:58:31: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    58 |   data->size = read_number (hd.filesize, ARRAY_SIZE (hd.filesize));
[   59s]       |                             ~~^~~~~~~~~
[   59s] ../grub-core/fs/cpio_common.c:60:29: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    60 |     *mtime = read_number (hd.mtime, ARRAY_SIZE (hd.mtime));
[   59s]       |                           ~~^~~~~~
[   59s] ../grub-core/fs/cpio_common.c:61:28: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    61 |   modeval = read_number (hd.mode, ARRAY_SIZE (hd.mode));
[   59s]       |                          ~~^~~~~
[   59s] ../grub-core/fs/cpio_common.c:62:29: error: taking address of packed member of 'struct head' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]    62 |   namesize = read_number (hd.namesize, ARRAY_SIZE (hd.namesize));
[   59s]       |                           ~~^~~~~~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/fs/cpio_common.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/grub-core/fs/cpio_common.c b/grub-core/fs/cpio_common.c
index ed134d931..4e885d623 100644
--- a/grub-core/fs/cpio_common.c
+++ b/grub-core/fs/cpio_common.c
@@ -36,6 +36,11 @@ struct grub_archelp_data
   grub_off_t size;
 };
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+
 static grub_err_t
 grub_cpio_find_file (struct grub_archelp_data *data, char **name,
 		     grub_int32_t *mtime, grub_uint32_t *mode)
@@ -96,6 +101,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name,
   return GRUB_ERR_NONE;
 }
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic pop
+#endif
+
 static char *
 grub_cpio_get_link_target (struct grub_archelp_data *data)
 {
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 2/8] jfs: disable gcc9 -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
  2019-04-11  9:14 ` [PATCH v2 1/8] cpio: disable gcc9 -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 3/8] hfs: fix gcc9 error -Waddress-of-packed-member Michael Chang
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

Disable the -Wadress-of-packaed-member diagnostic for the
grub_jfs_getent function since the result is found to be false postive.

The leaf is read into memory as continous chunks in size of 32 bytes and
the pointer to its base is aligned, which also guarentee its member
leaf->namepart is aligned.

[   60s] ../grub-core/fs/jfs.c: In function 'grub_jfs_getent':
[   60s] ../grub-core/fs/jfs.c:557:44: error: taking address of packed member of 'struct grub_jfs_leaf_dirent' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   60s]   557 |   le_to_cpu16_copy (filename + strpos, leaf->namepart, len < diro->data->namecomponentlen ? len
[   60s]       |                                        ~~~~^~~~~~~~~~
[   60s] ../grub-core/fs/jfs.c:570:48: error: taking address of packed member of 'struct grub_jfs_leaf_next_dirent' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   60s]   570 |  le_to_cpu16_copy (filename + strpos, next_leaf->namepart, len < 15 ? len : 15);
[   60s]       |                                       ~~~~~~~~~^~~~~~~~~~
[   60s] cc1: all warnings being treated as errors

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/fs/jfs.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c
index 09bc5608d..d5a6d6527 100644
--- a/grub-core/fs/jfs.c
+++ b/grub-core/fs/jfs.c
@@ -505,6 +505,10 @@ le_to_cpu16_copy (grub_uint16_t *out, grub_uint16_t *in, grub_size_t len)
     *out++ = grub_le_to_cpu16 (*in++);
 }
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
 
 /* Read in the next dirent from the directory described by DIRO.  */
 static grub_err_t
@@ -582,6 +586,9 @@ grub_jfs_getent (struct grub_jfs_diropen *diro)
   return 0;
 }
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic pop
+#endif
 
 /* Read LEN bytes from the file described by DATA starting with byte
    POS.  Return the amount of read bytes in READ.  */
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 3/8] hfs: fix gcc9 error -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
  2019-04-11  9:14 ` [PATCH v2 1/8] cpio: disable gcc9 -Waddress-of-packed-member Michael Chang
  2019-04-11  9:14 ` [PATCH v2 2/8] jfs: " Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 4/8] hfsplus: fix gcc9 error with -Waddress-of-packed-member Michael Chang
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

Simply adds the missing packed attribute to 'struct grub_hfs_extent'.

[   83s] ../grub-core/fs/hfs.c: In function 'grub_hfs_iterate_records':
[   83s] ../grub-core/fs/hfs.c:699:9: error: taking address of packed member of 'struct grub_hfs_sblock' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   83s]   699 |      ? (&data->sblock.catalog_recs)
[   83s]       |        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~
[   83s] ../grub-core/fs/hfs.c:700:9: error: taking address of packed member of 'struct grub_hfs_sblock' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   83s]   700 |      : (&data->sblock.extent_recs));
[   83s]       |        ~^~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 include/grub/hfs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/grub/hfs.h b/include/grub/hfs.h
index d935f5005..e27993c42 100644
--- a/include/grub/hfs.h
+++ b/include/grub/hfs.h
@@ -29,7 +29,7 @@ struct grub_hfs_extent
   /* The first physical block.  */
   grub_uint16_t first_block;
   grub_uint16_t count;
-};
+} GRUB_PACKED;
 
 /* HFS stores extents in groups of 3.  */
 typedef struct grub_hfs_extent grub_hfs_datarecord_t[3];
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 4/8] hfsplus: fix gcc9 error with -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (2 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 3/8] hfs: fix gcc9 error -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 5/8] acpi: fix gcc9 error -Waddress-of-packed-member Michael Chang
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

The catkey->name could be unaligned since the address of 'void* record'
is calculated as offset in bytes to a malloc buffer.

The fix is using aligned buffer allocated by grub_malloc for holding
the UTF16 string copied from catkey->name. And use that buffer as
argument for grub_utf16_to_utf8 to convert to UTF8 strings.

In addition, using a new copy of buffer rather than catkey->name itself
for processing the endianess conversion, we can also get rid of the hunk
restoring byte order of catkey->name to what it was previously.

[   59s] ../grub-core/fs/hfsplus.c: In function 'list_nodes':
[   59s] ../grub-core/fs/hfsplus.c:738:57: error: taking address of packed member of 'struct grub_hfsplus_catkey' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]   738 |   *grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
[   59s]       |                                                   ~~~~~~^~~~~~
[   59s] ../grub-core/fs/hfsplus.c: In function 'grub_hfsplus_label':
[   59s] ../grub-core/fs/hfsplus.c:1019:57: error: taking address of packed member of 'struct grub_hfsplus_catkey' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[   59s]  1019 |   *grub_utf16_to_utf8 ((grub_uint8_t *) (*label), catkey->name,
[   59s]       |                                                   ~~~~~~^~~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/fs/hfsplus.c | 57 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 17 deletions(-)

diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c
index 73ae95fbc..54786bb1c 100644
--- a/grub-core/fs/hfsplus.c
+++ b/grub-core/fs/hfsplus.c
@@ -661,6 +661,7 @@ list_nodes (void *record, void *hook_arg)
   char *filename;
   int i;
   struct grub_fshelp_node *node;
+  grub_uint16_t *keyname;
   struct grub_hfsplus_catfile *fileinfo;
   enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
   struct list_nodes_ctx *ctx = hook_arg;
@@ -719,32 +720,34 @@ list_nodes (void *record, void *hook_arg)
   if (! filename)
     return 0;
 
+  keyname = grub_malloc (grub_be_to_cpu16 (catkey->namelen) * sizeof (*keyname));
+  if (!keyname)
+    {
+      grub_free (filename);
+      return 0;
+    }
+
   /* Make sure the byte order of the UTF16 string is correct.  */
   for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++)
     {
-      catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
+      keyname[i] = grub_be_to_cpu16 (catkey->name[i]);
 
-      if (catkey->name[i] == '/')
-	catkey->name[i] = ':';
+      if (keyname[i] == '/')
+	keyname[i] = ':';
 
       /* If the name is obviously invalid, skip this node.  */
-      if (catkey->name[i] == 0)
+      if (keyname[i] == 0)
 	{
+	  grub_free (keyname);
 	  grub_free (filename);
 	  return 0;
 	}
     }
 
-  *grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name,
+  *grub_utf16_to_utf8 ((grub_uint8_t *) filename, keyname,
 		       grub_be_to_cpu16 (catkey->namelen)) = '\0';
 
-  /* Restore the byte order to what it was previously.  */
-  for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++)
-    {
-      if (catkey->name[i] == ':')
-	catkey->name[i] = '/';
-      catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
-    }
+  grub_free (keyname);
 
   /* hfs+ is case insensitive.  */
   if (! ctx->dir->data->case_sensitive)
@@ -975,6 +978,7 @@ grub_hfsplus_label (grub_device_t device, char **label)
   grub_disk_t disk = device->disk;
   struct grub_hfsplus_catkey *catkey;
   int i, label_len;
+  grub_uint16_t *label_name;
   struct grub_hfsplus_key_internal intern;
   struct grub_hfsplus_btnode *node = NULL;
   grub_disk_addr_t ptr = 0;
@@ -1003,22 +1007,41 @@ grub_hfsplus_label (grub_device_t device, char **label)
     grub_hfsplus_btree_recptr (&data->catalog_tree, node, ptr);
 
   label_len = grub_be_to_cpu16 (catkey->namelen);
+  label_name = grub_malloc (label_len * sizeof (*label_name));
+  if (!label_name)
+    {
+      grub_free (node);
+      grub_free (data);
+      return grub_errno;
+    }
+
   for (i = 0; i < label_len; i++)
     {
-      catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]);
+      label_name[i] = grub_be_to_cpu16 (catkey->name[i]);
 
       /* If the name is obviously invalid, skip this node.  */
-      if (catkey->name[i] == 0)
-	return 0;
+      if (label_name[i] == 0)
+	{
+	  grub_free (label_name);
+	  grub_free (node);
+	  grub_free (data);
+	  return 0;
+	}
     }
 
   *label = grub_malloc (label_len * GRUB_MAX_UTF8_PER_UTF16 + 1);
   if (! *label)
-    return grub_errno;
+    {
+      grub_free (label_name);
+      grub_free (node);
+      grub_free (data);
+      return grub_errno;
+    }
 
-  *grub_utf16_to_utf8 ((grub_uint8_t *) (*label), catkey->name,
+  *grub_utf16_to_utf8 ((grub_uint8_t *) (*label), label_name,
 		       label_len) = '\0';
 
+  grub_free (label_name);
   grub_free (node);
   grub_free (data);
 
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 5/8] acpi: fix gcc9 error -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (3 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 4/8] hfsplus: fix gcc9 error with -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 6/8] usbtest: disable gcc9 -Waddress-of-packed-member Michael Chang
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

Simply adds the missing packed attribute to 'struct grub_acpi_madt'.

[  233s] ../../grub-core/commands/lsacpi.c: In function 'disp_acpi_xsdt_table':
[  233s] ../../grub-core/commands/lsacpi.c:201:27: error: converting a packed 'struct grub_acpi_table_header' pointer (alignment 1) to a 'struct grub_acpi_madt' pointer (alignment 4) may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  233s]   201 |  disp_madt_table ((struct grub_acpi_madt *) t);
[  233s]       |                           ^~~~~~~~~~~~~~
[  233s] In file included from ../../grub-core/commands/lsacpi.c:23:
[  233s] ../../include/grub/acpi.h:50:8: note: defined here
[  233s]    50 | struct grub_acpi_table_header
[  233s]       |        ^~~~~~~~~~~~~~~~~~~~~~
[  233s] ../../include/grub/acpi.h:90:8: note: defined here
[  233s]    90 | struct grub_acpi_madt
[  233s]       |        ^~~~~~~~~~~~~~
[  233s] ../../grub-core/commands/lsacpi.c: In function 'disp_acpi_rsdt_table':
[  233s] ../../grub-core/commands/lsacpi.c:225:27: error: converting a packed 'struct grub_acpi_table_header' pointer (alignment 1) to a 'struct grub_acpi_madt' pointer (alignment 4) may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  233s]   225 |  disp_madt_table ((struct grub_acpi_madt *) t);
[  233s]       |                           ^~~~~~~~~~~~~~
[  233s] In file included from ../../grub-core/commands/lsacpi.c:23:
[  233s] ../../include/grub/acpi.h:50:8: note: defined here
[  233s]    50 | struct grub_acpi_table_header
[  233s]       |        ^~~~~~~~~~~~~~~~~~~~~~
[  233s] ../../include/grub/acpi.h:90:8: note: defined here
[  233s]    90 | struct grub_acpi_madt
[  233s]       |        ^~~~~~~~~~~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 include/grub/acpi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/grub/acpi.h b/include/grub/acpi.h
index 66148f684..84f49487d 100644
--- a/include/grub/acpi.h
+++ b/include/grub/acpi.h
@@ -93,7 +93,7 @@ struct grub_acpi_madt
   grub_uint32_t lapic_addr;
   grub_uint32_t flags;
   struct grub_acpi_madt_entry_header entries[0];
-};
+} GRUB_PACKED;
 
 enum
   {
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 6/8] usbtest: disable gcc9 -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (4 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 5/8] acpi: fix gcc9 error -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 7/8] chainloader: fix gcc9 error -Waddress-of-packed-member Michael Chang
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

Disable the -Wadress-of-packaed-member diagnostic for the
grub_usb_get_string function since the result is false postive. The
descstrp->str is found to be aligned in the buffer allocated for 'struct
grub_usb_desc_str'.

[  229s] ../../grub-core/commands/usbtest.c: In function 'grub_usb_get_string':
[  229s] ../../grub-core/commands/usbtest.c:104:58: error: taking address of packed member of 'struct grub_usb_desc_str' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  229s]   104 |   *grub_utf16_to_utf8 ((grub_uint8_t *) *string, descstrp->str,
[  229s]       |                                                  ~~~~~~~~^~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/commands/usbtest.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/grub-core/commands/usbtest.c b/grub-core/commands/usbtest.c
index 01cdca934..2c6d93fe6 100644
--- a/grub-core/commands/usbtest.c
+++ b/grub-core/commands/usbtest.c
@@ -63,6 +63,11 @@ static const char *usb_devspeed[] =
     "High"
   };
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+#endif
+
 static grub_usb_err_t
 grub_usb_get_string (grub_usb_device_t dev, grub_uint8_t index, int langid,
 		     char **string)
@@ -108,6 +113,10 @@ grub_usb_get_string (grub_usb_device_t dev, grub_uint8_t index, int langid,
   return GRUB_USB_ERR_NONE;
 }
 
+#if __GNUC__ >= 9
+#pragma GCC diagnostic pop
+#endif
+
 static void
 usb_print_str (const char *description, grub_usb_device_t dev, int idx)
 {
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 7/8] chainloader: fix gcc9 error -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (5 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 6/8] usbtest: disable gcc9 -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-11  9:14 ` [PATCH v2 8/8] efi: " Michael Chang
  2019-04-18 12:13 ` [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Daniel Kiper
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

The address of fp->path_name could be unaligned since seeking into the
device path buffer for a given node could end in byte boundary.

The fix is using aligned buffer allocated by grub_malloc for receiving
the converted UTF16 string by grub_utf8_to_utf16 and also the processing
after. The resulting string then gets copied to fp->path_name.

[  243s] ../../grub-core/loader/efi/chainloader.c: In function 'copy_file_path':
[  243s] ../../grub-core/loader/efi/chainloader.c:136:32: error: taking address of packed member of 'struct grub_efi_file_path_device_path' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  243s]   136 |   size = grub_utf8_to_utf16 (fp->path_name, len * GRUB_MAX_UTF16_PER_UTF8,
[  243s]       |                              ~~^~~~~~~~~~~
[  243s] ../../grub-core/loader/efi/chainloader.c:138:12: error: taking address of packed member of 'struct grub_efi_file_path_device_path' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  243s]   138 |   for (p = fp->path_name; p < fp->path_name + size; p++)
[  243s]       |            ^~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/loader/efi/chainloader.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
index f706b1ac3..cd92ea3f2 100644
--- a/grub-core/loader/efi/chainloader.c
+++ b/grub-core/loader/efi/chainloader.c
@@ -110,21 +110,27 @@ static void
 copy_file_path (grub_efi_file_path_device_path_t *fp,
 		const char *str, grub_efi_uint16_t len)
 {
-  grub_efi_char16_t *p;
+  grub_efi_char16_t *p, *path_name;
   grub_efi_uint16_t size;
 
   fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
   fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
 
-  size = grub_utf8_to_utf16 (fp->path_name, len * GRUB_MAX_UTF16_PER_UTF8,
+  path_name = grub_malloc (len * GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
+  if (!path_name)
+    return;
+
+  size = grub_utf8_to_utf16 (path_name, len * GRUB_MAX_UTF16_PER_UTF8,
 			     (const grub_uint8_t *) str, len, 0);
-  for (p = fp->path_name; p < fp->path_name + size; p++)
+  for (p = path_name; p < path_name + size; p++)
     if (*p == '/')
       *p = '\\';
 
+  grub_memcpy (fp->path_name, path_name, size * sizeof (*fp->path_name));
   /* File Path is NULL terminated */
   fp->path_name[size++] = '\0';
   fp->header.length = size * sizeof (grub_efi_char16_t) + sizeof (*fp);
+  grub_free (path_name);
 }
 
 static grub_efi_device_path_t *
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 8/8] efi: fix gcc9 error -Waddress-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (6 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 7/8] chainloader: fix gcc9 error -Waddress-of-packed-member Michael Chang
@ 2019-04-11  9:14 ` Michael Chang
  2019-04-18 12:13 ` [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Daniel Kiper
  8 siblings, 0 replies; 10+ messages in thread
From: Michael Chang @ 2019-04-11  9:14 UTC (permalink / raw)
  To: grub-devel

The address of fp->path_name could be unaligned since seeking into the
device path buffer for a given node could end in byte boundary.

The fix is allocating aligned buffer by grub_malloc for holding the
UTF16 string copied from fp->path_name, and after using that buffer as
argument for grub_utf16_to_utf8 to convert it to UTF8 string.

[  255s] ../../grub-core/kern/efi/efi.c: In function 'grub_efi_get_filename':
[  255s] ../../grub-core/kern/efi/efi.c:410:60: error: taking address of packed member of 'struct grub_efi_file_path_device_path' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  255s]   410 |    p = (char *) grub_utf16_to_utf8 ((unsigned char *) p, fp->path_name, len);
[  255s]       |                                                          ~~^~~~~~~~~~~
[  255s] ../../grub-core/kern/efi/efi.c: In function 'grub_efi_print_device_path':
[  255s] ../../grub-core/kern/efi/efi.c:900:33: error: taking address of packed member of 'struct grub_efi_file_path_device_path' may result in an unaligned pointer value [-Werror=address-of-packed-member]
[  255s]   900 |     *grub_utf16_to_utf8 (buf, fp->path_name,
[  255s]       |                               ~~^~~~~~~~~~~

Signed-off-by: Michael Chang <mchang@suse.com>
---
 grub-core/kern/efi/efi.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index 708581fcb..62cd9829e 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -369,6 +369,7 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
 	{
 	  grub_efi_file_path_device_path_t *fp;
 	  grub_efi_uint16_t len;
+	  grub_efi_char16_t *dup_name;
 
 	  *p++ = '/';
 
@@ -379,7 +380,16 @@ grub_efi_get_filename (grub_efi_device_path_t *dp0)
 	  while (len > 0 && fp->path_name[len - 1] == 0)
 	    len--;
 
-	  p = (char *) grub_utf16_to_utf8 ((unsigned char *) p, fp->path_name, len);
+	  dup_name = grub_malloc (len * sizeof (*dup_name));
+	  if (!dup_name)
+	    {
+	      grub_free (name);
+	      return NULL;
+	    }
+	  p = (char *) grub_utf16_to_utf8 ((unsigned char *) p,
+					    grub_memcpy (dup_name, fp->path_name, len * sizeof (*dup_name)),
+					    len);
+	  grub_free (dup_name);
 	}
 
       dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
@@ -809,9 +819,20 @@ grub_efi_print_device_path (grub_efi_device_path_t *dp)
 		fp = (grub_efi_file_path_device_path_t *) dp;
 		buf = grub_malloc ((len - 4) * 2 + 1);
 		if (buf)
-		  *grub_utf16_to_utf8 (buf, fp->path_name,
+		  {
+		    grub_efi_char16_t *dup_name = grub_malloc (len - 4);
+		    if (!dup_name)
+		      {
+			grub_errno = GRUB_ERR_NONE;
+			grub_printf ("/File((null))");
+			grub_free (buf);
+			break;
+		      }
+		    *grub_utf16_to_utf8 (buf, grub_memcpy (dup_name, fp->path_name, len - 4),
 				       (len - 4) / sizeof (grub_efi_char16_t))
-		    = '\0';
+		      = '\0';
+		    grub_free (dup_name);
+		  }
 		else
 		  grub_errno = GRUB_ERR_NONE;
 		grub_printf ("/File(%s)", buf);
-- 
2.16.4



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member
  2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
                   ` (7 preceding siblings ...)
  2019-04-11  9:14 ` [PATCH v2 8/8] efi: " Michael Chang
@ 2019-04-18 12:13 ` Daniel Kiper
  8 siblings, 0 replies; 10+ messages in thread
From: Daniel Kiper @ 2019-04-18 12:13 UTC (permalink / raw)
  To: Michael Chang; +Cc: grub-devel

On Thu, Apr 11, 2019 at 05:14:01PM +0800, Michael Chang wrote:
> This patch set attempts to resolve the build failure in openSUSE build
> service equipped with new gcc 9 compiler, which has added a new warning
> flag -Waddress-of-packed-member.
>
> The new warning performs the check for taking the address of packed
> member directly to a pointer variable with higher alignment requirement
> and the outcome is risky to memory alignment fault on some architecture
> when deferencing it.
>
> Please help to review.
>
> Thanks.
>
> v2: Disable -Werror=address-of-packed-member if its diagnose turns out
> to be false positive.

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com> for whole patch series.

If there are no objections I will apply this next week.

Daniel


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-04-18 12:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-11  9:14 [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 1/8] cpio: disable gcc9 -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 2/8] jfs: " Michael Chang
2019-04-11  9:14 ` [PATCH v2 3/8] hfs: fix gcc9 error -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 4/8] hfsplus: fix gcc9 error with -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 5/8] acpi: fix gcc9 error -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 6/8] usbtest: disable gcc9 -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 7/8] chainloader: fix gcc9 error -Waddress-of-packed-member Michael Chang
2019-04-11  9:14 ` [PATCH v2 8/8] efi: " Michael Chang
2019-04-18 12:13 ` [PATCH v2 0/8] fix gcc9 build with -Werror=address-of-packed-member Daniel Kiper

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.