diff options
author | Evan Green <evgreen@chromium.org> | 2019-09-23 12:57:40 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-10-05 00:47:45 +0000 |
commit | 3564b23531fc1814924f39e7ac97751d758c14db (patch) | |
tree | 39a04f30177288afe594f0d2a22a434a4a5b00f6 | |
parent | e34fca3e01d75552ad8d712879c3ccd6a6168584 (diff) | |
download | chrome-ec-3564b23531fc1814924f39e7ac97751d758c14db.tar.gz |
printf: Add support for %z
When printing size_t sized integers, utilize the standard %z
modifier so that the specifier format is correct. This will enable us
to turn on compile-time printf format verification.
BUG=chromium:984041
TEST=make -j buildall
BRANCH=none
Cq-Depend:chrome-internal:1860160
Change-Id: I2c95df5c0d87677cb9fcbde33ab8846708a774a1
Signed-off-by: Evan Green <evgreen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1819651
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
-rw-r--r-- | chip/ish/heci.c | 4 | ||||
-rw-r--r-- | chip/it83xx/ec2i.c | 2 | ||||
-rw-r--r-- | common/new_nvmem.c | 4 | ||||
-rw-r--r-- | common/pinweaver.c | 4 | ||||
-rw-r--r-- | common/printf.c | 10 | ||||
-rw-r--r-- | common/shmalloc.c | 8 | ||||
-rw-r--r-- | driver/accelgyro_bmi160.c | 6 | ||||
-rw-r--r-- | include/printf.h | 1 | ||||
-rw-r--r-- | test/nvmem.c | 16 | ||||
-rw-r--r-- | test/nvmem_tpm2_mock.c | 2 | ||||
-rw-r--r-- | test/pinweaver.c | 2 | ||||
-rw-r--r-- | test/printf.c | 2 |
12 files changed, 35 insertions, 26 deletions
diff --git a/chip/ish/heci.c b/chip/ish/heci.c index 146aa175f5..309a17c0a1 100644 --- a/chip/ish/heci.c +++ b/chip/ish/heci.c @@ -891,8 +891,8 @@ static int is_hbm_validity(struct hbm_h2i *h2i, size_t length) } if (valid_msg_len != length) { - CPRINTF("invalid cmd(%d) valid : %d, cur : %d\n", - valid_msg_len, length); + CPRINTF("invalid cmd(%d) valid : %d, cur : %zd\n", + h2i->cmd, valid_msg_len, length); /* TODO: invalid cmd. not sure to reply with error ? */ return 0; } diff --git a/chip/it83xx/ec2i.c b/chip/it83xx/ec2i.c index 80cd08ecc9..fe657438c4 100644 --- a/chip/it83xx/ec2i.c +++ b/chip/it83xx/ec2i.c @@ -284,7 +284,7 @@ static void pnpcfg_configure(const struct ec2i_t *settings, size_t entries) for (i = 0; i < entries; i++) { if (ec2i_write(settings[i].index_port, settings[i].data_port) == EC2I_WRITE_ERROR) { - ccprints("Failed to apply %d", i); + ccprints("Failed to apply %zd", i); break; } } diff --git a/common/new_nvmem.c b/common/new_nvmem.c index cd333520fa..445d731f86 100644 --- a/common/new_nvmem.c +++ b/common/new_nvmem.c @@ -1012,7 +1012,7 @@ test_export_static enum ec_error_list compact_nvmem(void) if (final_delimiter_needed) add_final_delimiter(); - CPRINTS("Compaction done, went from %d to %d bytes", before, + CPRINTS("Compaction done, went from %zd to %zd bytes", before, total_used_size()); return rv; } @@ -1573,7 +1573,7 @@ enum ec_error_list new_nvmem_migrate(unsigned int act_partition) /* Never returns. */ report_no_payload_failure(NVMEMF_MIGRATION_FAILURE); - CPRINTS("Migration success, used %d bytes of flash", + CPRINTS("Migration success, used %zd bytes of flash", total_used_size()); /* diff --git a/common/pinweaver.c b/common/pinweaver.c index 9a2c0132af..2cc4118c7d 100644 --- a/common/pinweaver.c +++ b/common/pinweaver.c @@ -1329,14 +1329,14 @@ static enum vendor_cmd_rc pw_vendor_specific_command(enum vendor_cmd_cc code, struct pw_response_t *response = buf; if (input_size < sizeof(request->header)) { - ccprintf("PinWeaver: message smaller than a header (%d).\n", + ccprintf("PinWeaver: message smaller than a header (%zd).\n", input_size); return VENDOR_RC_INTERNAL_ERROR; } if (input_size != request->header.data_length + sizeof(request->header)) { - ccprintf("PinWeaver: header size mismatch %d != %d.\n", + ccprintf("PinWeaver: header size mismatch %zd != %zd.\n", input_size, request->header.data_length + sizeof(request->header)); return VENDOR_RC_REQUEST_TOO_BIG; diff --git a/common/printf.c b/common/printf.c index 4da70653ee..3c7bc5feca 100644 --- a/common/printf.c +++ b/common/printf.c @@ -223,9 +223,17 @@ int vfnprintf(int (*addchar)(void *context, int c), void *context, int ptrspec; void *ptrval; - /* Handle length */ + /* + * Handle length: + * %l - long + * %z - size_t + */ if (c == 'l') { flags |= PF_64BIT; + } else if (c == 'z') { + if (sizeof(size_t) == sizeof(uint64_t)) + flags |= PF_64BIT; + c = *format++; } diff --git a/common/shmalloc.c b/common/shmalloc.c index 5cf9556fce..b1705b52d1 100644 --- a/common/shmalloc.c +++ b/common/shmalloc.c @@ -379,10 +379,10 @@ static int command_shmem(int argc, char **argv) mutex_unlock(&shmem_lock); - ccprintf("Total: %6d\n", allocated_size + free_size); - ccprintf("Allocated: %6d\n", allocated_size); - ccprintf("Free: %6d\n", free_size); - ccprintf("Max free buf: %6d\n", max_free); + ccprintf("Total: %6zd\n", allocated_size + free_size); + ccprintf("Allocated: %6zd\n", allocated_size); + ccprintf("Free: %6zd\n", free_size); + ccprintf("Max free buf: %6zd\n", max_free); ccprintf("Max allocated: %6d\n", max_allocated_size); return EC_SUCCESS; } diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index aa4eaf13c7..5fd9e3acda 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -990,7 +990,7 @@ static int load_fifo(struct motion_sensor_t *s, uint32_t last_ts) state = FIFO_DATA_CONFIG; break; default: - CPRINTS("Unknown header: 0x%02x @ %d", + CPRINTS("Unknown header: 0x%02x @ %zd", hdr, bp - bmi160_buffer); raw_write8(s->port, s->i2c_spi_addr_flags, BMI160_CMD_REG, @@ -1000,13 +1000,13 @@ static int load_fifo(struct motion_sensor_t *s, uint32_t last_ts) break; } case FIFO_DATA_SKIP: - CPRINTS("@ %d - %d, skipped %d frames", + CPRINTS("@ %zd - %d, skipped %d frames", bp - bmi160_buffer, length, *bp); bp++; state = FIFO_HEADER; break; case FIFO_DATA_CONFIG: - CPRINTS("@ %d - %d, config change: 0x%02x", + CPRINTS("@ %zd - %d, config change: 0x%02x", bp - bmi160_buffer, length, *bp); bp++; state = FIFO_HEADER; diff --git a/include/printf.h b/include/printf.h index 6e245a3f2a..a5243c79b4 100644 --- a/include/printf.h +++ b/include/printf.h @@ -35,6 +35,7 @@ * useful for printing times, voltages, and currents. * * Length may be: + * - 'z' = integer is sizeof(size_t) * - 'l' = integer is 64-bit instead of native 32-bit * * Type may be: diff --git a/test/nvmem.c b/test/nvmem.c index 8eaad92f23..303263e614 100644 --- a/test/nvmem.c +++ b/test/nvmem.c @@ -119,8 +119,8 @@ static void dump_nvmem_state(const char *title, ccprintf("deleted_obj_count: %d\n", tr->deleted_obj_count); ccprintf("deimiter_count: %d\n", tr->delimiter_count); ccprintf("unexpected_count: %d\n", tr->unexpected_count); - ccprintf("valid_data_size: %d\n", tr->valid_data_size); - ccprintf("erased_data_size: %d\n\n", tr->erased_data_size); + ccprintf("valid_data_size: %zd\n", tr->valid_data_size); + ccprintf("erased_data_size: %zd\n\n", tr->erased_data_size); } static void wipe_out_nvmem_cache(void) @@ -398,7 +398,7 @@ static size_t get_free_nvmem_room(void) free_room = (free_pages - 1) * (CONFIG_FLASH_BANK_SIZE - sizeof(struct nn_page_header)) + CONFIG_FLASH_BANK_SIZE - master_at.mt.data_offset; - ccprintf("free pages %d, data offset 0x%x\n", free_pages, + ccprintf("free pages %zd, data offset 0x%x\n", free_pages, master_at.mt.data_offset); return free_room; } @@ -649,12 +649,12 @@ static size_t fill_obj_offsets(uint16_t *offsets, size_t max_objects) obj_count = init_object_offsets(offsets, max_objects); - ccprintf("%d objects\n", obj_count); + ccprintf("%zd objects\n", obj_count); for (i = 0; i < obj_count; i++) { uint32_t *op; op = evictable_offs_to_addr(offsets[i]); - ccprintf("offs %04x:%08x:%08x:%08x addr %pP size %d\n", + ccprintf("offs %04x:%08x:%08x:%08x addr %pP size %zd\n", offsets[i], op[-1], op[0], op[1], op, (uintptr_t)nvmem_cache_base(NVMEM_TPM) + op[-1] - (uintptr_t)op); @@ -767,7 +767,7 @@ static int caches_match(const uint8_t *cache1, const uint8_t *cache2) if (!memcmp(cache1 + offset, cache2 + offset, size)) continue; - ccprintf("%s:%d failed comparing %d:%d:\n", __func__, + ccprintf("%s:%d failed comparing %zd:%zd:\n", __func__, __LINE__, i, j); for (k = offset; k < (offset + size); k++) if (cache1[k] != cache2[k]) @@ -1248,7 +1248,7 @@ static int compare_object(uint16_t obj_offset, size_t obj_size, const void *obj) evictable_offs_to_addr(obj_offset - sizeof(next_addr)), sizeof(next_addr)); - ccprintf("next_addr %x, sum %x size %d\n", next_addr, + ccprintf("next_addr %x, sum %zx size %zd\n", next_addr, (s_evictNvStart + obj_offset + obj_size), obj_size); TEST_ASSERT(next_addr == (s_evictNvStart + obj_offset + obj_size)); @@ -1289,7 +1289,7 @@ static int test_tpm_nvmem_modify_evictable_objects(void) for (i = 0; i < num_objects; i++) { memcpy(handles + i, evictable_offs_to_addr(offsets[i]), sizeof(handles[i])); - ccprintf("obj %d handle %08x\n", i, handles[i]); + ccprintf("obj %zd handle %08x\n", i, handles[i]); } /* * Let's modify the object which currently is stored second in the diff --git a/test/nvmem_tpm2_mock.c b/test/nvmem_tpm2_mock.c index 3b2e5340ad..a6d32bcb34 100644 --- a/test/nvmem_tpm2_mock.c +++ b/test/nvmem_tpm2_mock.c @@ -302,7 +302,7 @@ size_t add_evictable_obj(void *obj, size_t obj_size) next_addr = end_addr + sizeof(uint32_t) + obj_size; if (next_addr >= s_evictNvEnd) { - ccprintf("%s: could not fit %d bytes!\n", __func__, obj_size); + ccprintf("%s: could not fit %zd bytes!\n", __func__, obj_size); return 0; } diff --git a/test/pinweaver.c b/test/pinweaver.c index 093443e49e..63a7e2e067 100644 --- a/test/pinweaver.c +++ b/test/pinweaver.c @@ -986,7 +986,7 @@ static int check_dcrypto_mutex_usage(void) { if (MOCK_DECRYPTO_init_counter == MOCK_DECRYPTO_release_counter) return EC_SUCCESS; - ccprintf("ASSERTION failed: DCRYPTO init(%d) != DCRYPTO release(%d)\n", + ccprintf("ASSERTION failed: DCRYPTO init(%zd) != DCRYPTO release(%zd)\n", MOCK_DECRYPTO_init_counter, MOCK_DECRYPTO_release_counter); return EC_ERROR_UNKNOWN; } diff --git a/test/printf.c b/test/printf.c index 97bb00c4be..28a78ca973 100644 --- a/test/printf.c +++ b/test/printf.c @@ -26,7 +26,7 @@ int run(int expect_ret, const char *expect, int rv; ccprintf("\n"); - ccprintf("size_limit=%-4d | format='%s'\n", size_limit, format); + ccprintf("size_limit=%-4zd | format='%s'\n", size_limit, format); ccprintf("expect ='%s' | expect_status=%d\n", expect ? expect : "NO_BYTES_TOUCHED", expect_ret); |