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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 22, 2016 at 17:44 UTC b32fa95fd8293ebfecb2b7b6c8d460579318f9fe
34 files changed +75 -64
alias.c
+1 -1
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)
23 int src, dst, count = 0, size = 16;
24 char quoted = 0;
25
26 - *argv = xmalloc(sizeof(**argv) * size);
26 + ALLOC_ARRAY(*argv, size);
27
28 /* split alias_string */
29 (*argv)[count++] = cmdline;
attr.c
+1 -1
@@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
799 ++count;
800 }
801 *num = count;
802 - *check = xmalloc(sizeof(**check) * count);
802 + ALLOC_ARRAY(*check, count);
803 j = 0;
804 for (i = 0; i < attr_nr; i++) {
805 const char *value = check_all_attr[i].value;
bisect.c
+2 -2
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)
708
709 static struct commit **get_bad_and_good_commits(int *rev_nr)
710 {
711 - int len = 1 + good_revs.nr;
712 - struct commit **rev = xmalloc(len * sizeof(*rev));
711 + struct commit **rev;
712 int i, n = 0;
713
714 + ALLOC_ARRAY(rev, 1 + good_revs.nr);
715 rev[n++] = get_commit_reference(current_bad_oid->hash);
716 for (i = 0; i < good_revs.nr; i++)
717 rev[n++] = get_commit_reference(good_revs.sha1[i]);
builtin/blame.c
+2 -1
@@ -2042,7 +2042,8 @@ static int prepare_lines(struct scoreboard *sb)
2042 for (p = buf; p < end; p = get_next_line(p, end))
2043 num++;
2044
2045 - sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
2045 + ALLOC_ARRAY(sb->lineno, num + 1);
2046 + lineno = sb->lineno;
2047
2048 for (p = buf; p < end; p = get_next_line(p, end))
2049 *lineno++ = p - buf;
builtin/clean.c
+1 -1
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
543 int eof = 0;
544 int i;
545
546 - chosen = xmalloc(sizeof(int) * stuff->nr);
546 + ALLOC_ARRAY(chosen, stuff->nr);
547 /* set chosen as uninitialized */
548 for (i = 0; i < stuff->nr; i++)
549 chosen[i] = -1;
builtin/fast-export.c
+1 -1
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
1021 const char **refspecs_str;
1022 int i;
1023
1024 - refspecs_str = xmalloc(sizeof(*refspecs_str) * refspecs_list.nr);
1024 + ALLOC_ARRAY(refspecs_str, refspecs_list.nr);
1025 for (i = 0; i < refspecs_list.nr; i++)
1026 refspecs_str[i] = refspecs_list.items[i].string;
1027
builtin/index-pack.c
+2 -2
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
1346 * before deltas depending on them, a good heuristic is to start
1347 * resolving deltas in the same order as their position in the pack.
1348 */
1349 - sorted_by_pos = xmalloc(nr_ref_deltas * sizeof(*sorted_by_pos));
1349 + ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
1350 for (i = 0; i < nr_ref_deltas; i++)
1351 sorted_by_pos[i] = &ref_deltas[i];
1352 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)
1759 if (show_stat)
1760 show_pack_info(stat_only);
1761
1762 - idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1762 + ALLOC_ARRAY(idx_objects, nr_objects);
1763 for (i = 0; i < nr_objects; i++)
1764 idx_objects[i] = &objects[i].idx;
1765 curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
builtin/merge-base.c
+1 -1
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
252 if (argc < 2)
253 usage_with_options(merge_base_usage, options);
254
255 - rev = xmalloc(argc * sizeof(*rev));
255 + ALLOC_ARRAY(rev, argc);
256 while (argc-- > 0)
257 rev[rev_nr++] = get_commit_reference(*argv++);
258 return show_merge_base(rev, rev_nr, show_all);
builtin/mv.c
+2 -1
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix,
24 int count, unsigned flags)
25 {
26 int i;
27 - const char **result = xmalloc((count + 1) * sizeof(const char *));
27 + const char **result;
28 + ALLOC_ARRAY(result, count + 1);
29 memcpy(result, pathspec, count * sizeof(const char *));
30 result[count] = NULL;
31 for (i = 0; i < count; i++) {
builtin/pack-objects.c
+4 -3
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void)
624 {
625 unsigned int i, wo_end, last_untagged;
626
627 - struct object_entry **wo = xmalloc(to_pack.nr_objects * sizeof(*wo));
627 + struct object_entry **wo;
628 struct object_entry *objects = to_pack.objects;
629
630 for (i = 0; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)
657 * Give the objects in the original recency order until
658 * we see a tagged tip.
659 */
660 + ALLOC_ARRAY(wo, to_pack.nr_objects);
661 for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
662 if (objects[i].tagged)
663 break;
@@ -769,7 +770,7 @@ static void write_pack_file(void)
770
771 if (progress > pack_to_stdout)
772 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);
774 write_order = compute_write_order();
775
776 do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)
2130 if (!to_pack.nr_objects || !window || !depth)
2131 return;
2132
2132 - delta_list = xmalloc(to_pack.nr_objects * sizeof(*delta_list));
2133 + ALLOC_ARRAY(delta_list, to_pack.nr_objects);
2134 nr_deltas = n = 0;
2135
2136 for (i = 0; i < to_pack.nr_objects; i++) {
builtin/pack-redundant.c
+1 -1
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void)
53 free_nodes = free_nodes->next;
54 } else {
55 int i = 1;
56 - new = xmalloc(sizeof(struct llist_item) * BLKSIZE);
56 + ALLOC_ARRAY(new, BLKSIZE);
57 for (; i < BLKSIZE; i++)
58 llist_item_put(&new[i]);
59 }
builtin/receive-pack.c
+2 -3
@@ -1591,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands,
1591 {
1592 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1593
1594 - si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1595 - si->shallow->nr);
1594 + ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
1595 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1596
1597 si->need_reachability_test =
@@ -1658,7 +1657,7 @@ static void update_shallow_info(struct command *commands,
1657 return;
1658 }
1659
1661 - ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1660 + ALLOC_ARRAY(ref_status, ref->nr);
1661 assign_shallow_commits_to_refs(si, NULL, ref_status);
1662 for (cmd = commands; cmd; cmd = cmd->next) {
1663 if (is_null_sha1(cmd->new_sha1))
column.c
+1 -1
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list,
164 data.colopts = colopts;
165 data.opts = *opts;
166
167 - data.len = xmalloc(sizeof(*data.len) * list->nr);
167 + ALLOC_ARRAY(data.len, list->nr);
168 for (i = 0; i < list->nr; i++)
169 data.len[i] = item_length(colopts, list->items[i].string);
170
combine-diff.c
+2 -2
@@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(
1372 struct combine_diff_path paths_head;
1373 struct strbuf base;
1374
1375 - parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
1375 + ALLOC_ARRAY(parents_sha1, nparent);
1376 for (i = 0; i < nparent; i++)
1377 parents_sha1[i] = parents->sha1[i];
1378
@@ -1483,7 +1483,7 @@ void diff_tree_combined(const unsigned char *sha1,
1483 if (opt->orderfile && num_paths) {
1484 struct obj_order *o;
1485
1486 - o = xmalloc(sizeof(*o) * num_paths);
1486 + ALLOC_ARRAY(o, num_paths);
1487 for (i = 0, p = paths; p; p = p->next, i++)
1488 o[i].obj = p;
1489 order_objects(opt->orderfile, path_path, o, num_paths);
commit.c
+1 -1
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt)
903
904 work = xcalloc(cnt, sizeof(*work));
905 redundant = xcalloc(cnt, 1);
906 - filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
906 + ALLOC_ARRAY(filled_index, cnt - 1);
907
908 for (i = 0; i < cnt; i++)
909 parse_commit(array[i]);
compat/mingw.c
+3 -3
@@ -852,7 +852,7 @@ static char **get_path_split(void)
852 if (!n)
853 return NULL;
854
855 - path = xmalloc((n+1)*sizeof(char *));
855 + ALLOC_ARRAY(path, n + 1);
856 p = envpath;
857 i = 0;
858 do {
@@ -937,7 +937,7 @@ static wchar_t *make_environment_block(char **deltaenv)
937 i++;
938
939 /* copy the environment, leaving space for changes */
940 - tmpenv = xmalloc((size + i) * sizeof(char*));
940 + ALLOC_ARRAY(tmpenv, size + i);
941 memcpy(tmpenv, environ, size * sizeof(char*));
942
943 /* merge supplied environment changes into the temporary environment */
@@ -1127,7 +1127,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
1127 int argc = 0;
1128 const char **argv2;
1129 while (argv[argc]) argc++;
1130 - argv2 = xmalloc(sizeof(*argv) * (argc+1));
1130 + ALLOC_ARRAY(argv2, argc + 1);
1131 argv2[0] = (char *)cmd; /* full path to the script file */
1132 memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
1133 pid = mingw_spawnv(prog, argv2, 1);
diffcore-order.c
+2 -2
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile)
52 }
53 if (pass == 0) {
54 order_cnt = cnt;
55 - order = xmalloc(sizeof(*order) * cnt);
55 + ALLOC_ARRAY(order, cnt);
56 }
57 }
58 }
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)
120 if (!q->nr)
121 return;
122
123 - o = xmalloc(sizeof(*o) * q->nr);
123 + ALLOC_ARRAY(o, q->nr);
124 for (i = 0; i < q->nr; i++)
125 o[i].obj = q->queue[i];
126 order_objects(orderfile, pair_pathtwo, o, q->nr);
dir.c
+3 -3
@@ -2448,14 +2448,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
2448 ud.untracked_alloc = value;
2449 ud.untracked_nr = value;
2450 if (ud.untracked_nr)
2451 - ud.untracked = xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);
2451 + ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
2452 data = next;
2453
2454 next = data;
2455 ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
2456 if (next > end)
2457 return -1;
2458 - ud.dirs = xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);
2458 + ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
2459 data = next;
2460
2461 len = strlen((const char *)data);
@@ -2575,7 +2575,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
2575 rd.data = next;
2576 rd.end = end;
2577 rd.index = 0;
2578 - rd.ucd = xmalloc(sizeof(*rd.ucd) * len);
2578 + ALLOC_ARRAY(rd.ucd, len);
2579
2580 if (read_one_dir(&uc->root, &rd) || rd.index != len)
2581 goto done;
fast-import.c
+3 -2
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)
814 if (!avail_tree_entry) {
815 unsigned int n = tree_entry_alloc;
816 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;
819 while (n-- > 1) {
820 *((void**)e) = e + 1;
821 e++;
@@ -898,7 +899,7 @@ static const char *create_index(void)
899 struct object_entry_pool *o;
900
901 /* Build the table of object IDs. */
901 - idx = xmalloc(object_count * sizeof(*idx));
902 + ALLOC_ARRAY(idx, object_count);
903 c = idx;
904 for (o = blocks; o; o = o->next_pool)
905 for (e = o->next_free; e-- != o->entries;)
fsck.c
+2 -1
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,
199
200 if (!options->msg_type) {
201 int i;
202 - int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
202 + int *msg_type;
203 + ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
204 for (i = 0; i < FSCK_MSG_MAX; i++)
205 msg_type[i] = fsck_msg_type(i, options);
206 options->msg_type = msg_type;
graph.c
+4 -6
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)
234 * We'll automatically grow columns later if we need more room.
235 */
236 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);
241
242 /*
243 * The diff output prefix callback, with this we can make
khash.h
+1 -1
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;
117 if (new_n_buckets < 4) new_n_buckets = 4; \
118 if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
119 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)); \
121 if (!new_flags) return -1; \
122 memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
123 if (h->n_buckets < new_n_buckets) { /* expand */ \
levenshtein.c
+5 -3
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2,
42 int w, int s, int a, int d)
43 {
44 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;
46 int i, j;
47
48 + ALLOC_ARRAY(row0, len2 + 1);
49 + ALLOC_ARRAY(row1, len2 + 1);
50 + ALLOC_ARRAY(row2, len2 + 1);
51 +
52 for (j = 0; j <= len2; j++)
53 row1[j] = j * a;
54 for (i = 0; i < len1; i++) {
line-log.c
+4 -4
@@ -522,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
522 if (diff_populate_filespec(spec, 0))
523 die("Cannot read blob %s", sha1_to_hex(spec->sha1));
524
525 - ends = xmalloc(size * sizeof(*ends));
525 + ALLOC_ARRAY(ends, size);
526 ends[cur++] = 0;
527 data = spec->data;
528 while (num < spec->size) {
@@ -1142,9 +1142,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm
1142 if (nparents > 1 && rev->first_parent_only)
1143 nparents = 1;
1144
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);
1148
1149 p = commit->parents;
1150 for (i = 0; i < nparents; i++) {
notes.c
+1 -1
@@ -1032,7 +1032,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs)
1032 struct string_list_item *item;
1033 int counter = 0;
1034 struct notes_tree **trees;
1035 - trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
1035 + ALLOC_ARRAY(trees, refs->nr + 1);
1036 for_each_string_list_item(item, refs) {
1037 struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
1038 init_notes(t, item->string, combine_notes_ignore, 0);
pack-check.c
+1 -1
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,
89 * we do not do scan-streaming check on the pack file.
90 */
91 nr_objects = p->num_objects;
92 - entries = xmalloc((nr_objects + 1) * sizeof(*entries));
92 + ALLOC_ARRAY(entries, nr_objects + 1);
93 entries[nr_objects].offset = pack_sig_ofs;
94 /* first sort entries by pack offset, since unpacking them is more efficient that way */
95 for (i = 0; i < nr_objects; i++) {
pack-revindex.c
+8 -4
@@ -83,10 +83,14 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
83 * keep track of them with alias pointers, always sorting from "from"
84 * to "to".
85 */
86 - struct revindex_entry *tmp = xmalloc(n * sizeof(*tmp));
87 - struct revindex_entry *from = entries, *to = tmp;
86 + struct revindex_entry *tmp, *from, *to;
87 int bits;
89 - unsigned *pos = xmalloc(BUCKETS * sizeof(*pos));
88 + unsigned *pos;
89 +
90 + ALLOC_ARRAY(pos, BUCKETS);
91 + ALLOC_ARRAY(tmp, n);
92 + from = entries;
93 + to = tmp;
94
95 /*
96 * If (max >> bits) is zero, then we know that the radix digit we are
@@ -161,7 +165,7 @@ static void create_pack_revindex(struct pack_revindex *rix)
165 unsigned i;
166 const char *index = p->index_data;
167
164 - rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
168 + ALLOC_ARRAY(rix->revindex, num_ent + 1);
169 index += 4 * 256;
170
171 if (p->index_version > 1) {
pathspec.c
+3 -2
@@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec,
406 n++;
407
408 pathspec->nr = n;
409 - pathspec->items = item = xmalloc(sizeof(*item) * n);
409 + ALLOC_ARRAY(pathspec->items, n);
410 + item = pathspec->items;
411 pathspec->_raw = argv;
412 prefixlen = prefix ? strlen(prefix) : 0;
413
@@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
484 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
485 {
486 *dst = *src;
486 - dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
487 + ALLOC_ARRAY(dst->items, dst->nr);
488 memcpy(dst->items, src->items,
489 sizeof(struct pathspec_item) * dst->nr);
490 }
remote-curl.c
+2 -1
@@ -696,9 +696,10 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
696 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
697 {
698 struct walker *walker;
699 - char **targets = xmalloc(nr_heads * sizeof(char*));
699 + char **targets;
700 int ret, i;
701
702 + ALLOC_ARRAY(targets, nr_heads);
703 if (options.depth)
704 die("dumb http transport does not support --depth");
705 for (i = 0; i < nr_heads; i++)
sha1_file.c
+2 -2
@@ -1942,7 +1942,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,
1942 /* Push the object we're going to leave behind */
1943 if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
1944 poi_stack_alloc = alloc_nr(poi_stack_nr);
1945 - poi_stack = xmalloc(sizeof(off_t)*poi_stack_alloc);
1945 + ALLOC_ARRAY(poi_stack, poi_stack_alloc);
1946 memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);
1947 } else {
1948 ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
@@ -2308,7 +2308,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
2308 if (delta_stack_nr >= delta_stack_alloc
2309 && delta_stack == small_delta_stack) {
2310 delta_stack_alloc = alloc_nr(delta_stack_nr);
2311 - delta_stack = xmalloc(sizeof(*delta_stack)*delta_stack_alloc);
2311 + ALLOC_ARRAY(delta_stack, delta_stack_alloc);
2312 memcpy(delta_stack, small_delta_stack,
2313 sizeof(*delta_stack)*delta_stack_nr);
2314 } else {
shallow.c
+3 -3
@@ -315,8 +315,8 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
315 info->shallow = sa;
316 if (!sa)
317 return;
318 - info->ours = xmalloc(sizeof(*info->ours) * sa->nr);
319 - info->theirs = xmalloc(sizeof(*info->theirs) * sa->nr);
318 + ALLOC_ARRAY(info->ours, sa->nr);
319 + ALLOC_ARRAY(info->theirs, sa->nr);
320 for (i = 0; i < sa->nr; i++) {
321 if (has_sha1_file(sa->sha1[i])) {
322 struct commit_graft *graft;
@@ -487,7 +487,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
487 struct paint_info pi;
488
489 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
490 - shallow = xmalloc(sizeof(*shallow) * (info->nr_ours + info->nr_theirs));
490 + ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
491 for (i = 0; i < info->nr_ours; i++)
492 shallow[nr_shallow++] = info->ours[i];
493 for (i = 0; i < info->nr_theirs; i++)
show-index.c
+2 -1
@@ -50,7 +50,8 @@ int main(int argc, char **argv)
50 unsigned char sha1[20];
51 uint32_t crc;
52 uint32_t off;
53 - } *entries = xmalloc(nr * sizeof(entries[0]));
53 + } *entries;
54 + ALLOC_ARRAY(entries, nr);
55 for (i = 0; i < nr; i++)
56 if (fread(entries[i].sha1, 20, 1, stdin) != 1)
57 die("unable to read sha1 %u/%u", i, nr);
transport.c
+1 -1
@@ -1306,7 +1306,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
1306 * This condition shouldn't be met in a non-deepening fetch
1307 * (see builtin/fetch.c:quickfetch()).
1308 */
1309 - heads = xmalloc(nr_refs * sizeof(*heads));
1309 + ALLOC_ARRAY(heads, nr_refs);
1310 for (rm = refs; rm; rm = rm->next)
1311 heads[nr_heads++] = rm;
1312 }
xdiff-interface.c
+1 -1
@@ -265,7 +265,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
265 for (i = 0, regs->nr = 1; value[i]; i++)
266 if (value[i] == '\n')
267 regs->nr++;
268 - regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
268 + ALLOC_ARRAY(regs->array, regs->nr);
269 for (i = 0; i < regs->nr; i++) {
270 struct ff_reg *reg = regs->array + i;
271 const char *ep = strchr(value, '\n'), *expression;