diff options
| author | Mattias Andrée <m@maandree.se> | 2026-05-17 17:56:20 +0200 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-05-17 17:56:20 +0200 |
| commit | f87b3dc9431b257943fe6c416b0915ea8df44186 (patch) | |
| tree | b2d26ed04341ab5b5b35d9bccef972650c238ba3 | |
| parent | Silence warnings (diff) | |
| download | libenv-f87b3dc9431b257943fe6c416b0915ea8df44186.tar.gz libenv-f87b3dc9431b257943fe6c416b0915ea8df44186.tar.bz2 libenv-f87b3dc9431b257943fe6c416b0915ea8df44186.tar.xz | |
Fix complaints from sanitizer in the test code1.0.3.1
Signed-off-by: Mattias Andrée <m@maandree.se>
| -rw-r--r-- | config.mk | 10 | ||||
| -rw-r--r-- | test.c | 80 |
2 files changed, 64 insertions, 26 deletions
@@ -3,6 +3,12 @@ MANPREFIX = $(PREFIX)/share/man CC = c99 +COMMON_SANITIZE = -fsanitize=alignment,shift,signed-integer-overflow,object-size,null,undefined,bounds,address +CLANG_SANITIZE = -O1 $(COMMON_SANITIZE),cfi -flto -fvisibility=hidden -fno-sanitize-trap=cfi +GCC_SANITIZE = -O1 $(COMMON_SANITIZE) +#SANITIZE = $(CLANG_SANITIZE) +#SANITIZE = $(GCC_SANITIZE) + CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE -CFLAGS = -LDFLAGS = +CFLAGS = $(SANITIZE) +LDFLAGS = $(SANITIZE) @@ -58,49 +58,81 @@ check(size_t count, char **strings, ...) } +static void +cnst_check(size_t count, const char **strings, ...) +{ + size_t i, len; + const char *expect; + va_list args; + va_start(args, strings); + for (i = 0; (expect = va_arg(args, const char *)); i++) { + len = strlen(expect); + assert(strings[i]); + assert(!strncmp(strings[i], expect, len)); + assert(!strings[i][len] || strings[i][len] == '='); + } + va_end(args); + assert(!strings[i]); + assert(count == i); +} + + #define CHECK(CALL, ...)\ do {\ + const char *cstrings[8];\ char *strings[8];\ char *allocations[8];\ size_t i;\ char *p;\ - assert((strings[0] = allocations[0] = strdup("a=")));\ - assert((strings[1] = allocations[1] = strdup("aa=1")));\ - assert((strings[2] = allocations[2] = strdup("b=12")));\ - assert((strings[3] = allocations[3] = strdup("bb=34")));\ - assert((strings[4] = allocations[4] = strdup("BB")));\ - assert((strings[5] = allocations[5] = strdup("x=5")));\ - assert((strings[6] = allocations[6] = strdup("xx")));\ - strings[7] = allocations[7] = NULL;\ + assert((cstrings[0] = strings[0] = allocations[0] = strdup("a=")));\ + assert((cstrings[1] = strings[1] = allocations[1] = strdup("aa=1")));\ + assert((cstrings[2] = strings[2] = allocations[2] = strdup("b=12")));\ + assert((cstrings[3] = strings[3] = allocations[3] = strdup("bb=34")));\ + assert((cstrings[4] = strings[4] = allocations[4] = strdup("BB")));\ + assert((cstrings[5] = strings[5] = allocations[5] = strdup("x=5")));\ + assert((cstrings[6] = strings[6] = allocations[6] = strdup("xx")));\ + cstrings[7] = strings[7] = allocations[7] = NULL;\ if (!variable_version) {\ for (i = 0; strings[i]; i++) {\ for (p = strings[i]; *p && *p != '='; p++);\ *p = '\0';\ }\ }\ - check(CALL, strings, __VA_ARGS__);\ + if (dealloc_version)\ + check(CALL, strings, __VA_ARGS__);\ + else if (dealloc_version)\ + cnst_check(CALL, cstrings, __VA_ARGS__);\ dealloc(dealloc_version ? strings : allocations);\ } while (0) +#define TEST_FILTER(FUN, STRS)\ + do {\ + CHECK((*function->FUN)(STRS, LIBENV_END), NULL); /* each contain all of nothing, so remove all */\ + if (!variable_version) {\ + CHECK((*function->FUN)(STRS, 3, LIBENV_END), "a", "aa", "b", "bb", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 2, LIBENV_END), "a", "b", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 1, 2, 3, LIBENV_END), "a", "aa", "b", "bb", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 1, 2, LIBENV_END), "a", "b", "bb", "x", "xx", NULL);\ + } else {\ + CHECK((*function->FUN)(STRS, 3, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 2, LIBENV_END), "a", "b", "BB", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 1, 2, 3, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 1, 2, LIBENV_END), "a", "b", "bb", "BB", "x", "xx", NULL);\ + }\ + CHECK((*function->FUN)(STRS, 10, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 64, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL);\ + CHECK((*function->FUN)(STRS, 100000, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL);\ + } while (0) + + static void test_filter(int dealloc_version, int variable_version, const union list_process_function *function) { - CHECK((*function->dyn)(strings, LIBENV_END), NULL); /* each contain all of nothing, so remove all */ - if (!variable_version) { - CHECK((*function->dyn)(strings, 3, LIBENV_END), "a", "aa", "b", "bb", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 2, LIBENV_END), "a", "b", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 1, 2, 3, LIBENV_END), "a", "aa", "b", "bb", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 1, 2, LIBENV_END), "a", "b", "bb", "x", "xx", NULL); - } else { - CHECK((*function->dyn)(strings, 3, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 2, LIBENV_END), "a", "b", "BB", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 1, 2, 3, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 1, 2, LIBENV_END), "a", "b", "bb", "BB", "x", "xx", NULL); - } - CHECK((*function->dyn)(strings, 10, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 64, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL); - CHECK((*function->dyn)(strings, 100000, LIBENV_END), "a", "aa", "b", "bb", "BB", "x", "xx", NULL); + if (dealloc_version) + TEST_FILTER(dyn, strings); + else + TEST_FILTER(cnst, cstrings); } |
