test-hashmap: use xsnprintf rather than snprintf
authorJeff King <peff@peff.net>
Wed, 14 Feb 2018 18:06:57 +0000 (13:06 -0500)
committerJunio C Hamano <gitster@pobox.com>
Wed, 14 Feb 2018 18:31:09 +0000 (10:31 -0800)
In general, using a bare snprintf can truncate the resulting
buffer, leading to confusing results. In this case we know
that our buffer is sized large enough to accommodate our
loop, so there's no bug. However, we should use xsnprintf()
to document (and check) that assumption, and to model good
practice to people reading the code.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-hashmap.c
index 2100877c2bc1bd7b3e2c11595c6ac6667230fc30..28b913fbd66c24536afe6060d873e4e5bf0b349b 100644 (file)
@@ -87,7 +87,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
        ALLOC_ARRAY(entries, TEST_SIZE);
        ALLOC_ARRAY(hashes, TEST_SIZE);
        for (i = 0; i < TEST_SIZE; i++) {
-               snprintf(buf, sizeof(buf), "%i", i);
+               xsnprintf(buf, sizeof(buf), "%i", i);
                entries[i] = alloc_test_entry(0, buf, strlen(buf), "", 0);
                hashes[i] = hash(method, i, entries[i]->key);
        }