Skip to content

Commit b32fa95

Browse files
peffgitster
authored andcommitted
convert trivial cases to ALLOC_ARRAY
Each of these cases can be converted to use ALLOC_ARRAY or REALLOC_ARRAY, which has two advantages: 1. It automatically checks the array-size multiplication for overflow. 2. It always uses sizeof(*array) for the element-size, so that it can never go out of sync with the declared type of the array. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 850d2fe commit b32fa95

34 files changed

+75
-64
lines changed

alias.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)
2323
int src, dst, count = 0, size = 16;
2424
char quoted = 0;
2525

26-
*argv = xmalloc(sizeof(**argv) * size);
26+
ALLOC_ARRAY(*argv, size);
2727

2828
/* split alias_string */
2929
(*argv)[count++] = cmdline;

attr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
799799
++count;
800800
}
801801
*num = count;
802-
*check = xmalloc(sizeof(**check) * count);
802+
ALLOC_ARRAY(*check, count);
803803
j = 0;
804804
for (i = 0; i < attr_nr; i++) {
805805
const char *value = check_all_attr[i].value;

bisect.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)
708708

709709
static struct commit **get_bad_and_good_commits(int *rev_nr)
710710
{
711-
int len = 1 + good_revs.nr;
712-
struct commit **rev = xmalloc(len * sizeof(*rev));
711+
struct commit **rev;
713712
int i, n = 0;
714713

714+
ALLOC_ARRAY(rev, 1 + good_revs.nr);
715715
rev[n++] = get_commit_reference(current_bad_oid->hash);
716716
for (i = 0; i < good_revs.nr; i++)
717717
rev[n++] = get_commit_reference(good_revs.sha1[i]);

builtin/blame.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,8 @@ static int prepare_lines(struct scoreboard *sb)
20422042
for (p = buf; p < end; p = get_next_line(p, end))
20432043
num++;
20442044

2045-
sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
2045+
ALLOC_ARRAY(sb->lineno, num + 1);
2046+
lineno = sb->lineno;
20462047

20472048
for (p = buf; p < end; p = get_next_line(p, end))
20482049
*lineno++ = p - buf;

builtin/clean.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
543543
int eof = 0;
544544
int i;
545545

546-
chosen = xmalloc(sizeof(int) * stuff->nr);
546+
ALLOC_ARRAY(chosen, stuff->nr);
547547
/* set chosen as uninitialized */
548548
for (i = 0; i < stuff->nr; i++)
549549
chosen[i] = -1;

builtin/fast-export.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
10211021
const char **refspecs_str;
10221022
int i;
10231023

1024-
refspecs_str = xmalloc(sizeof(*refspecs_str) * refspecs_list.nr);
1024+
ALLOC_ARRAY(refspecs_str, refspecs_list.nr);
10251025
for (i = 0; i < refspecs_list.nr; i++)
10261026
refspecs_str[i] = refspecs_list.items[i].string;
10271027

builtin/index-pack.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
13461346
* before deltas depending on them, a good heuristic is to start
13471347
* resolving deltas in the same order as their position in the pack.
13481348
*/
1349-
sorted_by_pos = xmalloc(nr_ref_deltas * sizeof(*sorted_by_pos));
1349+
ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
13501350
for (i = 0; i < nr_ref_deltas; i++)
13511351
sorted_by_pos[i] = &ref_deltas[i];
13521352
qsort(sorted_by_pos, nr_ref_deltas, sizeof(*sorted_by_pos), delta_pos_compare);
@@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
17591759
if (show_stat)
17601760
show_pack_info(stat_only);
17611761

1762-
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1762+
ALLOC_ARRAY(idx_objects, nr_objects);
17631763
for (i = 0; i < nr_objects; i++)
17641764
idx_objects[i] = &objects[i].idx;
17651765
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);

builtin/merge-base.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
252252
if (argc < 2)
253253
usage_with_options(merge_base_usage, options);
254254

