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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 850d2fe commit b32fa95

34 files changed

+75
-64
lines changed

alias.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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++) {

0 commit comments

Comments
 (0)