diff options
author | David Robillard <d@drobilla.net> | 2018-11-13 10:57:59 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-11-13 10:57:59 +0100 |
commit | 734ca87bf6608ce018b995acb05e4ac61c30d8a9 (patch) | |
tree | 4807d2c3d36ef62c92f2ace3cc60181855525a6b /test | |
parent | 5944e39578b2a5475e74186a1a965b14a9492d97 (diff) | |
download | zix-734ca87bf6608ce018b995acb05e4ac61c30d8a9.tar.gz zix-734ca87bf6608ce018b995acb05e4ac61c30d8a9.tar.bz2 zix-734ca87bf6608ce018b995acb05e4ac61c30d8a9.zip |
Fix out of bounds accesses in bitset test
Diffstat (limited to 'test')
-rw-r--r-- | test/bitset_test.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/test/bitset_test.c b/test/bitset_test.c index 0bb3cd0..0fbbcab 100644 --- a/test/bitset_test.c +++ b/test/bitset_test.c @@ -22,6 +22,8 @@ #define N_BITS 256 #define N_ELEMS (ZIX_BITSET_ELEMS(N_BITS)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + static int test_fail(const char* fmt, ...) { @@ -57,24 +59,29 @@ main(int argc, char** argv) for (size_t i = 0; i <= N_BITS; ++i) { const size_t count = zix_bitset_count_up_to(b, t, i); if (count != i) { - return test_fail("Count up to %zu is %zu != %zu\n", i, count, i); + return test_fail("Count to %zu is %zu != %zu\n", i, count, i); } } for (size_t i = 0; i <= N_BITS; ++i) { - zix_bitset_reset(b, t, i); + if (i < N_BITS) { + zix_bitset_reset(b, t, i); + } const size_t count = zix_bitset_count_up_to(b, t, i); if (count != 0) { - return test_fail("Count up to %zu is %zu != %zu\n", i, count, 0); + return test_fail("Count to %zu is %zu != %zu\n", i, count, 0); } } zix_bitset_clear(b, t, N_BITS); for (size_t i = 0; i <= N_BITS; i += 2) { - zix_bitset_set(b, t, i); - const size_t count = zix_bitset_count_up_to(b, t, i + 1); - if (count != i / 2 + 1) { - return test_fail("Count up to %zu is %zu != %zu\n", i, count, i / 2 + 1); + if (i < N_BITS) { + zix_bitset_set(b, t, i); + } + const size_t count = zix_bitset_count_up_to(b, t, i + 1); + const size_t result = MIN(N_BITS / 2, i / 2 + 1); + if (count != result) { + return test_fail("Count to %zu is %zu != %zu\n", i, count, result); } } |