From 71d21f06eb45ea98c13cd1b4067dccfe84adac8b Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sun, 21 Sep 2025 15:50:31 +0200 Subject: [PATCH 1/3] ztest: Add validation of zassert strings Add __printf_like attribute to ztest assertion functions zassert, zassume, zexpect to validate format strings and arguments at compile time. Signed-off-by: Tim Pambor --- .../testsuite/ztest/include/zephyr/ztest_assert.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/subsys/testsuite/ztest/include/zephyr/ztest_assert.h b/subsys/testsuite/ztest/include/zephyr/ztest_assert.h index b3815871ffd39..6e72bb2de6df4 100644 --- a/subsys/testsuite/ztest/include/zephyr/ztest_assert.h +++ b/subsys/testsuite/ztest/include/zephyr/ztest_assert.h @@ -76,8 +76,9 @@ static inline bool z_zexpect_(bool cond, const char *file, int line) #else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */ -static inline bool z_zassert(bool cond, const char *default_msg, const char *file, int line, - const char *func, const char *msg, ...) +static inline __printf_like(6, 7) bool z_zassert(bool cond, const char *default_msg, + const char *file, int line, const char *func, + const char *msg, ...) { if (cond == false) { va_list vargs; @@ -100,8 +101,9 @@ static inline bool z_zassert(bool cond, const char *default_msg, const char *fil return true; } -static inline bool z_zassume(bool cond, const char *default_msg, const char *file, int line, - const char *func, const char *msg, ...) +static inline __printf_like(6, 7) bool z_zassume(bool cond, const char *default_msg, + const char *file, int line, const char *func, + const char *msg, ...) { if (cond == false) { va_list vargs; @@ -124,8 +126,9 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil return true; } -static inline bool z_zexpect(bool cond, const char *default_msg, const char *file, int line, - const char *func, const char *msg, ...) +static inline __printf_like(6, 7) bool z_zexpect(bool cond, const char *default_msg, + const char *file, int line, const char *func, + const char *msg, ...) { if (cond == false) { va_list vargs; From a2b8c984c6594c90a73ef0e32c352c8fefe9b512 Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sun, 21 Sep 2025 16:51:17 +0200 Subject: [PATCH 2/3] tests: zdsp: Correct format specifier in assert_strings strlen returns size_t, not int. Fix the format specifier to avoid warnings when building with -Wformat. Signed-off-by: Tim Pambor --- tests/subsys/dsp/print_format/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/subsys/dsp/print_format/src/main.c b/tests/subsys/dsp/print_format/src/main.c index fcaff86113a65..36e84d962fd81 100644 --- a/tests/subsys/dsp/print_format/src/main.c +++ b/tests/subsys/dsp/print_format/src/main.c @@ -9,7 +9,7 @@ #define float_multiplier(type) ((INT64_C(1) << (8 * sizeof(type) - 1)) - 1) #define assert_strings(expected, actual) \ - zexpect_equal(strlen(expected), strlen(actual), "Expected %d(%s), got %d(%s)", \ + zexpect_equal(strlen(expected), strlen(actual), "Expected %zu(%s), got %zu(%s)", \ strlen(expected), expected, strlen(actual), actual); \ zexpect_mem_equal(expected, actual, MIN(strlen(expected), strlen(actual)), \ "Expected '%s', got '%s'", expected, actual) From 5184fd5f5e1afad000d38d99a3fb002698654e5c Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sun, 21 Sep 2025 17:01:44 +0200 Subject: [PATCH 3/3] tests: ram_context_for_isr: fix format specifier for addresses Use %lx for printing addresses (uintptr_t) instead of %x. Signed-off-by: Tim Pambor --- .../ram_context_for_isr/src/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/application_development/ram_context_for_isr/src/main.c b/tests/application_development/ram_context_for_isr/src/main.c index 1a1164794ccee..64955e7e2a2f3 100644 --- a/tests/application_development/ram_context_for_isr/src/main.c +++ b/tests/application_development/ram_context_for_isr/src/main.c @@ -39,16 +39,16 @@ static void test_irq_callback(const struct device *dev, void *user_data) /* Check that the function and its call stack are in RAM */ zassert_true(func_addr >= CONFIG_SRAM_BASE_ADDRESS && func_addr <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE * 1024, - "%s is not in RAM! Address: 0x%x", __func__, func_addr); + "%s is not in RAM! Address: 0x%lx", __func__, func_addr); zassert_true(driver_isr_addr >= CONFIG_SRAM_BASE_ADDRESS && driver_isr_addr <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE * 1024, - "fake_driver_isr is not in RAM! Address: 0x%x", driver_isr_addr); + "fake_driver_isr is not in RAM! Address: 0x%lx", driver_isr_addr); zassert_true(arch_isr_wrapper_addr >= CONFIG_SRAM_BASE_ADDRESS && arch_isr_wrapper_addr <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE * 1024, - "arch_isr_wrapper_addr is not in RAM! Address: 0x%x", arch_isr_wrapper_addr); + "arch_isr_wrapper_addr is not in RAM! Address: 0x%lx", arch_isr_wrapper_addr); TC_PRINT("Callback function address: 0x%lx\n", func_addr); TC_PRINT("Driver ISR address: 0x%lx\n", driver_isr_addr); @@ -63,7 +63,7 @@ ZTEST(ram_context_for_isr, test_fake_driver_in_ram) zassert_true(dev_addr >= CONFIG_SRAM_BASE_ADDRESS && dev_addr <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE * 1024, - "fake driver device is not in RAM! Address: 0x%x", dev_addr); + "fake driver device is not in RAM! Address: 0x%lx", dev_addr); TC_PRINT("Fake driver device address: 0x%lx\n", dev_addr);