summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-02-22 22:44:32 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-02-22 22:51:09 (GMT)
commit96ffc06f72f693d80f05059a1f0e5ca9007d5f1b (patch)
tree8f4587b3132a422757280f3d4f7bc302230efd7c /dir.c
parent3733e6946465d4a3a1d89026a5ec911d3af339ab (diff)
downloadgit-96ffc06f72f693d80f05059a1f0e5ca9007d5f1b.zip
git-96ffc06f72f693d80f05059a1f0e5ca9007d5f1b.tar.gz
git-96ffc06f72f693d80f05059a1f0e5ca9007d5f1b.tar.bz2
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 'dir.c')
-rw-r--r--dir.c16
1 files changed, 3 insertions, 13 deletions
diff --git a/dir.c b/dir.c
index cb5bff8..c1bc538 100644
--- a/dir.c
+++ b/dir.c
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,
parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
if (flags & EXC_FLAG_MUSTBEDIR) {
- char *s;
- x = xmalloc(sizeof(*x) + patternlen + 1);
- s = (char *)(x+1);
- memcpy(s, string, patternlen);
- s[patternlen] = '\0';
- x->pattern = s;
+ FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
} else {
x = xmalloc(sizeof(*x));
x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
}
uc->dir_created++;
- d = xmalloc(sizeof(*d) + len + 1);
- memset(d, 0, sizeof(*d));
- memcpy(d->name, name, len);
- d->name[len] = '\0';
+ FLEX_ALLOC_MEM(d, name, name, len);
ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
{
struct dir_entry *ent;
- ent = xmalloc(sizeof(*ent) + len + 1);
+ FLEX_ALLOC_MEM(ent, name, pathname, len);
ent->len = len;
- memcpy(ent->name, pathname, len);
- ent->name[len] = 0;
return ent;
}