git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Code AI <techsupport@mycode.ai>
Subject: [PATCH 1/6] test-hashmap: use ALLOC_ARRAY rather than bare malloc
Date: Wed, 14 Feb 2018 13:05:46 -0500	[thread overview]
Message-ID: <20180214180545.GA9919@sigill.intra.peff.net> (raw)
In-Reply-To: <20180214180322.GA9190@sigill.intra.peff.net>

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>
---
The sizeof() thing came from Code AI's original email. I'm happy to
include a Reported-by there, but I wasn't sure of the correct entity to
credit. :)

 t/helper/test-hashmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index 1145d51671..b36886bf35 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -85,8 +85,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 	unsigned int *hashes;
 	unsigned int i, j;
 
-	entries = malloc(TEST_SIZE * sizeof(struct test_entry *));
-	hashes = malloc(TEST_SIZE * sizeof(int));
+	ALLOC_ARRAY(entries, TEST_SIZE);
+	ALLOC_ARRAY(hashes, TEST_SIZE);
 	for (i = 0; i < TEST_SIZE; i++) {
 		snprintf(buf, sizeof(buf), "%i", i);
 		entries[i] = alloc_test_entry(0, buf, strlen(buf), "", 0);
-- 
2.16.1.464.gc4bae515b7


  reply	other threads:[~2018-02-14 18:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-14 18:03 [PATCH 0/6] minor test-hashmap fixes Jeff King
2018-02-14 18:05 ` Jeff King [this message]
2018-02-14 18:47   ` [PATCH 1/6] test-hashmap: use ALLOC_ARRAY rather than bare malloc Code AI
2018-02-14 18:06 ` [PATCH 2/6] test-hashmap: check allocation computation for overflow Jeff King
2018-02-14 18:06 ` [PATCH 3/6] test-hashmap: use xsnprintf rather than snprintf Jeff King
2018-02-14 18:07 ` [PATCH 4/6] test-hashmap: use strbuf_getline rather than fgets Jeff King
2018-02-14 18:08 ` [PATCH 5/6] test-hashmap: simplify alloc_test_entry Jeff King
2018-02-14 19:01   ` Junio C Hamano
2018-02-14 18:08 ` [PATCH 6/6] test-hashmap: use "unsigned int" for hash storage Jeff King
2018-02-14 18:41 ` [PATCH 0/6] minor test-hashmap fixes Stefan Beller
2018-02-14 18:48   ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180214180545.GA9919@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=techsupport@mycode.ai \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).