diff --git a/test/include/test/test.h b/test/include/test/test.h index fd0e5265..07f58a47 100644 --- a/test/include/test/test.h +++ b/test/include/test/test.h @@ -336,3 +336,5 @@ test_status_t p_test_no_malloc_init(test_t *t, ...); void p_test_init(const char *name); void p_test_fini(void); void p_test_fail(const char *prefix, const char *message); + +void strncpy_cond(void *dst, const char *src, bool cond); diff --git a/test/src/test.c b/test/src/test.c index f97ce4d1..4583e55a 100644 --- a/test/src/test.c +++ b/test/src/test.c @@ -232,3 +232,16 @@ p_test_fail(const char *prefix, const char *message) { malloc_cprintf(NULL, NULL, "%s%s\n", prefix, message); test_status = test_status_fail; } + +void +strncpy_cond(void *dst, const char *src, bool cond) { + if (cond) { + /* + * Avoid strcpy and explicitly set length to 0 because the + * `stringop-overflow` check may warn even if the specific test + * is unreachable. + */ + size_t n = cond ? strlen(src) + 1 : 0; + strncpy(dst, src, n); + } +} diff --git a/test/unit/log.c b/test/unit/log.c index a52bd737..10f45bc1 100644 --- a/test/unit/log.c +++ b/test/unit/log.c @@ -2,13 +2,18 @@ #include "jemalloc/internal/log.h" +static void +update_log_var_names(const char *names) { + strncpy_cond(log_var_names, names, config_log); +} + static void expect_no_logging(const char *names) { log_var_t log_l1 = LOG_VAR_INIT("l1"); log_var_t log_l2 = LOG_VAR_INIT("l2"); log_var_t log_l2_a = LOG_VAR_INIT("l2.a"); - strcpy(log_var_names, names); + update_log_var_names(names); int count = 0; @@ -50,7 +55,7 @@ TEST_BEGIN(test_log_enabled_direct) { int count; count = 0; - strcpy(log_var_names, "l1"); + update_log_var_names("l1"); for (int i = 0; i < 10; i++) { log_do_begin(log_l1) count++; @@ -59,7 +64,7 @@ TEST_BEGIN(test_log_enabled_direct) { assert_d_eq(count, 10, "Mis-logged!"); count = 0; - strcpy(log_var_names, "l1.a"); + update_log_var_names("l1.a"); for (int i = 0; i < 10; i++) { log_do_begin(log_l1_a) count++; @@ -68,7 +73,7 @@ TEST_BEGIN(test_log_enabled_direct) { assert_d_eq(count, 10, "Mis-logged!"); count = 0; - strcpy(log_var_names, "l1.a|abc|l2|def"); + update_log_var_names("l1.a|abc|l2|def"); for (int i = 0; i < 10; i++) { log_do_begin(log_l1_a) count++; @@ -85,7 +90,7 @@ TEST_END TEST_BEGIN(test_log_enabled_indirect) { test_skip_if(!config_log); atomic_store_b(&log_init_done, true, ATOMIC_RELAXED); - strcpy(log_var_names, "l0|l1|abc|l2.b|def"); + update_log_var_names("l0|l1|abc|l2.b|def"); /* On. */ log_var_t log_l1 = LOG_VAR_INIT("l1"); @@ -135,7 +140,7 @@ TEST_END TEST_BEGIN(test_log_enabled_global) { test_skip_if(!config_log); atomic_store_b(&log_init_done, true, ATOMIC_RELAXED); - strcpy(log_var_names, "abc|.|def"); + update_log_var_names("abc|.|def"); log_var_t log_l1 = LOG_VAR_INIT("l1"); log_var_t log_l2_a_a = LOG_VAR_INIT("l2.a.a");