Skip to content

Commit 241b5d3

Browse files
rscharfegitster
authored andcommitted
fix xcalloc() argument order
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 59ec224 commit 241b5d3

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

add-interactive.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ struct file_item {
413413

414414
static void add_file_item(struct string_list *files, const char *name)
415415
{
416-
struct file_item *item = xcalloc(sizeof(*item), 1);
416+
struct file_item *item = xcalloc(1, sizeof(*item));
417417

418418
string_list_append(files, name)->util = item;
419419
}
@@ -476,7 +476,7 @@ static void collect_changes_cb(struct diff_queue_struct *q,
476476

477477
add_file_item(s->files, name);
478478

479-
entry = xcalloc(sizeof(*entry), 1);
479+
entry = xcalloc(1, sizeof(*entry));
480480
hashmap_entry_init(&entry->ent, hash);
481481
entry->name = s->files->items[s->files->nr - 1].string;
482482
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)
11201120
int res = 0;
11211121

11221122
for (i = 0; i < ARRAY_SIZE(command_list); i++) {
1123-
struct command_item *util = xcalloc(sizeof(*util), 1);
1123+
struct command_item *util = xcalloc(1, sizeof(*util));
11241124
util->command = command_list[i].command;
11251125
string_list_append(&commands.items, command_list[i].string)
11261126
->util = util;

blame.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,13 @@ static int *fuzzy_find_matching_lines(struct blame_origin *parent,
951951
max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
952952
- 1) / length_a;
953953

954-
result = xcalloc(sizeof(int), length_b);
955-
second_best_result = xcalloc(sizeof(int), length_b);
956-
certainties = xcalloc(sizeof(int), length_b);
954+
result = xcalloc(length_b, sizeof(int));
955+
second_best_result = xcalloc(length_b, sizeof(int));
956+
certainties = xcalloc(length_b, sizeof(int));
957957

958958
/* See get_similarity() for details of similarities. */
959959
similarity_count = length_b * (max_search_distance_a * 2 + 1);
960-
similarities = xcalloc(sizeof(int), similarity_count);
960+
similarities = xcalloc(similarity_count, sizeof(int));
961961

962962
for (i = 0; i < length_b; ++i) {
963963
result[i] = -1;
@@ -995,7 +995,7 @@ static void fill_origin_fingerprints(struct blame_origin *o)
995995
return;
996996
o->num_lines = find_line_starts(&line_starts, o->file.ptr,
997997
o->file.size);
998-
o->fingerprints = xcalloc(sizeof(struct fingerprint), o->num_lines);
998+
o->fingerprints = xcalloc(o->num_lines, sizeof(struct fingerprint));
999999
get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
10001000
0, o->num_lines);
10011001
free(line_starts);
@@ -1853,8 +1853,8 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
18531853
diffp = NULL;
18541854

18551855
if (ignore_diffs && same - tlno > 0) {
1856-
line_blames = xcalloc(sizeof(struct blame_line_tracker),
1857-
same - tlno);
1856+
line_blames = xcalloc(same - tlno,
1857+
sizeof(struct blame_line_tracker));
18581858
guess_line_blames(parent, target, tlno, offset, same,
18591859
parent_len, line_blames);
18601860
}

contrib/coccinelle/xcalloc.cocci

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@@
2+
type T;
3+
T *ptr;
4+
expression n;
5+
@@
6+
xcalloc(
7+
+ n,
8+
\( sizeof(T) \| sizeof(*ptr) \)
9+
- , n
10+
)

range-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int read_patches(const char *range, struct string_list *list,
9393
string_list_append(list, buf.buf)->util = util;
9494
strbuf_reset(&buf);
9595
}
96-
util = xcalloc(sizeof(*util), 1);
96+
util = xcalloc(1, sizeof(*util));
9797
if (get_oid(p, &util->oid)) {
9898
error(_("could not parse commit '%s'"), p);
9999
free(util);

ref-filter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state
768768
struct strbuf *unused_err)
769769
{
770770
struct ref_formatting_stack *new_stack;
771-
struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
771+
struct if_then_else *if_then_else = xcalloc(1,
772+
sizeof(struct if_then_else));
772773

773774
if_then_else->str = atomv->atom->u.if_then_else.str;
774775
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,
22422243
if (!check_reachable)
22432244
return;
22442245

2245-
to_clear = xcalloc(sizeof(struct commit *), array->nr);
2246+
to_clear = xcalloc(array->nr, sizeof(struct commit *));
22462247

22472248
repo_init_revisions(the_repository, &revs, NULL);
22482249

trailer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static void print_all(FILE *outfile, struct list_head *head,
174174

175175
static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
176176
{
177-
struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
177+
struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
178178
new_item->token = arg_tok->token;
179179
new_item->value = arg_tok->value;
180180
arg_tok->token = arg_tok->value = NULL;
@@ -445,7 +445,7 @@ static struct arg_item *get_conf_item(const char *name)
445445
}
446446

447447
/* Item does not already exists, create it */
448-
item = xcalloc(sizeof(*item), 1);
448+
item = xcalloc(1, sizeof(*item));
449449
duplicate_conf(&item->conf, &default_conf_info);
450450
item->conf.name = xstrdup(name);
451451

@@ -664,7 +664,7 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val,
664664
static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
665665
char *val)
666666
{
667-
struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
667+
struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
668668
new_item->token = tok;
669669
new_item->value = val;
670670
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,
675675
const struct conf_info *conf,
676676
const struct new_trailer_item *new_trailer_item)
677677
{
678-
struct arg_item *new_item = xcalloc(sizeof(*new_item), 1);
678+
struct arg_item *new_item = xcalloc(1, sizeof(*new_item));
679679
new_item->token = tok;
680680
new_item->value = val;
681681
duplicate_conf(&new_item->conf, conf);

0 commit comments

Comments
 (0)