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>
Jeff King committed
Feb 22, 2016 at 17:44 UTC
96ffc06f72f693d80f05059a1f0e5ca9007d5f1b
17 files changed
+35
-82
attr.c
+1
-3
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
93
if (invalid_attr_name(name, len))
94
return NULL;
95
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);
97
a->h = hval;
98
a->next = git_attr_hash[pos];
99
a->attr_nr = attr_nr++;
builtin/blame.c
+1
-3
@@ -459,13 +459,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
459
static struct origin *make_origin(struct commit *commit, const char *path)
460
{
461
struct origin *o;
462
- size_t pathlen = strlen(path) + 1;
463
- o = xcalloc(1, sizeof(*o) + pathlen);
462
+ FLEX_ALLOC_STR(o, path, path);
463
o->commit = commit;
464
o->refcnt = 1;
465
o->next = commit->util;
466
commit->util = o;
468
- memcpy(o->path, path, pathlen); /* includes NUL */
467
return o;
468
}
469
builtin/help.c
+3
-6
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)
171
static void add_man_viewer(const char *name)
172
{
173
struct man_viewer_list **p = &man_viewer_list;
174
- size_t len = strlen(name);
174
175
while (*p)
176
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);
178
}
179
180
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,
188
size_t len,
189
const char *value)
190
{
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);
193
new->info = xstrdup(value);
194
new->next = man_viewer_info_list;
195
man_viewer_info_list = new;
builtin/mktree.c
+5
-4
@@ -19,16 +19,17 @@ static int alloc, used;
19
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
20
{
21
struct treeent *ent;
22
- int len = strlen(path);
22
+ size_t len = strlen(path);
23
if (strchr(path, '/'))
24
die("path %s contains slash", path);
25
26
- ALLOC_GROW(entries, used + 1, alloc);
27
- ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
26
+ FLEX_ALLOC_MEM(ent, name, path, len);
27
ent->mode = mode;
28
ent->len = len;
29
hashcpy(ent->sha1, sha1);
31
- memcpy(ent->name, path, len+1);
30
+
31
+ ALLOC_GROW(entries, used + 1, alloc);
32
+ entries[used++] = ent;
33
}
34
35
static int ent_compare(const void *a_, const void *b_)
builtin/reflog.c
+2
-5
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
382
{
383
struct collected_reflog *e;
384
struct collect_reflog_cb *cb = cb_data;
385
- size_t namelen = strlen(ref);
385
387
- e = xmalloc(sizeof(*e) + namelen + 1);
386
+ FLEX_ALLOC_STR(e, reflog, ref);
387
hashcpy(e->sha1, oid->hash);
389
- memcpy(e->reflog, ref, namelen + 1);
388
ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
389
cb->e[cb->nr++] = e;
390
return 0;
@@ -411,8 +409,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
409
ent->pattern[len] == '\0')
410
return ent;
411
414
- ent = xcalloc(1, sizeof(*ent) + len + 1);
415
- memcpy(ent->pattern, pattern, len);
412
+ FLEX_ALLOC_MEM(ent, pattern, pattern, len);
413
*reflog_expire_cfg_tail = ent;
414
reflog_expire_cfg_tail = &(ent->next);
415
return ent;
cache-tree.c
+1
-3
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
79
ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
80
it->subtree_nr++;
81
82
- down = xmalloc(sizeof(*down) + pathlen + 1);
82
+ FLEX_ALLOC_MEM(down, name, path, pathlen);
83
down->cache_tree = NULL;
84
down->namelen = pathlen;
85
- memcpy(down->name, path, pathlen);
86
- down->name[pathlen] = 0;
85
86
if (pos < it->subtree_nr)
87
memmove(it->down + pos + 1,
combine-diff.c
+1
-3
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
319
if (line[len-1] == '\n')
320
len--;
321
322
- lline = xmalloc(sizeof(*lline) + len + 1);
322
+ FLEX_ALLOC_MEM(lline, line, line, len);
323
lline->len = len;
324
lline->next = NULL;
325
lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
330
sline->plost.lost_tail = lline;
331
sline->plost.len++;
332
lline->parent_map = this_mask;
333
- memcpy(lline->line, line, len);
334
- lline->line[len] = 0;
333
}
334
335
struct combine_diff_state {
diff.c
+2
-5
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
2607
2608
struct diff_filespec *alloc_filespec(const char *path)
2609
{
2610
- int namelen = strlen(path);
2611
- struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
2610
+ struct diff_filespec *spec;
2611
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);
2613
spec->count = 1;
2614
spec->is_binary = -1;
2615
return spec;
dir.c
+3
-13
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,
503
504
parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
505
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);
507
} else {
508
x = xmalloc(sizeof(*x));
509
x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
620
}
621
622
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);
624
625
ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
626
memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
1159
{
1160
struct dir_entry *ent;
1161
1170
- ent = xmalloc(sizeof(*ent) + len + 1);
1162
+ FLEX_ALLOC_MEM(ent, name, pathname, len);
1163
ent->len = len;
1172
- memcpy(ent->name, pathname, len);
1173
- ent->name[len] = 0;
1164
return ent;
1165
}
1166
hashmap.c
+1
-2
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len)
256
e = hashmap_get(&map, &key, data);
257
if (!e) {
258
/* not found: create it */
259
- e = xmallocz(sizeof(struct pool_entry) + len);
259
+ FLEX_ALLOC_MEM(e, data, data, len);
260
hashmap_entry_init(e, key.ent.hash);
261
e->len = len;
262
- memcpy(e->data, data, len);
262
hashmap_add(&map, e);
263
}
264
return e->data;
help.c
+2
-4
@@ -11,11 +11,9 @@
11
12
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
13
{
14
- struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
15
-
14
+ struct cmdname *ent;
15
+ FLEX_ALLOC_MEM(ent, name, name, len);
16
ent->len = len;
17
- memcpy(ent->name, name, len);
18
- ent->name[len] = 0;
17
18
ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
19
cmds->names[cmds->cnt++] = ent;
log-tree.c
+2
-3
@@ -77,9 +77,8 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const ch
77
78
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
79
{
80
- int nlen = strlen(name);
81
- struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
82
- memcpy(res->name, name, nlen + 1);
80
+ struct name_decoration *res;
81
+ FLEX_ALLOC_STR(res, name, name);
82
res->type = type;
83
res->next = add_decoration(&name_decoration, obj, res);
84
}
name-hash.c
+1
-2
@@ -55,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
55
dir = find_dir_entry(istate, ce->name, namelen);
56
if (!dir) {
57
/* not found, create it and add to hash table */
58
- dir = xcalloc(1, sizeof(struct dir_entry) + namelen + 1);
58
+ FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
59
hashmap_entry_init(dir, memihash(ce->name, namelen));
60
dir->namelen = namelen;
61
- strncpy(dir->name, ce->name, namelen);
61
hashmap_add(&istate->dir_hash, dir);
62
63
/* recursively add missing parent directories */
ref-filter.c
+2
-4
@@ -1255,10 +1255,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
1255
const unsigned char *objectname,
1256
int flag)
1257
{
1258
- size_t len = strlen(refname);
1259
- struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
1260
- memcpy(ref->refname, refname, len);
1261
- ref->refname[len] = '\0';
1258
+ struct ref_array_item *ref;
1259
+ FLEX_ALLOC_STR(ref, refname, refname);
1260
hashcpy(ref->objectname, objectname);
1261
ref->flag = flag;
1262
refs.c
+2
-4
@@ -761,10 +761,8 @@ void ref_transaction_free(struct ref_transaction *transaction)
761
static struct ref_update *add_update(struct ref_transaction *transaction,
762
const char *refname)
763
{
764
- size_t len = strlen(refname) + 1;
765
- struct ref_update *update = xcalloc(1, sizeof(*update) + len);
766
-
767
- memcpy((char *)update->refname, refname, len); /* includes NUL */
764
+ struct ref_update *update;
765
+ FLEX_ALLOC_STR(update, refname, refname);
766
ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
767
transaction->updates[transaction->nr++] = update;
768
return update;
refs/files-backend.c
+5
-14
@@ -199,17 +199,14 @@ static struct ref_entry *create_ref_entry(const char *refname,
199
const unsigned char *sha1, int flag,
200
int check_name)
201
{
202
- int len;
202
struct ref_entry *ref;
203
204
if (check_name &&
205
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
206
die("Reference has invalid format: '%s'", refname);
208
- len = strlen(refname) + 1;
209
- ref = xmalloc(sizeof(struct ref_entry) + len);
207
+ FLEX_ALLOC_STR(ref, name, refname);
208
hashcpy(ref->u.value.oid.hash, sha1);
209
oidclr(&ref->u.value.peeled);
212
- memcpy(ref->name, refname, len);
210
ref->flag = flag;
211
return ref;
212
}
@@ -268,9 +265,7 @@ static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
265
int incomplete)
266
{
267
struct ref_entry *direntry;
271
- direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1);
272
- memcpy(direntry->name, dirname, len);
273
- direntry->name[len] = '\0';
268
+ FLEX_ALLOC_MEM(direntry, name, dirname, len);
269
direntry->u.subdir.ref_cache = ref_cache;
270
direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
271
return direntry;
@@ -939,13 +934,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)
934
*/
935
static struct ref_cache *create_ref_cache(const char *submodule)
936
{
942
- int len;
937
struct ref_cache *refs;
938
if (!submodule)
939
submodule = "";
946
- len = strlen(submodule) + 1;
947
- refs = xcalloc(1, sizeof(struct ref_cache) + len);
948
- memcpy(refs->name, submodule, len);
940
+ FLEX_ALLOC_STR(refs, name, submodule);
941
refs->next = submodule_ref_caches;
942
submodule_ref_caches = refs;
943
return refs;
@@ -2191,10 +2183,9 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
2183
2184
/* Schedule the loose reference for pruning if requested. */
2185
if ((cb->flags & PACK_REFS_PRUNE)) {
2194
- int namelen = strlen(entry->name) + 1;
2195
- struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
2186
+ struct ref_to_prune *n;
2187
+ FLEX_ALLOC_STR(n, name, entry->name);
2188
hashcpy(n->sha1, entry->u.value.oid.hash);
2197
- memcpy(n->name, entry->name, namelen); /* includes NUL */
2189
n->next = cb->ref_to_prune;
2190
cb->ref_to_prune = n;
2191
}
remote.c
+1
-4
@@ -2132,16 +2132,13 @@ static int one_local_ref(const char *refname, const struct object_id *oid,
2132
{
2133
struct ref ***local_tail = cb_data;
2134
struct ref *ref;
2135
- int len;
2135
2136
/* we already know it starts with refs/ to get here */
2137
if (check_refname_format(refname + 5, 0))
2138
return 0;
2139
2141
- len = strlen(refname) + 1;
2142
- ref = xcalloc(1, sizeof(*ref) + len);
2140
+ ref = alloc_ref(refname);
2141
oidcpy(&ref->new_oid, oid);
2144
- memcpy(ref->name, refname, len);
2142
**local_tail = ref;
2143
*local_tail = &ref->next;
2144
return 0;