1From b98ba8921010d03f46704a476c69861515deb1ca Mon Sep 17 00:00:00 2001 2From: Peter Jones <pjones@redhat.com> 3Date: Mon, 7 Jan 2019 10:30:59 -0500 4Subject: [PATCH] dp.h: make format_guid() handle misaligned guid pointers 5 safely. 6 7GCC 9 adds -Werror=address-of-packed-member, which causes us to see the 8build error reported at 9 https://bugzilla.opensuse.org/show_bug.cgi?id=1120862 . 10 11That bug report shows us the following: 12 13In file included from dp.c:26: 14dp.h: In function 'format_vendor_helper': 15dp.h:120:37: error: taking address of packed member of 'struct <anonymous>' may result in an unaligned pointer value [-Werror=address-of-packed-member] 16 120 | format_guid(buf, size, off, label, &dp->hw_vendor.vendor_guid); 17 | ^~~~~~~~~~~~~~~~~~~~~~~~~~ 18dp.h:74:25: note: in definition of macro 'format_guid' 19 74 | _rc = efi_guid_to_str(guid, &_guidstr); \ 20 | ^~~~ 21cc1: all warnings being treated as errors 22 23This patch makes format_guid() use a local variable as a bounce buffer 24in the case that the guid we're passed is aligned as chaotic neutral. 25 26Note that this only fixes this instance and there may be others that bz 27didn't show because it exited too soon, and I don't have a gcc 9 build 28in front of me right now. 29 30Signed-off-by: Peter Jones <pjones@redhat.com> 31[james.hilliard1@gmail.com: backport from upstream commit 32b98ba8921010d03f46704a476c69861515deb1ca] 33Signed-off-by: James Hilliard <james.hilliard1@gmail.com> 34--- 35 src/dp.h | 11 +++++++++-- 36 1 file changed, 9 insertions(+), 2 deletions(-) 37 38diff --git a/src/dp.h b/src/dp.h 39index aa4e390..20cb608 100644 40--- a/src/dp.h 41+++ b/src/dp.h 42@@ -70,8 +70,15 @@ 43 #define format_guid(buf, size, off, dp_type, guid) ({ \ 44 int _rc; \ 45 char *_guidstr = NULL; \ 46- \ 47- _rc = efi_guid_to_str(guid, &_guidstr); \ 48+ efi_guid_t _guid; \ 49+ const efi_guid_t * const _guid_p = \ 50+ likely(__alignof__(guid) == sizeof(guid)) \ 51+ ? guid \ 52+ : &_guid; \ 53+ \ 54+ if (unlikely(__alignof__(guid) == sizeof(guid))) \ 55+ memmove(&_guid, guid, sizeof(_guid)); \ 56+ _rc = efi_guid_to_str(_guid_p, &_guidstr); \ 57 if (_rc < 0) { \ 58 efi_error("could not build %s GUID DP string", \ 59 dp_type); \ 60-- 612.20.1 62 63