From 6b54245bbc8285b739293488f708430c914f70dc Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Fri, 24 Jun 2022 22:10:37 -0400 Subject: [PATCH] tests: Silence some warnings from `gcc --analyzer` --- tests/test29.c | 1 + tests/test35.c | 2 ++ tests/test41.c | 3 +++ tests/test43.c | 5 ++++- tests/test45.c | 4 +++- tests/test65.c | 7 +++++-- tests/test89.c | 2 ++ tests/test90.c | 7 +++++-- tests/test92.c | 4 ++++ tests/test93.c | 4 +++- tests/test96.c | 2 ++ 11 files changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/test29.c b/tests/test29.c index e579a0e2..13f2619b 100644 --- a/tests/test29.c +++ b/tests/test29.c @@ -45,6 +45,7 @@ int main() } /* now delete the list head */ + assert(head != NULL); printf("deleting head %shead->prev: %s", head->bname, head->prev->bname); DL_DELETE(head,head); DL_FOREACH(head,tmp) { diff --git a/tests/test35.c b/tests/test35.c index a9c82b9c..f3d73571 100644 --- a/tests/test35.c +++ b/tests/test35.c @@ -1,4 +1,5 @@ #include "uthash.h" +#include #include /* strcpy */ #include /* malloc */ #include /* printf */ @@ -16,6 +17,7 @@ int main() char label[6] = "hello"; for(i=0; i<10; i++) { elts[i].s = (char*)malloc(6UL); + assert(elts[i].s != NULL); strcpy(elts[i].s, "hello"); elts[i].s[0] = 'a' + i; printf("%d: %s\n", i, elts[i].s); diff --git a/tests/test41.c b/tests/test41.c index 88a1a96e..6b2174ee 100644 --- a/tests/test41.c +++ b/tests/test41.c @@ -33,6 +33,7 @@ int main() if (head != NULL) { printf("non-null head\n"); } + assert(head == NULL); /* test DL macros */ printf("DL macros\n"); @@ -50,6 +51,7 @@ int main() if (head != NULL) { printf("non-null head\n"); } + assert(head == NULL); /* test LL macros */ printf("LL macros\n"); @@ -67,6 +69,7 @@ int main() if (head != NULL) { printf("non-null head\n"); } + assert(head == NULL); return 0; } diff --git a/tests/test43.c b/tests/test43.c index 216f529a..7301c4c9 100644 --- a/tests/test43.c +++ b/tests/test43.c @@ -1,6 +1,8 @@ -#include #include "utarray.h" +#include +#include + typedef struct { int a; int b; @@ -20,6 +22,7 @@ int main() printf("push\n"); printf("length is %u\n", utarray_len(pairs)); ip = (intpair_t*)utarray_back(pairs); + assert(ip != NULL); printf("back is %d %d\n", ip->a, ip->b); utarray_pop_back(pairs); printf("pop\n"); diff --git a/tests/test45.c b/tests/test45.c index b0633f2b..042b3f96 100644 --- a/tests/test45.c +++ b/tests/test45.c @@ -1,5 +1,6 @@ -#include #include "utarray.h" +#include +#include int main() { @@ -23,6 +24,7 @@ int main() printf("\n"); utarray_extend_back(a); p = (int*)utarray_back(a); + assert(p != NULL); *p = 1000; p = NULL; while ( (p=(int*)utarray_next(a,p)) != NULL ) { diff --git a/tests/test65.c b/tests/test65.c index 5820a455..509152f6 100644 --- a/tests/test65.c +++ b/tests/test65.c @@ -1,6 +1,7 @@ -#include -#include #include "uthash.h" +#include +#include +#include // this is an example of how to do a LRU cache in C using uthash // https://troydhanson.github.io/uthash/ @@ -25,6 +26,8 @@ static void add_to_cache(const char *key, const char *value) } entry->key = strdup(key); entry->value = strdup(value); + assert(entry->key != NULL); + assert(entry->value != NULL); HASH_ADD_KEYPTR(hh, cache, entry->key, strlen(entry->key), entry); // prune the cache to MAX_CACHE_SIZE diff --git a/tests/test89.c b/tests/test89.c index 74d7ec48..fb7b7c0a 100644 --- a/tests/test89.c +++ b/tests/test89.c @@ -3,6 +3,7 @@ #include "utlist.h" +#include #include #include #include @@ -52,6 +53,7 @@ int main() for (i = 0; i < sizeof(timestamps)/sizeof(clock_time_t); i++) { struct netq_t *node = (struct netq_t *)malloc(sizeof *node); + assert(node != NULL); memset(node, '\0', sizeof *node); node->t = timestamps[i]; diff --git a/tests/test90.c b/tests/test90.c index 6fce121f..7b0da45a 100644 --- a/tests/test90.c +++ b/tests/test90.c @@ -20,7 +20,8 @@ int sort_func(const struct item *a, const struct item *b) int main() { size_t i; - struct item *p, *tmp; + struct item *p; + struct item *tmp; int total = 0; /** The sorted list */ @@ -30,9 +31,11 @@ int main() /* fill in the sorted list */ for(i=0; i<100; i++) { p = (struct item *)malloc(sizeof *p); + assert(p != NULL); + p->sort_field = (unsigned char *)malloc(sizeof(int)); + assert(p->sort_field != NULL); p->sort_field_len = sizeof(int); - p->sort_field = (unsigned char *)malloc(p->sort_field_len); *(int*)(void*)p->sort_field = counter++; HASH_ADD_KEYPTR_INORDER(hh, list, p->sort_field, p->sort_field_len, p, sort_func); diff --git a/tests/test92.c b/tests/test92.c index 10681945..12ac0863 100644 --- a/tests/test92.c +++ b/tests/test92.c @@ -1,3 +1,4 @@ +#include #include #define HASH_NONFATAL_OOM 1 @@ -66,6 +67,7 @@ int main() int i; int saved_cnt; + assert(user != NULL); user->id = id; #ifdef HASH_BLOOM @@ -112,6 +114,7 @@ int main() malloc_cnt = 1; for (id = 1; 1; ++id) { user = (example_user_t*)malloc(sizeof(example_user_t)); + assert(user != NULL); user->id = id; if (id >= 1000) { // prevent infinite, or too long of a loop here @@ -171,6 +174,7 @@ int main() for (i=0; i < saved_cnt; i++) { user = (example_user_t*)malloc(sizeof(example_user_t)); + assert(user != NULL); user->id = ++id; malloc_cnt = 20; // don't fail HASH_ADD_INT(users, id, user); diff --git a/tests/test93.c b/tests/test93.c index 81103a1a..f2b6d9a9 100644 --- a/tests/test93.c +++ b/tests/test93.c @@ -1,3 +1,4 @@ +#include #include #include @@ -41,7 +42,8 @@ static void alt_fatal(char const * s) { static void init_users(int need_malloc_cnt) { users = NULL; - example_user_t * user = (example_user_t*)malloc(sizeof(example_user_t)); + example_user_t *user = (example_user_t*)malloc(sizeof(example_user_t)); + assert(user != NULL); user->id = user_id; is_fatal = 0; malloc_cnt = need_malloc_cnt; diff --git a/tests/test96.c b/tests/test96.c index 700bdcf3..42a81f32 100644 --- a/tests/test96.c +++ b/tests/test96.c @@ -1,3 +1,4 @@ +#include #include #include @@ -34,6 +35,7 @@ int main() for (int i=0; i < 40; ++i) { struct clockface *elt = (struct clockface *)malloc(sizeof(*elt)); struct clockface *found = NULL; + assert(elt != NULL); elt->time = random_data[i]; HASH_FIND_INT(times, &elt->time, found); if (found) {