Skip to content

Commit 2f538e7

Browse files
zuiderkwasthpatro
authored andcommitted
Fix UBSAN run for hashtable unittest (#2126)
The unit test declared an array of size 1 and used pointers to elements outside the array. This is fine because the pointers are never dereferenced, but undefined-sanitizer complains. Now, instead allocate a huge array to make sure all pointers into it are valid. Example failure: https://github.com/valkey-io/valkey/actions/runs/15175084203/job/42673713108#step:10:123 Signed-off-by: Viktor Söderqvist <[email protected]> Signed-off-by: Harkrishn Patro <[email protected]>
1 parent 35c0138 commit 2f538e7

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/unit/test_hashtable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,7 @@ int test_random_entry_sparse_table(int argc, char **argv, int flags) {
902902
monotonicInit();
903903

904904
/* Populate */
905-
unsigned values[1]; /* We don't need to allocate the full size (count) on
906-
* the stack, because the array is never accessed. We
907-
* only use pointers to the array. */
905+
unsigned *values = zmalloc(sizeof(unsigned) * count);
908906
for (size_t j = 0; j < count; j++) {
909907
TEST_ASSERT(hashtableAdd(ht, &values[j]));
910908
}
@@ -942,6 +940,7 @@ int test_random_entry_sparse_table(int argc, char **argv, int flags) {
942940
TEST_ASSERT(us <= us0 * 10);
943941
}
944942
hashtableRelease(ht);
943+
zfree(values);
945944
return 0;
946945
}
947946

0 commit comments

Comments
 (0)