aboutsummaryrefslogtreecommitdiff
path: root/builtin/mktree.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-02-22 17:44:32 -0500
committerJunio C Hamano <gitster@pobox.com>2016-02-22 14:51:09 -0800
commit96ffc06f72f693d80f05059a1f0e5ca9007d5f1b (patch)
tree8f4587b3132a422757280f3d4f7bc302230efd7c /builtin/mktree.c
parent3733e6946465d4a3a1d89026a5ec911d3af339ab (diff)
downloadgit-96ffc06f72f693d80f05059a1f0e5ca9007d5f1b.tar.gz
git-96ffc06f72f693d80f05059a1f0e5ca9007d5f1b.tar.xz
convert trivial cases to FLEX_ARRAY macros
Using FLEX_ARRAY macros reduces the amount of manual computation size we have to do. It also ensures we don't overflow size_t, and it makes sure we write the same number of bytes that we allocated. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/mktree.c')
-rw-r--r--builtin/mktree.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/builtin/mktree.c b/builtin/mktree.c
index a964d6be5..b0aab6535 100644
--- a/builtin/mktree.c
+++ b/builtin/mktree.c
@@ -19,16 +19,17 @@ static int alloc, used;
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
{
struct treeent *ent;
- int len = strlen(path);
+ size_t len = strlen(path);
if (strchr(path, '/'))
die("path %s contains slash", path);
- ALLOC_GROW(entries, used + 1, alloc);
- ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
+ FLEX_ALLOC_MEM(ent, name, path, len);
ent->mode = mode;
ent->len = len;
hashcpy(ent->sha1, sha1);
- memcpy(ent->name, path, len+1);
+
+ ALLOC_GROW(entries, used + 1, alloc);
+ entries[used++] = ent;
}
static int ent_compare(const void *a_, const void *b_)