From 8db3c2a48549f28e40febae827666fe1730ebcd0 Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Thu, 25 May 2017 10:48:57 -0700 Subject: [PATCH] ifdtool: avoid potential unaligned pointer usage In get_region, ifdtool assigns a not-known-to-be-aligned pointer to a uint32_t *. Now you know and I know that it is almost certainly aligned, but clang on OSX doesn't like this, and it's a dicey thing to do in any event, just waiting to hit someone hard at some future date. Assign the pointer to a void * and use memmove to copy the value to a uint32_t. This usage is more portable to all little-endian architectures, now, but is still not endian-safe. I doubt we'll ever care. Change-Id: Ifb2f260c3363ab0f5b4a59e5a4e0b5ecf049fa96 Signed-off-by: Ronald G. Minnich Reviewed-on: https://review.coreboot.org/19921 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer --- util/ifdtool/ifdtool.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/util/ifdtool/ifdtool.c b/util/ifdtool/ifdtool.c index 1024690423..9382bc4714 100644 --- a/util/ifdtool/ifdtool.c +++ b/util/ifdtool/ifdtool.c @@ -105,7 +105,8 @@ static region_t get_region(frba_t *frba, int region_type) { int base_mask; int limit_mask; - uint32_t *flreg; + uint32_t flreg; + void *v; region_t region; if (ifd_version >= IFD_VERSION_2) @@ -117,39 +118,40 @@ static region_t get_region(frba_t *frba, int region_type) switch (region_type) { case 0: - flreg = &frba->flreg0; + v = &frba->flreg0; break; case 1: - flreg = &frba->flreg1; + v = &frba->flreg1; break; case 2: - flreg = &frba->flreg2; + v = &frba->flreg2; break; case 3: - flreg = &frba->flreg3; + v = &frba->flreg3; break; case 4: - flreg = &frba->flreg4; + v = &frba->flreg4; break; case 5: - flreg = &frba->flreg5; + v = &frba->flreg5; break; case 6: - flreg = &frba->flreg6; + v = &frba->flreg6; break; case 7: - flreg = &frba->flreg7; + v = &frba->flreg7; break; case 8: - flreg = &frba->flreg8; + v = &frba->flreg8; break; default: fprintf(stderr, "Invalid region type %d.\n", region_type); exit (EXIT_FAILURE); } - region.base = (*flreg & base_mask) << 12; - region.limit = ((*flreg & limit_mask) >> 4) | 0xfff; + memmove(&flreg, v, sizeof(flreg)); + region.base = (flreg & base_mask) << 12; + region.limit = ((flreg & limit_mask) >> 4) | 0xfff; region.size = region.limit - region.base + 1; if (region.size < 0)