Skip to content

Commit aef6cf1

Browse files
peffgitster
authored andcommitted
test-hashmap: use ALLOC_ARRAY rather than bare malloc
These two array allocations have several minor flaws: - they use bare malloc, rather than our error-checking xmalloc - they do a bare multiplication to determine the total size (which in theory can overflow, though in this case the sizes are all constants) - they use sizeof(type), but the type in the second one doesn't match the actual array (though it's "int" versus "unsigned int", which are guaranteed by C99 to have the same size) None of these are likely to be problems in practice, and this is just a test helper. But since people often look at test helpers as reference code, we should do our best to model the recommended techniques. Switching to ALLOC_ARRAY fixes all three. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3013dff commit aef6cf1

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

t/helper/test-hashmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
8585
unsigned int *hashes;
8686
unsigned int i, j;
8787

88-
entries = malloc(TEST_SIZE * sizeof(struct test_entry *));
89-
hashes = malloc(TEST_SIZE * sizeof(int));
88+
ALLOC_ARRAY(entries, TEST_SIZE);
89+
ALLOC_ARRAY(hashes, TEST_SIZE);
9090
for (i = 0; i < TEST_SIZE; i++) {
9191
snprintf(buf, sizeof(buf), "%i", i);
9292
entries[i] = alloc_test_entry(0, buf, strlen(buf), "", 0);

0 commit comments

Comments
 (0)