Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Feb 22, 2016
1 parent 850d2fe commit b32fa95
Show file tree
Hide file tree
Showing 34 changed files with 75 additions and 64 deletions.
2 changes: 1 addition & 1 deletion alias.c
Expand Up @@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)
int src, dst, count = 0, size = 16;
char quoted = 0;

*argv = xmalloc(sizeof(**argv) * size);
ALLOC_ARRAY(*argv, size);

/* split alias_string */
(*argv)[count++] = cmdline;
Expand Down
2 changes: 1 addition & 1 deletion attr.c
Expand Up @@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
++count;
}
*num = count;
*check = xmalloc(sizeof(**check) * count);
ALLOC_ARRAY(*check, count);
j = 0;
for (i = 0; i < attr_nr; i++) {
const char *value = check_all_attr[i].value;
Expand Down
4 changes: 2 additions & 2 deletions bisect.c
Expand Up @@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)

static struct commit **get_bad_and_good_commits(int *rev_nr)
{
int len = 1 + good_revs.nr;
struct commit **rev = xmalloc(len * sizeof(*rev));
struct commit **rev;
int i, n = 0;

ALLOC_ARRAY(rev, 1 + good_revs.nr);
rev[n++] = get_commit_reference(current_bad_oid->hash);
for (i = 0; i < good_revs.nr; i++)
rev[n++] = get_commit_reference(good_revs.sha1[i]);
Expand Down
3 changes: 2 additions & 1 deletion builtin/blame.c
Expand Up @@ -2042,7 +2042,8 @@ static int prepare_lines(struct scoreboard *sb)
for (p = buf; p < end; p = get_next_line(p, end))
num++;

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

for (p = buf; p < end; p = get_next_line(p, end))
*lineno++ = p - buf;
Expand Down
2 changes: 1 addition & 1 deletion builtin/clean.c
Expand Up @@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
int eof = 0;
int i;

chosen = xmalloc(sizeof(int) * stuff->nr);
ALLOC_ARRAY(chosen, stuff->nr);
/* set chosen as uninitialized */
for (i = 0; i < stuff->nr; i++)
chosen[i] = -1;
Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Expand Up @@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
const char **refspecs_str;
int i;

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

Expand Down
4 changes: 2 additions & 2 deletions builtin/index-pack.c
Expand Up @@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
* before deltas depending on them, a good heuristic is to start
* resolving deltas in the same order as their position in the pack.
*/
sorted_by_pos = xmalloc(nr_ref_deltas * sizeof(*sorted_by_pos));
ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
for (i = 0; i < nr_ref_deltas; i++)
sorted_by_pos[i] = &ref_deltas[i];
qsort(sorted_by_pos, nr_ref_deltas, sizeof(*sorted_by_pos), delta_pos_compare);
Expand Down Expand Up @@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
if (show_stat)
show_pack_info(stat_only);

idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
ALLOC_ARRAY(idx_objects, nr_objects);
for (i = 0; i < nr_objects; i++)
idx_objects[i] = &objects[i].idx;
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
Expand Down
2 changes: 1 addition & 1 deletion builtin/merge-base.c
Expand Up @@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
if (argc < 2)
usage_with_options(merge_base_usage, options);

rev = xmalloc(argc * sizeof(*rev));
ALLOC_ARRAY(rev, argc);
while (argc-- > 0)
rev[rev_nr++] = get_commit_reference(*argv++);
return show_merge_base(rev, rev_nr, show_all);
Expand Down
3 changes: 2 additions & 1 deletion builtin/mv.c
Expand Up @@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix,
int count, unsigned flags)
{
int i;
const char **result = xmalloc((count + 1) * sizeof(const char *));
const char **result;
ALLOC_ARRAY(result, count + 1);
memcpy(result, pathspec, count * sizeof(const char *));
result[count] = NULL;
for (i = 0; i < count; i++) {
Expand Down
7 changes: 4 additions & 3 deletions builtin/pack-objects.c
Expand Up @@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void)
{
unsigned int i, wo_end, last_untagged;

struct object_entry **wo = xmalloc(to_pack.nr_objects * sizeof(*wo));
struct object_entry **wo;
struct object_entry *objects = to_pack.objects;

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

if (progress > pack_to_stdout)
progress_state = start_progress(_("Writing objects"), nr_result);
written_list = xmalloc(to_pack.nr_objects * sizeof(*written_list));
ALLOC_ARRAY(written_list, to_pack.nr_objects);
write_order = compute_write_order();

do {
Expand Down Expand Up @@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)
if (!to_pack.nr_objects || !window || !depth)
return;

delta_list = xmalloc(to_pack.nr_objects * sizeof(*delta_list));
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
nr_deltas = n = 0;

for (i = 0; i < to_pack.nr_objects; i++) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/pack-redundant.c
Expand Up @@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void)
free_nodes = free_nodes->next;
} else {
int i = 1;
new = xmalloc(sizeof(struct llist_item) * BLKSIZE);
ALLOC_ARRAY(new, BLKSIZE);
for (; i < BLKSIZE; i++)
llist_item_put(&new[i]);
}
Expand Down
5 changes: 2 additions & 3 deletions builtin/receive-pack.c
Expand Up @@ -1591,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands,
{
int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;

si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
si->shallow->nr);
ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
assign_shallow_commits_to_refs(si, si->used_shallow, NULL);

