]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fix xcalloc() argument order
authorRené Scharfe <l.s.r@web.de>
Sat, 6 Mar 2021 11:26:19 +0000 (12:26 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Mar 2021 17:45:04 +0000 (09:45 -0800)
Pass the number of elements first and ther size second, as expected
by xcalloc().  Provide a semantic patch, which was actually used to
generate the rest of this patch.

The semantic patch would generate flip-flop diffs if both arguments
are sizeofs.  We don't have such a case, and it's hard to imagine
the usefulness of such an allocation.  If it ever occurs then we
could deal with it by duplicating the rule in the semantic patch to
make it cancel itself out, or we could change the code to use
CALLOC_ARRAY.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-interactive.c
blame.c
contrib/coccinelle/xcalloc.cocci [new file with mode: 0644]
range-diff.c
ref-filter.c
trailer.c

index 9b8cdb4a31a6ab69e7bb4e698782b11764c094c7..09faee0e8f8b2694a87e36f6b5e91e6b14300bf7 100644 (file)
@@ -413,7 +413,7 @@ struct file_item {
 
 static void add_file_item(struct string_list *files, const char *name)
 {
-       struct file_item *item = xcalloc(sizeof(*item), 1);
+       struct file_item *item = xcalloc(1, sizeof(*item));
 
        string_list_append(files, name)->util = item;
 }
@@ -476,7 +476,7 @@ static void collect_changes_cb(struct diff_queue_struct *q,
 
                        add_file_item(s->files, name);
 
-                       entry = xcalloc(sizeof(*entry), 1);
+                       entry = xcalloc(1, sizeof(*entry));
                        hashmap_entry_init(&entry->ent, hash);
                        entry->name = s->files->items[s->files->nr - 1].string;
                        entry->item = s->files->items[s->files->nr - 1].util;
@@ -1120,7 +1120,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
        int res = 0;
 
        for (i = 0; i < ARRAY_SIZE(command_list); i++) {
-               struct command_item *util = xcalloc(sizeof(*util), 1);
+               struct command_item *util = xcalloc(1, sizeof(*util));
                util->command = command_list[i].command;
                string_list_append(&commands.items, command_list[i].string)
                        ->util = util;
diff --git a/blame.c b/blame.c
index a5044fcfaa62644daa45103dd6ad17647bc6a811..d6a3f5b70bb6821e4be0860f0a3314a831245edb 100644 (file)
--- a/blame.c
+++ b/blame.c
@@ -951,13 +951,13 @@ static int *fuzzy_find_matching_lines(struct blame_origin *parent,
        max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
                                 - 1) / length_a;
 
-       result = xcalloc(sizeof(int), length_b);
-       second_best_result = xcalloc(sizeof(int), length_b);
-       certainties = xcalloc(sizeof(int), length_b);
+       result = xcalloc(length_b, sizeof(int));
+       second_best_result = xcalloc(length_b, sizeof(int));
+       certainties = xcalloc(length_b, sizeof(int));
 
        /* See get_similarity() for details of similarities. */
        similarity_count = length_b * (max_search_distance_a * 2 + 1);
-       similarities = xcalloc(sizeof(int), similarity_count);
+       similarities = xcalloc(similarity_count, sizeof(int));
 
        for (i = 0; i < length_b; ++i) {
                result[i] = -1;
@@ -995,7 +995,7 @@ static void fill_origin_fingerprints(struct blame_origin *o)
                return;
        o->num_lines = find_line_starts(&line_starts, o->file.ptr,
                                        o->file.size);
-       o->fingerprints = xcalloc(sizeof(struct fingerprint), o->num_lines);
+       o->fingerprints = xcalloc(o->num_lines, sizeof(struct fingerprint));
        get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
                              0, o->num_lines);
        free(line_starts);
@@ -1853,8 +1853,8 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
        diffp = NULL;
 
        if (ignore_diffs && same - tlno > 0) {
-               line_blames = xcalloc(sizeof(struct blame_line_tracker),
-                                     same - tlno);
+               line_blames = xcalloc(same - tlno,
+                                     sizeof(struct blame_line_tracker));
                guess_line_blames(parent, target, tlno, offset, same,
                                  parent_len, line_blames);
        }
diff --git a/contrib/coccinelle/xcalloc.cocci b/contrib/coccinelle/xcalloc.cocci
new file mode 100644 (file)
index 0000000..c291011
--- /dev/null
@@ -0,0 +1,10 @@
+@@
+type T;
+T *ptr;
+expression n;
+@@
+  xcalloc(
++ n,
+  \( sizeof(T) \| sizeof(*ptr) \)
+- , n
+  )
index b9950f10c8c486b16a2b916b0898d88c8d82fb59..dfc20ad349978f38d80d76a1ed88eefb891f93cd 100644 (file)
@@ -93,7 +93,7 @@ static int read_patches(const char *range, struct string_list *list,
                                string_list_append(list, buf.buf)->util = util;
                                strbuf_reset(&buf);
                        }
-                       util = xcalloc(sizeof(*util), 1);
+                       util = xcalloc(1, sizeof(*util));
                        if (get_oid(p, &util->oid)) {
                                error(_("could not parse commit '%s'"), p);
                                free(util);
index ee337df232a5025f474128a4e6331dd8eb167f92..033cd4f9f2a9c15502ec871abaab81c23d006a8c 100644 (file)
@@ -768,7 +768,8 @@ static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state
                           struct strbuf *unused_err)
 {
        struct ref_formatting_stack *new_stack;
-       struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
+       struct if_then_else *if_then_else = xcalloc(1,
+                                                   sizeof(struct if_then_else));
 
        if_then_else->str = atomv->atom->u.if_then_else.str;
        if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
@@ -2242,7 +2243,7 @@ static void reach_filter(struct ref_array *array,
        if (!check_reachable)
                return;
 
-       to_clear = xcalloc(sizeof(struct commit *), array->nr);
+       to_clear = xcalloc(array->nr, sizeof(struct commit *));
 
        repo_init_revisions(the_repository, &revs, NULL);
 
index 3f7391d793c87b9ff9e40572e9c219f9377d9d6a..61ffbf6066c033cf402849b5c446054081762e52 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -174,7 +174,7 @@ static void print_all(FILE *outfile, struct list_head *head,
 
 static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
 {
-       struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
+       struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
        new_item->token = arg_tok->token;
        new_item->value = arg_tok->value;
        arg_tok->token = arg_tok->value = NULL;
@@ -445,7 +445,7 @@ static struct arg_item *get_conf_item(const char *name)
        }
 
        /* Item does not already exists, create it */
-       item = xcalloc(sizeof(*item), 1);
+       item = xcalloc(1, sizeof(*item));
        duplicate_conf(&item->conf, &default_conf_info);
        item->conf.name = xstrdup(name);
 
@@ -664,7 +664,7 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val,
 static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
                                             char *val)
 {
-       struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
+       struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
        new_item->token = tok;
        new_item->value = val;
        list_add_tail(&new_item->list, head);
@@ -675,7 +675,7 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
                         const struct conf_info *conf,
                         const struct new_trailer_item *new_trailer_item)
 {
-       struct arg_item *new_item = xcalloc(sizeof(*new_item), 1);
+       struct arg_item *new_item = xcalloc(1, sizeof(*new_item));
        new_item->token = tok;
        new_item->value = val;
        duplicate_conf(&new_item->conf, conf);