Skip to content

Commit

Permalink
tests: Silence some warnings from gcc --analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Quuxplusone committed Jul 8, 2022
1 parent bb83b06 commit 6b54245
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions tests/test29.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions tests/test35.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "uthash.h"
#include <assert.h>
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests/test41.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int main()
if (head != NULL) {
printf("non-null head\n");
}
assert(head == NULL);

/* test DL macros */
printf("DL macros\n");
Expand All @@ -50,6 +51,7 @@ int main()
if (head != NULL) {
printf("non-null head\n");
}
assert(head == NULL);

/* test LL macros */
printf("LL macros\n");
Expand All @@ -67,6 +69,7 @@ int main()
if (head != NULL) {
printf("non-null head\n");
}
assert(head == NULL);

return 0;
}
5 changes: 4 additions & 1 deletion tests/test43.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include "utarray.h"

#include <assert.h>
#include <stdio.h>

typedef struct {
int a;
int b;
Expand All @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion tests/test45.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include "utarray.h"
#include <assert.h>
#include <stdio.h>

int main()
{
Expand All @@ -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 ) {
Expand Down
7 changes: 5 additions & 2 deletions tests/test65.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string.h>
#include <stdio.h>
#include "uthash.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>

// this is an example of how to do a LRU cache in C using uthash
// https://troydhanson.github.io/uthash/
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/test89.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "utlist.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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];

Expand Down
7 changes: 5 additions & 2 deletions tests/test90.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/test92.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>

#define HASH_NONFATAL_OOM 1
Expand Down Expand Up @@ -66,6 +67,7 @@ int main()
int i;
int saved_cnt;

assert(user != NULL);
user->id = id;

#ifdef HASH_BLOOM
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/test93.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <setjmp.h>

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/test96.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b54245

Please sign in to comment.