si->need_reachability_test =
Expand Down Expand Up @@ -1658,7 +1657,7 @@ static void update_shallow_info(struct command *commands,
return;
}

ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
ALLOC_ARRAY(ref_status, ref->nr);
assign_shallow_commits_to_refs(si, NULL, ref_status);
for (cmd = commands; cmd; cmd = cmd->next) {
if (is_null_sha1(cmd->new_sha1))
Expand Down
2 changes: 1 addition & 1 deletion column.c
Expand Up @@ -164,7 +164,7 @@ static void display_table(const struct string_list *list,
data.colopts = colopts;
data.opts = *opts;

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

Expand Down
4 changes: 2 additions & 2 deletions combine-diff.c
Expand Up @@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(
struct combine_diff_path paths_head;
struct strbuf base;

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

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

o = xmalloc(sizeof(*o) * num_paths);
ALLOC_ARRAY(o, num_paths);
for (i = 0, p = paths; p; p = p->next, i++)
o[i].obj = p;
order_objects(opt->orderfile, path_path, o, num_paths);
Expand Down
2 changes: 1 addition & 1 deletion commit.c
Expand Up @@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt)

work = xcalloc(cnt, sizeof(*work));
redundant = xcalloc(cnt, 1);
filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
ALLOC_ARRAY(filled_index, cnt - 1);

for (i = 0; i < cnt; i++)
parse_commit(array[i]);
Expand Down
6 changes: 3 additions & 3 deletions compat/mingw.c
Expand Up @@ -852,7 +852,7 @@ static char **get_path_split(void)
if (!n)
return NULL;

path = xmalloc((n+1)*sizeof(char *));
ALLOC_ARRAY(path, n + 1);
p = envpath;
i = 0;
do {
Expand Down Expand Up @@ -937,7 +937,7 @@ static wchar_t *make_environment_block(char **deltaenv)
i++;

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

/* merge supplied environment changes into the temporary environment */
Expand Down Expand Up @@ -1127,7 +1127,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
int argc = 0;
const char **argv2;
while (argv[argc]) argc++;
argv2 = xmalloc(sizeof(*argv) * (argc+1));
ALLOC_ARRAY(argv2, argc + 1);
argv2[0] = (char *)cmd; /* full path to the script file */
memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
pid = mingw_spawnv(prog, argv2, 1);
Expand Down
4 changes: 2 additions & 2 deletions diffcore-order.c
Expand Up @@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile)
}
if (pass == 0) {
order_cnt = cnt;
order = xmalloc(sizeof(*order) * cnt);
ALLOC_ARRAY(order, cnt);
}
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)
if (!q->nr)
return;

o = xmalloc(sizeof(*o) * q->nr);
ALLOC_ARRAY(o, q->nr);
for (i = 0; i < q->nr; i++)
o[i].obj = q->queue[i];
order_objects(orderfile, pair_pathtwo, o, q->nr);
Expand Down
6 changes: 3 additions & 3 deletions dir.c
Expand Up @@ -2448,14 +2448,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_,
ud.untracked_alloc = value;
ud.untracked_nr = value;
if (ud.untracked_nr)
ud.untracked = xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);
ALLOC_ARRAY(ud.untracked, ud.untracked_nr);
data = next;

next = data;
ud.dirs_alloc = ud.dirs_nr = decode_varint(&next);
if (next > end)
return -1;
ud.dirs = xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);
ALLOC_ARRAY(ud.dirs, ud.dirs_nr);
data = next;

len = strlen((const char *)data);
Expand Down Expand Up @@ -2575,7 +2575,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
rd.data = next;
rd.end = end;
rd.index = 0;
rd.ucd = xmalloc(sizeof(*rd.ucd) * len);
ALLOC_ARRAY(rd.ucd, len);

