Skip to content

Commit 96ffc06

Browse files
peffgitster
authored andcommitted
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>
1 parent 3733e69 commit 96ffc06

File tree

17 files changed

+35
-82
lines changed

17 files changed

+35
-82
lines changed

attr.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
9393
if (invalid_attr_name(name, len))
9494
return NULL;
9595

96-
a = xmalloc(sizeof(*a) + len + 1);
97-
memcpy(a->name, name, len);
98-
a->name[len] = 0;
96+
FLEX_ALLOC_MEM(a, name, name, len);
9997
a->h = hval;
10098
a->next = git_attr_hash[pos];
10199
a->attr_nr = attr_nr++;

builtin/blame.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
459459
static struct origin *make_origin(struct commit *commit, const char *path)
460460
{
461461
struct origin *o;
462-
size_t pathlen = strlen(path) + 1;
463-
o = xcalloc(1, sizeof(*o) + pathlen);
462+
FLEX_ALLOC_STR(o, path, path);
464463
o->commit = commit;
465464
o->refcnt = 1;
466465
o->next = commit->util;
467466
commit->util = o;
468-
memcpy(o->path, path, pathlen); /* includes NUL */
469467
return o;
470468
}
471469

builtin/help.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)
171171
static void add_man_viewer(const char *name)
172172
{
173173
struct man_viewer_list **p = &man_viewer_list;
174-
size_t len = strlen(name);
175174

176175
while (*p)
177176
p = &((*p)->next);
178-
*p = xcalloc(1, (sizeof(**p) + len + 1));
179-
memcpy((*p)->name, name, len); /* NUL-terminated by xcalloc */
177+
FLEX_ALLOC_STR(*p, name, name);
180178
}
181179

182180
static int supported_man_viewer(const char *name, size_t len)
@@ -190,9 +188,8 @@ static void do_add_man_viewer_info(const char *name,
190188
size_t len,
191189
const char *value)
192190
{
193-
struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
194-
195-
memcpy(new->name, name, len); /* NUL-terminated by xcalloc */
191+
struct man_viewer_info_list *new;
192+
FLEX_ALLOC_MEM(new, name, name, len);
196193
new->info = xstrdup(value);
197194
new->next = man_viewer_info_list;
198195
man_viewer_info_list = new;

builtin/mktree.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ static int alloc, used;
1919
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
2020
{
2121
struct treeent *ent;
22-
int len = strlen(path);
22+
size_t len = strlen(path);
2323
if (strchr(path, '/'))
2424
die("path %s contains slash", path);
2525

26-
ALLOC_GROW(entries, used + 1, alloc);
27-
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
26+
FLEX_ALLOC_MEM(ent, name, path, len);
2827
ent->mode = mode;
2928
ent->len = len;
3029
hashcpy(ent->sha1, sha1);
31-
memcpy(ent->name, path, len+1);
30+
31+
ALLOC_GROW(entries, used + 1, alloc);
32+
entries[used++] = ent;
3233
}
3334

3435
static int ent_compare(const void *a_, const void *b_)

builtin/reflog.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
382382
{
383383
struct collected_reflog *e;
384384
struct collect_reflog_cb *cb = cb_data;
385-
size_t namelen = strlen(ref);
386385

387-
e = xmalloc(sizeof(*e) + namelen + 1);
386+
FLEX_ALLOC_STR(e, reflog, ref);
388387
hashcpy(e->sha1, oid->hash);
389-
memcpy(e->reflog, ref, namelen + 1);
390388
ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
391389
cb->e[cb->nr++] = e;
392390
return 0;
@@ -411,8 +409,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
411409
ent->pattern[len] == '\0')
412410
return ent;
413411

414-
ent = xcalloc(1, sizeof(*ent) + len + 1);
415-
memcpy(ent->pattern, pattern, len);
412+
FLEX_ALLOC_MEM(ent, pattern, pattern, len);
416413
*reflog_expire_cfg_tail = ent;
417414
reflog_expire_cfg_tail = &(ent->next);
418415
return ent;

cache-tree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
7979
ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
8080
it->subtree_nr++;
8181

82-
down = xmalloc(sizeof(*down) + pathlen + 1);
82+
FLEX_ALLOC_MEM(down, name, path, pathlen);
8383
down->cache_tree = NULL;
8484
down->namelen = pathlen;
85-
memcpy(down->name, path, pathlen);
86-
down->name[pathlen] = 0;
8785

8886
if (pos < it->subtree_nr)
8987
memmove(it->down + pos + 1,

combine-diff.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
319319
if (line[len-1] == '\n')
320320
len--;
321321

322-
lline = xmalloc(sizeof(*lline) + len + 1);
322+
FLEX_ALLOC_MEM(lline, line, line, len);
323323
lline->len = len;
324324
lline->next = NULL;
325325
lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
330330
sline->plost.lost_tail = lline;
331331
sline->plost.len++;
332332
lline->parent_map = this_mask;
333-
memcpy(lline->line, line, len);
334-
lline->line[len] = 0;
335333
}
336334

337335
struct combine_diff_state {

diff.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
26072607

26082608
struct diff_filespec *alloc_filespec(const char *path)
26092609
{
2610-
int namelen = strlen(path);
2611-
struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
2610+
struct diff_filespec *spec;
26122611

2613-
memset(spec, 0, sizeof(*spec));
2614-
spec->path = (char *)(spec + 1);
2615-
memcpy(spec->path, path, namelen+1);
2612+
FLEXPTR_ALLOC_STR(spec, path, path);
26162613
spec->count = 1;
26172614
spec->is_binary = -1;
26182615
return spec;

dir.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,
503503

504504
parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
505505
if (flags & EXC_FLAG_MUSTBEDIR) {
506-
char *s;
507-
x = xmalloc(sizeof(*x) + patternlen + 1);
508-
s = (char *)(x+1);
509-
memcpy(s, string, patternlen);
510-
s[patternlen] = '\0';
511-
x->pattern = s;
506+
FLEXPTR_ALLOC_MEM(x, pattern, string, patternlen);
512507
} else {
513508
x = xmalloc(sizeof(*x));
514509
x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
625620
}
626621

627622
uc->dir_created++;
628-
d = xmalloc(sizeof(*d) + len + 1);
629-
memset(d, 0, sizeof(*d));
630-
memcpy(d->name, name, len);
631-
d->name[len] = '\0';
623+
FLEX_ALLOC_MEM(d, name, name, len);
632624

633625
ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
634626
memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
11671159
{
11681160
struct dir_entry *ent;
11691161

1170-
ent = xmalloc(sizeof(*ent) + len + 1);
1162+
FLEX_ALLOC_MEM(ent, name, pathname, len);
11711163
ent->len = len;
1172-
memcpy(ent->name, pathname, len);
1173-
ent->name[len] = 0;
11741164
return ent;
11751165
}
11761166

hashmap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len)
256256
e = hashmap_get(&map, &key, data);
257257
if (!e) {
258258
/* not found: create it */
259-
e = xmallocz(sizeof(struct pool_entry) + len);
259+
FLEX_ALLOC_MEM(e, data, data, len);
260260
hashmap_entry_init(e, key.ent.hash);
261261
e->len = len;
262-
memcpy(e->data, data, len);
263262
hashmap_add(&map, e);
264263
}
265264
return e->data;

0 commit comments

Comments
 (0)