255-
rev = xmalloc(argc * sizeof(*rev));
255+
ALLOC_ARRAY(rev, argc);
256256
while (argc-- > 0)
257257
rev[rev_nr++] = get_commit_reference(*argv++);
258258
return show_merge_base(rev, rev_nr, show_all);

builtin/mv.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix,
2424
int count, unsigned flags)
2525
{
2626
int i;
27-
const char **result = xmalloc((count + 1) * sizeof(const char *));
27+
const char **result;
28+
ALLOC_ARRAY(result, count + 1);
2829
memcpy(result, pathspec, count * sizeof(const char *));
2930
result[count] = NULL;
3031
for (i = 0; i < count; i++) {

builtin/pack-objects.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void)
624624
{
625625
unsigned int i, wo_end, last_untagged;
626626

627-
struct object_entry **wo = xmalloc(to_pack.nr_objects * sizeof(*wo));
627+
struct object_entry **wo;
628628
struct object_entry *objects = to_pack.objects;
629629

630630
for (i = 0; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)
657657
* Give the objects in the original recency order until
658658
* we see a tagged tip.
659659
*/
660+
ALLOC_ARRAY(wo, to_pack.nr_objects);
660661
for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
661662
if (objects[i].tagged)
662663
break;
@@ -769,7 +770,7 @@ static void write_pack_file(void)
769770

770771
if (progress > pack_to_stdout)
771772
progress_state = start_progress(_("Writing objects"), nr_result);
772-
written_list = xmalloc(to_pack.nr_objects * sizeof(*written_list));
773+
ALLOC_ARRAY(written_list, to_pack.nr_objects);
773774
write_order = compute_write_order();
774775

775776
do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)
21292130
if (!to_pack.nr_objects || !window || !depth)
21302131
return;
21312132

2132-
delta_list = xmalloc(to_pack.nr_objects * sizeof(*delta_list));
2133+
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
21332134
nr_deltas = n = 0;
21342135

21352136
for (i = 0; i < to_pack.nr_objects; i++) {

builtin/pack-redundant.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void)
5353
free_nodes = free_nodes->next;
5454
} else {
5555
int i = 1;
56-
new = xmalloc(sizeof(struct llist_item) * BLKSIZE);
56+
ALLOC_ARRAY(new, BLKSIZE);
5757
for (; i < BLKSIZE; i++)
5858
llist_item_put(&new[i]);
5959
}

builtin/receive-pack.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands,
15911591
{
15921592
int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
15931593

1594-
si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1595-
si->shallow->nr);
1594+
ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
15961595
assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
15971596

15981597
si->need_reachability_test =
@@ -1658,7 +1657,7 @@ static void update_shallow_info(struct command *commands,
16581657
return;
16591658
}
16601659

