diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-22 18:51:59 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-05-06 11:53:17 -0600 |
commit | 24e5d0ec1219e2dbb4b9510ef20833092a2b3871 (patch) | |
tree | 01bbcc34c6190fa1c35b2625e9ba1744b1447606 /libmisc/tests | |
parent | f09b7435b3a5222597d27238226d23ec0cbd5bd2 (diff) |
wip: Build with -Wconversionlukeshu/safe-conversion
I think this found a real bug in the dhcp packet parser.
I don't think anything called lib9p_str{,n}() values that could be big
enough, but their bounds-checking was broken.
Diffstat (limited to 'libmisc/tests')
-rw-r--r-- | libmisc/tests/test_obj_nest.c | 12 | ||||
-rw-r--r-- | libmisc/tests/test_rand.c | 6 |
2 files changed, 13 insertions, 5 deletions
diff --git a/libmisc/tests/test_obj_nest.c b/libmisc/tests/test_obj_nest.c index bb9d6de..be303f2 100644 --- a/libmisc/tests/test_obj_nest.c +++ b/libmisc/tests/test_obj_nest.c @@ -5,6 +5,10 @@ */ #include <string.h> /* for memcpy() */ +#include <limits.h> /* for SSIZE_MAX, not set by newlib */ +#ifndef SSIZE_MAX +#define SSIZE_MAX (SIZE_MAX >> 1) +#endif #include <libmisc/obj.h> @@ -45,8 +49,10 @@ static ssize_t myclass_read(struct myclass *self, void *buf, size_t count) { test_assert(self); if (count > self->len) count = self->len; + if (count > SSIZE_MAX) + count = SSIZE_MAX; memcpy(buf, self->buf, count); - return count; + return (ssize_t)count; } static ssize_t myclass_write(struct myclass *self, void *buf, size_t count) { @@ -55,9 +61,11 @@ static ssize_t myclass_write(struct myclass *self, void *buf, size_t count) { return -1; if (count > sizeof(self->buf)) count = sizeof(self->buf); + if (count > SSIZE_MAX) + count = SSIZE_MAX; memcpy(self->buf, buf, count); self->len = count; - return count; + return (ssize_t)count; } /* main test body *************************************************************/ diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c index 02f4c1c..b98dacf 100644 --- a/libmisc/tests/test_rand.c +++ b/libmisc/tests/test_rand.c @@ -53,14 +53,14 @@ static void test_n(uint64_t cnt) { bool seen[MAX_SEE_ALL] = {0}; for (int i = 0; i < ROUNDS; i++) { uint64_t val = rand_uint63n(cnt); - sum += val; + sum += (double)val; test_assert(val < cnt); if (cnt < MAX_SEE_ALL) seen[val] = true; } if (cnt > 1) { - test_assert(sum/ROUNDS > 0.45*(cnt-1)); - test_assert(sum/ROUNDS < 0.55*(cnt-1)); + test_assert(sum/ROUNDS > 0.45*(double)(cnt-1)); + test_assert(sum/ROUNDS < 0.55*(double)(cnt-1)); } if (cnt < MAX_SEE_ALL) { for (uint64_t i = 0; i < cnt; i++) |