From: Michael Chang <mchang@suse.com>
To: grub-devel@gnu.org
Subject: [PATCH 4/8] hfsplus: fix gcc9 error address-of-packed-member
Date: Tue, 9 Apr 2019 18:46:55 +0800 [thread overview]
Message-ID: <20190409104659.4125-5-mchang@suse.com> (raw)
In-Reply-To: <20190409104659.4125-1-mchang@suse.com>
Use pointer to buffer returned by grub_malloc, which is supposed to be
suitably aligned for any data type, for holding the UTF16 string
converted to host by order from packed struct member catkey->name. The
aligned buffer is later used as argument to grub_utf16_to_utf8.
By using a new copy of buffer rather than catkey->name itself for
holding the endianess converted data, we can also get rid of the hunk
restoring byte order of catkey->name to what it was previously.
The solved gcc9 error like this.
[ 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
next prev parent reply other threads:[~2019-04-09 10:47 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-09 10:46 [PATCH 0/8] fix gcc9 build with -Werror=address-of-packed-member Michael Chang
2019-04-09 10:46 ` [PATCH 1/8] cpio: fix gcc9 error address-of-packed-member Michael Chang
2019-04-09 10:46 ` [PATCH 2/8] jfs: " Michael Chang
2019-04-09 10:46 ` [PATCH 3/8] hfs: " Michael Chang
2019-04-09 10:46 ` Michael Chang [this message]
2019-04-09 10:46 ` [PATCH 5/8] acpi: " Michael Chang
2019-04-09 10:46 ` [PATCH 6/8] usbtest: " Michael Chang
2019-04-09 10:46 ` [PATCH 7/8] chainloader: " Michael Chang
2019-04-09 10:46 ` [PATCH 8/8] efi: " Michael Chang
2019-04-09 10:57 ` [PATCH 0/8] fix gcc9 build with -Werror=address-of-packed-member Vladimir 'phcoder' Serbinenko
2019-04-09 11:39 ` Michael Chang
2019-04-09 12:15 ` Vladimir 'phcoder' Serbinenko
2019-04-10 4:41 ` Michael Chang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190409104659.4125-5-mchang@suse.com \
--to=mchang@suse.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.