1661-
ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1660+
ALLOC_ARRAY(ref_status, ref->nr);
16621661
assign_shallow_commits_to_refs(si, NULL, ref_status);
16631662
for (cmd = commands; cmd; cmd = cmd->next) {
16641663
if (is_null_sha1(cmd->new_sha1))

column.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list,
164164
data.colopts = colopts;
165165
data.opts = *opts;
166166

167-
data.len = xmalloc(sizeof(*data.len) * list->nr);
167+
ALLOC_ARRAY(data.len, list->nr);
168168
for (i = 0; i < list->nr; i++)
169169
data.len[i] = item_length(colopts, list->items[i].string);
170170

combine-diff.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(
13721372
struct combine_diff_path paths_head;
13731373
struct strbuf base;
13741374

1375-
parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
1375+
ALLOC_ARRAY(parents_sha1, nparent);
13761376
for (i = 0; i < nparent; i++)
13771377
parents_sha1[i] = parents->sha1[i];
13781378

@@ -1483,7 +1483,7 @@ void diff_tree_combined(const unsigned char *sha1,
14831483
if (opt->orderfile && num_paths) {
14841484
struct obj_order *o;
14851485

1486-
o = xmalloc(sizeof(*o) * num_paths);
1486+
ALLOC_ARRAY(o, num_paths);
14871487
for (i = 0, p = paths; p; p = p->next, i++)
14881488
o[i].obj = p;
14891489
order_objects(opt->orderfile, path_path, o, num_paths);

commit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt)
903903

904904
work = xcalloc(cnt, sizeof(*work));
905905
redundant = xcalloc(cnt, 1);
906-
filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
906+
ALLOC_ARRAY(filled_index, cnt - 1);
907907

908908
for (i = 0; i < cnt; i++)
909909
parse_commit(array[i]);

compat/mingw.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ static char **get_path_split(void)
852852
if (!n)
853853
return NULL;
854854

855-
path = xmalloc((n+1)*sizeof(char *));
855+
ALLOC_ARRAY(path, n + 1);
856856
p = envpath;
857857
i = 0;
858858
do {
@@ -937,7 +937,7 @@ static wchar_t *make_environment_block(char **deltaenv)
937937
i++;
938938

939939
/* copy the environment, leaving space for changes */
940-
tmpenv = xmalloc((size + i) * sizeof(char*));
940+
ALLOC_ARRAY(tmpenv, size + i);
941941
memcpy(tmpenv, environ, size * sizeof(char*));
942942

943943
/* merge supplied environment changes into the temporary environment */
@@ -1127,7 +1127,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
11271127
int argc = 0;
11281128
const char **argv2;
11291129
while (argv[argc]) argc++;
1130-
argv2 = xmalloc(sizeof(*argv) * (argc+1));
1130+
ALLOC_ARRAY(argv2, argc + 1);
11311131
argv2[0] = (char *)cmd; /* full path to the script file */
11321132
memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
11331133
pid = mingw_spawnv(prog, argv2, 1);

diffcore-order.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile)
5252
}
5353
if (pass == 0) {
5454
order_cnt = cnt;
55-
order = xmalloc(sizeof(*order) * cnt);
55+
ALLOC_ARRAY(order, cnt);
5656
}
5757
}
5858
}
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)
120120
if (!q->nr)
121121
return;
122122

123-
o = xmalloc(sizeof(*o) * q->nr);
123+
ALLOC_ARRAY(o, q->nr);
124124
for (i = 0; i < q->nr; i++)
125125
o[i].obj = q->queue[i];
126126
order_objects(orderfile, pair_pathtwo, o, q->nr);

dir.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2448,14 +2448,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
24482448
ud.untracked_alloc = value;
24492449
ud.untracked_nr = value;
24502450
if (ud.untracked_nr)
2451-
ud.untracked = xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);
2451+
ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
24522452
data = next;
24532453

24542454
next = data;
24552455
ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
24562456
if (next > end)
24572457
return -1;
2458-
ud.dirs = xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);
2458+
ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
24592459
data = next;
24602460

24612461
len = strlen((const char *)data);
@@ -2575,7 +2575,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
25752575
rd.data = next;
25762576
rd.end = end;
25772577
rd.index = 0;
2578-
rd.ucd = xmalloc(sizeof(*rd.ucd) * len);
2578+
ALLOC_ARRAY(rd.ucd, len);
25792579

25802580
if (read_one_dir(&uc->root, &rd) || rd.index != len)
25812581
goto done;

