From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 2/6] test-hashmap: check allocation computation for overflow
Date: Wed, 14 Feb 2018 13:06:34 -0500 [thread overview]
Message-ID: <20180214180634.GB9919@sigill.intra.peff.net> (raw)
In-Reply-To: <20180214180322.GA9190@sigill.intra.peff.net>
When we allocate the test_entry flex-struct, we have to add
up all of the elements that go into the flex array. If these
were to overflow a size_t, this would allocate a too-small
buffer, which we would then overflow in our memcpy steps.
Since this is just a test-helper, it probably doesn't matter
in practice, but we should model the correct technique by
using the st_add() macros.
Unfortunately, we cannot use the FLEX_ALLOC() macros here,
because we are stuffing two different buffers into a single
flex array.
While we're here, let's also swap out "malloc" for our
error-checking "xmalloc", and use the preferred
"sizeof(*var)" instead of "sizeof(type)".
Signed-off-by: Jeff King <peff@peff.net>
---
t/helper/test-hashmap.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index b36886bf35..2100877c2b 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -32,8 +32,7 @@ static int test_entry_cmp(const void *cmp_data,
static struct test_entry *alloc_test_entry(int hash, char *key, int klen,
char *value, int vlen)
{
- struct test_entry *entry = malloc(sizeof(struct test_entry) + klen
- + vlen + 2);
+ struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
hashmap_entry_init(entry, hash);
memcpy(entry->key, key, klen + 1);
memcpy(entry->key + klen + 1, value, vlen + 1);
--
2.16.1.464.gc4bae515b7
next prev parent reply other threads:[~2018-02-14 18:06 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 ` [PATCH 1/6] test-hashmap: use ALLOC_ARRAY rather than bare malloc Jeff King
2018-02-14 18:47 ` Code AI
2018-02-14 18:06 ` Jeff King [this message]
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=20180214180634.GB9919@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
/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).