From 00080db82508e04b4342200fbabfac5e85728211 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 7 May 2025 15:08:48 +0200 Subject: [PATCH] test: add assert()s to fix analyzer warnings Reduce the analyzer spam a bit by adding some extra asserts. Since it's test code, we can't have enough of them anyways ;-) Signed-off-by: Enrico Weigelt, metux IT consult --- test/damage/primitives.c | 8 ++++++-- test/list.c | 4 ++++ test/string.c | 3 +++ test/test_xkb.c | 11 +++++++++++ test/xi2/protocol-common.c | 1 + test/xi2/xi2.c | 12 +++++++++--- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/test/damage/primitives.c b/test/damage/primitives.c index d60da8d04..810fc1340 100644 --- a/test/damage/primitives.c +++ b/test/damage/primitives.c @@ -75,8 +75,10 @@ get_image(struct test_setup *setup, xcb_drawable_t drawable) assert(reply->depth == 24); assert(len == 4 * setup->width * setup->height); - uint32_t *result = malloc(sizeof(uint32_t) * - setup->width * setup->height); + uint32_t *result = calloc(setup->width * setup->height, sizeof(uint32_t)); + if (!result) + return NULL; + memcpy(result, data, len); free(reply); @@ -93,7 +95,9 @@ compute_expected_damage(struct test_setup *setup) uint32_t *results = get_image(setup, setup->d); bool any_modified_pixels = false; + assert(results); for (int i = 0; i < setup->width * setup->height; i++) { + assert(setup->start_drawable_contents); if (results[i] != setup->start_drawable_contents[i]) { setup->expected[i / 32] |= 1 << (i % 32); any_modified_pixels = true; diff --git a/test/list.c b/test/list.c index b93cfd43a..29cd1b997 100644 --- a/test/list.c +++ b/test/list.c @@ -242,6 +242,7 @@ test_nt_list_append(void) struct foo *item; for (item = foo, i = 1; i <= 10; i++, item++) { + assert(item); item->a = i; item->b = i * 2; nt_list_init(item, next); @@ -273,6 +274,7 @@ test_nt_list_insert(void) { int i; struct foo *foo = calloc(10, sizeof(struct foo)); + assert(foo); struct foo *item; foo->a = 1; @@ -309,6 +311,8 @@ test_nt_list_delete(void) { int i = 1; struct foo *list = calloc(10, sizeof(struct foo)); + assert(list); + struct foo *foo = list; struct foo *item, *tmp; struct foo *empty_list = foo; diff --git a/test/string.c b/test/string.c index be971e623..a9a2a5d0f 100644 --- a/test/string.c +++ b/test/string.c @@ -52,6 +52,8 @@ strndup_checks(void) char *firsthalf = strndup(sample, 8); char *secondhalf = strndup(sample + 8, 8); + assert(firsthalf); + assert(secondhalf); assert(strcmp(firsthalf, "01234567") == 0); assert(strcmp(secondhalf, "89abcdef") == 0); @@ -59,6 +61,7 @@ strndup_checks(void) free(secondhalf); allofit = strndup(sample, 20); + assert(allofit); assert(strcmp(allofit, sample) == 0); free(allofit); } diff --git a/test/test_xkb.c b/test/test_xkb.c index 11a0f4ce8..19f6b2fb1 100644 --- a/test/test_xkb.c +++ b/test/test_xkb.c @@ -153,6 +153,17 @@ xkb_set_get_rules_test(void) /* This test is iffy, because strictly we may be comparing against already * freed memory */ + assert(rmlvo.rules); + assert(rmlvo.model); + assert(rmlvo.layout); + assert(rmlvo.variant); + assert(rmlvo.options); + assert(rmlvo_backup.rules); + assert(rmlvo_backup.model); + assert(rmlvo_backup.layout); + assert(rmlvo_backup.variant); + assert(rmlvo_backup.options); + assert(strcmp(rmlvo.rules, rmlvo_backup.rules) == 0); assert(strcmp(rmlvo.model, rmlvo_backup.model) == 0); assert(strcmp(rmlvo.layout, rmlvo_backup.layout) == 0); diff --git a/test/xi2/protocol-common.c b/test/xi2/protocol-common.c index 7b6ea1eb7..f19cffa2f 100644 --- a/test/xi2/protocol-common.c +++ b/test/xi2/protocol-common.c @@ -57,6 +57,7 @@ fake_init_sprite(DeviceIntPtr dev) sprite->spriteTraceSize = 10; sprite->spriteTrace = calloc(sprite->spriteTraceSize, sizeof(WindowPtr)); + assert(sprite->spriteTrace); sprite->spriteTraceGood = 1; sprite->spriteTrace[0] = &root; sprite->hot.x = SPRITE_X; diff --git a/test/xi2/xi2.c b/test/xi2/xi2.c index b49a8fe8d..00bcccebd 100644 --- a/test/xi2/xi2.c +++ b/test/xi2/xi2.c @@ -59,10 +59,12 @@ xi2mask_test(void) assert(xi2mask_num_masks(xi2mask) == xi2mask->nmasks); mask = calloc(1, xi2mask_mask_size(xi2mask)); + assert(mask); + /* ensure zeros */ for (i = 0; i < xi2mask_num_masks(xi2mask); i++) { const unsigned char *m = xi2mask_get_one_mask(xi2mask, i); - + assert(mask); assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0); } @@ -77,7 +79,10 @@ xi2mask_test(void) m = xi2mask_get_one_mask(xi2mask, i); SetBit(mask, i); - assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0); + assert(mask); + assert(m); + if (mask) + assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0); ClearBit(mask, i); } @@ -85,6 +90,7 @@ xi2mask_test(void) for (i = 0; i < xi2mask_num_masks(xi2mask); i++) { const unsigned char *m = xi2mask_get_one_mask(xi2mask, i); + assert(mask); assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) != 0); xi2mask_zero(xi2mask, i); assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0); @@ -97,7 +103,7 @@ xi2mask_test(void) for (i = 0; i < xi2mask_num_masks(xi2mask); i++) { const unsigned char *m = xi2mask_get_one_mask(xi2mask, i); - + assert(mask); assert(memcmp(mask, m, xi2mask_mask_size(xi2mask)) == 0); }