fast-import.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)
814814
if (!avail_tree_entry) {
815815
unsigned int n = tree_entry_alloc;
816816
total_allocd += n * sizeof(struct tree_entry);
817-
avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
817+
ALLOC_ARRAY(e, n);
818+
avail_tree_entry = e;
818819
while (n-- > 1) {
819820
*((void**)e) = e + 1;
820821
e++;
@@ -898,7 +899,7 @@ static const char *create_index(void)
898899
struct object_entry_pool *o;
899900

900901
/* Build the table of object IDs. */
901-
idx = xmalloc(object_count * sizeof(*idx));
902+
ALLOC_ARRAY(idx, object_count);
902903
c = idx;
903904
for (o = blocks; o; o = o->next_pool)
904905
for (e = o->next_free; e-- != o->entries;)

fsck.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,
199199

200200
if (!options->msg_type) {
201201
int i;
202-
int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
202+
int *msg_type;
203+
ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
203204
for (i = 0; i < FSCK_MSG_MAX; i++)
204205
msg_type[i] = fsck_msg_type(i, options);
205206
options->msg_type = msg_type;

graph.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)
234234
* We'll automatically grow columns later if we need more room.
235235
*/
236236
graph->column_capacity = 30;
237-
graph->columns = xmalloc(sizeof(struct column) *
238-
graph->column_capacity);
239-
graph->new_columns = xmalloc(sizeof(struct column) *
240-
graph->column_capacity);
241-
graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
242-
graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
237+
ALLOC_ARRAY(graph->columns, graph->column_capacity);
238+
ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
239+
ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
240+
ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
243241

244242
/*
245243
* The diff output prefix callback, with this we can make

khash.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;
117117
if (new_n_buckets < 4) new_n_buckets = 4; \
118118
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
119119
else { /* hash table size to be changed (shrink or expand); rehash */ \
120-
new_flags = (khint32_t*)xmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
120+
ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
121121
if (!new_flags) return -1; \
122122
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
123123
if (h->n_buckets < new_n_buckets) { /* expand */ \

levenshtein.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2,
4242
int w, int s, int a, int d)
4343
{
4444
int len1 = strlen(string1), len2 = strlen(string2);
45-
int *row0 = xmalloc(sizeof(int) * (len2 + 1));
46-
int *row1 = xmalloc(sizeof(int) * (len2 + 1));
47-
int *row2 = xmalloc(sizeof(int) * (len2 + 1));
45+
int *row0, *row1, *row2;
4846
int i, j;
4947

48+
ALLOC_ARRAY(row0, len2 + 1);
49+
ALLOC_ARRAY(row1, len2 + 1);
50+
ALLOC_ARRAY(row2, len2 + 1);
51+
5052
for (j = 0; j <= len2; j++)
5153
row1[j] = j * a;
5254
for (i = 0; i < len1; i++) {

line-log.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
522522
if (diff_populate_filespec(spec, 0))
523523
die("Cannot read blob %s", sha1_to_hex(spec->sha1));
524524

525-
ends = xmalloc(size * sizeof(*ends));
525+
ALLOC_ARRAY(ends, size);
526526
ends[cur++] = 0;
527527
data = spec->data;
528528
while (num < spec->size) {
@@ -1142,9 +1142,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm
11421142
if (nparents > 1 && rev->first_parent_only)
11431143
nparents = 1;
11441144

1145-
diffqueues = xmalloc(nparents * sizeof(*diffqueues));
1146-
cand = xmalloc(nparents * sizeof(*cand));
1147-
parents = xmalloc(nparents * sizeof(*parents));
1145+
ALLOC_ARRAY(diffqueues, nparents);
1146+
ALLOC_ARRAY(cand, nparents);
1147+
ALLOC_ARRAY(parents, nparents);
11481148

11491149
p = commit->parents;
11501150
for (i = 0; i < nparents; i++) {

notes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs)
10321032
struct string_list_item *item;
10331033
int counter = 0;
10341034
struct notes_tree **trees;
1035-
trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
1035+
ALLOC_ARRAY(trees, refs->nr + 1);
10361036
for_each_string_list_item(item, refs) {
10371037
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
10381038
init_notes(t, item->string, combine_notes_ignore, 0);

pack-check.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,
8989
* we do not do scan-streaming check on the pack file.
9090
*/
9191
nr_objects = p->num_objects;
92-
entries = xmalloc((nr_objects + 1) * sizeof(*entries));
92+
ALLOC_ARRAY(entries, nr_objects + 1);
9393
entries[nr_objects].offset = pack_sig_ofs;
9494
/* first sort entries by pack offset, since unpacking them is more efficient that way */
9595
for (i = 0; i < nr_objects; i++) {

0 commit comments

Comments
 (0)