if (read_one_dir(&uc->root, &rd) || rd.index != len)
goto done;
Expand Down
5 changes: 3 additions & 2 deletions fast-import.c
Expand Up @@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)
if (!avail_tree_entry) {
unsigned int n = tree_entry_alloc;
total_allocd += n * sizeof(struct tree_entry);
avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
ALLOC_ARRAY(e, n);
avail_tree_entry = e;
while (n-- > 1) {
*((void**)e) = e + 1;
e++;
Expand Down Expand Up @@ -898,7 +899,7 @@ static const char *create_index(void)
struct object_entry_pool *o;

/* Build the table of object IDs. */
idx = xmalloc(object_count * sizeof(*idx));
ALLOC_ARRAY(idx, object_count);
c = idx;
for (o = blocks; o; o = o->next_pool)
for (e = o->next_free; e-- != o->entries;)
Expand Down
3 changes: 2 additions & 1 deletion fsck.c
Expand Up @@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,

if (!options->msg_type) {
int i;
int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
int *msg_type;
ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
for (i = 0; i < FSCK_MSG_MAX; i++)
msg_type[i] = fsck_msg_type(i, options);
options->msg_type = msg_type;
Expand Down
10 changes: 4 additions & 6 deletions graph.c
Expand Up @@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)
* We'll automatically grow columns later if we need more room.
*/
graph->column_capacity = 30;
graph->columns = xmalloc(sizeof(struct column) *
graph->column_capacity);
graph->new_columns = xmalloc(sizeof(struct column) *
graph->column_capacity);
graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
ALLOC_ARRAY(graph->columns, graph->column_capacity);
ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);

/*
* The diff output prefix callback, with this we can make
Expand Down
2 changes: 1 addition & 1 deletion khash.h
Expand Up @@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;
if (new_n_buckets < 4) new_n_buckets = 4; \
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
else { /* hash table size to be changed (shrink or expand); rehash */ \
new_flags = (khint32_t*)xmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
if (!new_flags) return -1; \
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \
Expand Down
8 changes: 5 additions & 3 deletions levenshtein.c
Expand Up @@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2,
int w, int s, int a, int d)
{
int len1 = strlen(string1), len2 = strlen(string2);
int *row0 = xmalloc(sizeof(int) * (len2 + 1));
int *row1 = xmalloc(sizeof(int) * (len2 + 1));
int *row2 = xmalloc(sizeof(int) * (len2 + 1));
int *row0, *row1, *row2;
int i, j;

ALLOC_ARRAY(row0, len2 + 1);
ALLOC_ARRAY(row1, len2 + 1);
ALLOC_ARRAY(row2, len2 + 1);

for (j = 0; j <= len2; j++)
row1[j] = j * a;
for (i = 0; i < len1; i++) {
Expand Down
8 changes: 4 additions & 4 deletions line-log.c
Expand Up @@ -522,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
if (diff_populate_filespec(spec, 0))
die("Cannot read blob %s", sha1_to_hex(spec->sha1));

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

diffqueues = xmalloc(nparents * sizeof(*diffqueues));
cand = xmalloc(nparents * sizeof(*cand));
parents = xmalloc(nparents * sizeof(*parents));
ALLOC_ARRAY(diffqueues, nparents);
ALLOC_ARRAY(cand, nparents);
ALLOC_ARRAY(parents, nparents);

p = commit->parents;
for (i = 0; i < nparents; i++) {
Expand Down
2 changes: 1 addition & 1 deletion notes.c
Expand Up @@ -1032,7 +1032,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs)
struct string_list_item *item;
int counter = 0;
struct notes_tree **trees;
trees = xmalloc((refs->nr+1) * sizeof(struct notes_tree *));
ALLOC_ARRAY(trees, refs->nr + 1);
for_each_string_list_item(item, refs) {
struct notes_tree *t = xcalloc(1, sizeof(struct notes_tree));
init_notes(t, item->string, combine_notes_ignore, 0);
Expand Down
2 changes: 1 addition & 1 deletion pack-check.c
Expand Up @@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,
* we do not do scan-streaming check on the pack file.
*/
nr_objects = p->num_objects;
entries = xmalloc((nr_objects + 1) * sizeof(*entries));
ALLOC_ARRAY(entries, nr_objects + 1);
entries[nr_objects].offset = pack_sig_ofs;
/* first sort entries by pack offset, since unpacking them is more efficient that way */
for (i = 0; i < nr_objects; i++) {
Expand Down

0 comments on commit b32fa95

Please sign in to comment.