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 <rminnich@gmail.com>
Reviewed-on: https://review.coreboot.org/19921
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Ronald G. Minnich 2017-05-25 10:48:57 -07:00 committed by Stefan Reinauer
parent 8274f988d9
commit 8db3c2a485
1 changed files with 14 additions and 12 deletions

View File

@ -105,7 +105,8 @@ static region_t get_region(frba_t *frba, int region_type)
{ {
int base_mask; int base_mask;
int limit_mask; int limit_mask;
uint32_t *flreg; uint32_t flreg;
void *v;
region_t region; region_t region;
if (ifd_version >= IFD_VERSION_2) if (ifd_version >= IFD_VERSION_2)
@ -117,39 +118,40 @@ static region_t get_region(frba_t *frba, int region_type)
switch (region_type) { switch (region_type) {
case 0: case 0:
flreg = &frba->flreg0; v = &frba->flreg0;
break; break;
case 1: case 1:
flreg = &frba->flreg1; v = &frba->flreg1;
break; break;
case 2: case 2:
flreg = &frba->flreg2; v = &frba->flreg2;
break; break;
case 3: case 3:
flreg = &frba->flreg3; v = &frba->flreg3;
break; break;
case 4: case 4:
flreg = &frba->flreg4; v = &frba->flreg4;
break; break;
case 5: case 5:
flreg = &frba->flreg5; v = &frba->flreg5;
break; break;
case 6: case 6:
flreg = &frba->flreg6; v = &frba->flreg6;
break; break;
case 7: case 7:
flreg = &frba->flreg7; v = &frba->flreg7;
break; break;
case 8: case 8:
flreg = &frba->flreg8; v = &frba->flreg8;
break; break;
default: default:
fprintf(stderr, "Invalid region type %d.\n", region_type); fprintf(stderr, "Invalid region type %d.\n", region_type);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
region.base = (*flreg & base_mask) << 12; memmove(&flreg, v, sizeof(flreg));
region.limit = ((*flreg & limit_mask) >> 4) | 0xfff; region.base = (flreg & base_mask) << 12;
region.limit = ((flreg & limit_mask) >> 4) | 0xfff;
region.size = region.limit - region.base + 1; region.size = region.limit - region.base + 1;
if (region.size < 0) if (region.size < 0)