Skip to content

Commit c3c6d21

Browse files
committed
Added cppcheck --enable-portability, fixed UBs
Fixed reported UBs involving (void *) ptr arithmetic
1 parent dcb82b6 commit c3c6d21

File tree

6 files changed

+10
-7
lines changed

6 files changed

+10
-7
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ line-count-x86:
329329
cloc --force-lang-def cloc_lang_def.txt src/boot_x86_fsp.c src/boot_x86_fsp_payload.c src/boot_x86_fsp_start.S src/image.c src/keystore.c src/libwolfboot.c src/loader.c src/string.c src/update_disk.c src/x86/ahci.c src/x86/ata.c src/x86/common.c src/x86/gpt.c src/x86/hob.c src/pci.c src/x86/tgl_fsp.c hal/x86_fsp_tgl.c hal/x86_uart.c
330330

331331
cppcheck:
332-
cppcheck -f --enable=warning --suppress="ctunullpointer" --suppress="nullPointer" --suppress="objectIndex" --suppress="comparePointers" --error-exitcode=89 --std=c89 src/*.c hal/*.c hal/spi/*.c hal/uart/*.c
332+
cppcheck -f --enable=warning --enable=portability \
333+
--suppress="ctunullpointer" --suppress="nullPointer" \
334+
--suppress="objectIndex" --suppress="comparePointers" \
335+
--error-exitcode=89 --std=c89 src/*.c hal/*.c hal/spi/*.c hal/uart/*.c
333336

334337
%.o:%.c
335338
@echo "\t[CC-$(ARCH)] $@"

hal/nxp_p1021.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ struct qe_firmware {
12381238
static void qe_upload_microcode(const struct qe_firmware *firmware,
12391239
const struct qe_microcode *ucode)
12401240
{
1241-
const uint32_t *code = (void*)firmware + ucode->code_offset;
1241+
const uint32_t *code = (uint32_t*)((uint8_t *)firmware + ucode->code_offset);
12421242
unsigned int i;
12431243

12441244
wolfBoot_printf("QE: uploading '%s' version %u.%u.%u\n",

hal/nxp_t1024.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ struct qe_firmware {
11241124
static void qe_upload_microcode(const struct qe_firmware *firmware,
11251125
const struct qe_microcode *ucode)
11261126
{
1127-
const uint32_t *code = (void*)firmware + ucode->code_offset;
1127+
const uint32_t *code = (uint32_t*)((uint8_t *)firmware + ucode->code_offset);
11281128
unsigned int i;
11291129

11301130
wolfBoot_printf("QE: uploading '%s' version %u.%u.%u\n",

hal/sim.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int find_epc(void *base, struct entry_point_command **entry)
200200
*entry = NULL;
201201

202202
mh = (struct mach_header_64*)base;
203-
lc = (struct load_command*)(base + sizeof(struct mach_header_64));
203+
lc = (struct load_command*)((uint8_t *)base + sizeof(struct mach_header_64));
204204
for (i=0; i<(int)mh->ncmds; i++) {
205205
if (lc->cmd == LC_MAIN) { /* 0x80000028 */
206206
*entry = (struct entry_point_command *)lc;
@@ -250,7 +250,7 @@ void do_boot(const uint32_t *app_offset)
250250
/* restore mh_bundle type to allow hash to remain valid */
251251
app_buf[3] = typeVal;
252252

253-
main = (main_entry)(void*)(pSymbolAddress + epc->entryoff);
253+
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
254254
main(main_argc, main_argv, NULL, NULL);
255255
#else
256256
char *envp[1] = {NULL};

src/qspi_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ int spi_flash_write(uint32_t address, const void *data, int len)
444444
addr, QSPI_ADDR_SZ, QSPI_DATA_MODE_SPI, /* Address */
445445
0, 0, QSPI_DATA_MODE_NONE, /* Alternate Bytes */
446446
0, /* Dummy */
447-
(uint8_t*)(data + (page * FLASH_PAGE_SIZE)),
447+
((uint8_t*)data + (page * FLASH_PAGE_SIZE)),
448448
xferSz, QSPI_DATA_MODE /* Data */
449449
);
450450
#ifdef DEBUG_QSPI

src/update_disk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void RAMFUNCTION wolfBoot_start(void)
168168
load_off = 0;
169169
do {
170170
ret = disk_read(BOOT_DISK, cur_part, load_off, 512,
171-
(void *)load_address + load_off);
171+
(uint8_t *)load_address + load_off);
172172
if (ret < 0)
173173
break;
174174
load_off += ret;

0 commit comments

Comments
 (0)