Fix warnings from added -W flags

Fix a few warnings:

 idc.c: In function ‘IDC_get’:
 idc.c:248:12: warning: ‘idclen’ may be used uninitialised in this function [-Wuninitialized]

 image.c: In function ‘image_load’:
 image.c:37:15: warning: unused variable ‘bytes_read’ [-Wunused-variable]

Plus, a bunch of strict-aliasing warnings:

 image.c:101:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 [ similar warnings trimmed ]

when compiling image.c. Since struct external_PEI_DOS_hdr uses char[]
types for all members, we need to use accessors here.

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
Jeremy Kerr 2012-06-11 16:33:18 +08:00
parent 34edfd6348
commit ffc1f41ace
3 changed files with 40 additions and 10 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
ccan_modules="talloc read_write_all" ccan_modules="talloc read_write_all build_assert"
# Add ccan upstream sources # Add ccan upstream sources
if [ ! -e lib/ccan.git/Makefile ] if [ ! -e lib/ccan.git/Makefile ]

4
idc.c
View file

@ -243,6 +243,10 @@ struct idc *IDC_get(PKCS7 *p7, BIO *bio)
idclen = (idcbuf[2] << 8) + idclen = (idcbuf[2] << 8) +
idcbuf[3]; idcbuf[3];
idcbuf += 4; idcbuf += 4;
} else {
fprintf(stderr, "Invalid ASN.1 data in "
"IndirectDataContext?\n");
return NULL;
} }
BIO_write(bio, idcbuf, idclen); BIO_write(bio, idcbuf, idclen);

44
image.c
View file

@ -26,6 +26,7 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/read_write_all/read_write_all.h> #include <ccan/read_write_all/read_write_all.h>
#include <ccan/build_assert/build_assert.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include "image.h" #include "image.h"
@ -34,7 +35,6 @@
struct image *image_load(const char *filename) struct image *image_load(const char *filename)
{ {
unsigned int bytes_read;
struct stat statbuf; struct stat statbuf;
struct image *image; struct image *image;
int rc; int rc;
@ -78,6 +78,32 @@ err:
return NULL; return NULL;
} }
/**
* The PE/COFF headers export struct fields as arrays of chars. So, define
* a couple of accessor functions that allow fields to be deferenced as their
* native types, to allow strict aliasing. This also allows for endian-
* neutral behaviour.
*/
static uint32_t __pehdr_u32(char field[])
{
uint8_t *ufield = (uint8_t *)field;
return (ufield[3] << 24) +
(ufield[2] << 16) +
(ufield[1] << 8) +
ufield[0];
}
static uint16_t __pehdr_u16(char field[])
{
uint8_t *ufield = (uint8_t *)field;
return (ufield[1] << 8) +
ufield[0];
}
/* wrappers to ensure type correctness */
#define pehdr_u32(f) __pehdr_u32(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 4))
#define pehdr_u16(f) __pehdr_u16(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 2))
int image_pecoff_parse(struct image *image) int image_pecoff_parse(struct image *image)
{ {
char nt_sig[] = {'P', 'E', 0, 0}; char nt_sig[] = {'P', 'E', 0, 0};
@ -98,7 +124,7 @@ int image_pecoff_parse(struct image *image)
return -1; return -1;
} }
addr = *(uint32_t *)image->doshdr->e_lfanew; addr = pehdr_u32(image->doshdr->e_lfanew);
if (addr >= image->size) { if (addr >= image->size) {
fprintf(stderr, "pehdr is beyond end of file [0x%08x]\n", fprintf(stderr, "pehdr is beyond end of file [0x%08x]\n",
addr); addr);
@ -116,12 +142,12 @@ int image_pecoff_parse(struct image *image)
return -1; return -1;
} }
if (*(uint16_t *)image->pehdr->f_magic != AMD64MAGIC) { if (pehdr_u16(image->pehdr->f_magic) != AMD64MAGIC) {
fprintf(stderr, "Invalid PE header magic for x86_64\n"); fprintf(stderr, "Invalid PE header magic for x86_64\n");
return -1; return -1;
} }
if (*(uint16_t *)image->pehdr->f_opthdr != sizeof(*image->aouthdr)) { if (pehdr_u16(image->pehdr->f_opthdr) != sizeof(*image->aouthdr)) {
fprintf(stderr, "Invalid a.out header size\n"); fprintf(stderr, "Invalid a.out header size\n");
return -1; return -1;
} }
@ -151,7 +177,7 @@ int image_pecoff_parse(struct image *image)
else else
image->cert_table = NULL; image->cert_table = NULL;
image->sections = *(uint16_t *)image->pehdr->f_nscns; image->sections = pehdr_u16(image->pehdr->f_nscns);
image->scnhdr = (void *)(image->aouthdr+1); image->scnhdr = (void *)(image->aouthdr+1);
return 0; return 0;
@ -187,7 +213,7 @@ int image_find_regions(struct image *image)
size_t bytes; size_t bytes;
gap_warn = 0; gap_warn = 0;
align = *(uint32_t *)image->aouthdr->FileAlignment; align = pehdr_u32(image->aouthdr->FileAlignment);
/* now we know where the checksum and cert table data is, we can /* now we know where the checksum and cert table data is, we can
* construct regions that need to be signed */ * construct regions that need to be signed */
@ -222,7 +248,7 @@ int image_find_regions(struct image *image)
(void *)image->data_dir_sigtable (void *)image->data_dir_sigtable
+ sizeof(struct data_dir_entry), + sizeof(struct data_dir_entry),
image->buf + image->buf +
*(uint32_t *)image->aouthdr->SizeOfHeaders); pehdr_u32(image->aouthdr->SizeOfHeaders));
regions[2].name = "datadir[CERT]->headers"; regions[2].name = "datadir[CERT]->headers";
bytes += regions[2].size; bytes += regions[2].size;
@ -230,8 +256,8 @@ int image_find_regions(struct image *image)
for (i = 0; i < image->sections; i++) { for (i = 0; i < image->sections; i++) {
uint32_t file_offset, file_size; uint32_t file_offset, file_size;
file_offset = *(uint32_t *)image->scnhdr[i].s_scnptr; file_offset = pehdr_u32(image->scnhdr[i].s_scnptr);
file_size = *(uint32_t *)image->scnhdr[i].s_size; file_size = pehdr_u32(image->scnhdr[i].s_size);
if (!file_size) if (!file_size)
continue; continue;