Subject: Re: [PATCH] fix calloc() and xcalloc() argument ordering

From: Junio C Hamano <[email-masked]>
Date: Tue, 02 Sep 2014 11:42:35 -0700

Arjun Sreedharan <[email-masked]> writes:

> @number first, @size second argument.
>
> Signed-off-by: Arjun Sreedharan <[email-masked]>
> ---
>  builtin/for-each-ref.c        |  2 +-

Thanks.  compat/regex/ being borrowed code, I'd prefer not to touch
them, though.

>  compat/regex/regcomp.c        | 12 ++++++------
>  compat/regex/regex_internal.c |  4 ++--
>  compat/regex/regexec.c        | 10 +++++-----
>  imap-send.c                   |  2 +-
>  5 files changed, 15 insertions(+), 15 deletions(-)
>
> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index 47bd624..69bba06 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -633,7 +633,7 @@ static void populate_value(struct refinfo *ref)
>  	unsigned long size;
>  	const unsigned char *tagged;
>  
> -	ref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);
> +	ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
>  
>  	if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
>  		unsigned char unused1[20];
> diff --git a/compat/regex/regcomp.c b/compat/regex/regcomp.c
> index 06f3088..e375c50 100644
> --- a/compat/regex/regcomp.c
> +++ b/compat/regex/regcomp.c
> @@ -861,7 +861,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)
>      if (table_size > pat_len)
>        break;
>  
> -  dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
> +  dfa->state_table = calloc (table_size, sizeof (struct re_state_table_entry));
>    dfa->state_hash_mask = table_size - 1;
>  
>    dfa->mb_cur_max = MB_CUR_MAX;
> @@ -929,7 +929,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)
>  	{
>  	  int i, j, ch;
>  
> -	  dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
> +	  dfa->sb_char = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));
>  	  if (BE (dfa->sb_char == NULL, 0))
>  	    return REG_ESPACE;
>  
> @@ -3072,9 +3072,9 @@ parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
>  						   _NL_COLLATE_SYMB_EXTRAMB);
>      }
>  #endif
> -  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
> +  sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));
>  #ifdef RE_ENABLE_I18N
> -  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
> +  mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t));
>  #endif /* RE_ENABLE_I18N */
>  #ifdef RE_ENABLE_I18N
>    if (BE (sbcset == NULL || mbcset == NULL, 0))
> @@ -3619,9 +3619,9 @@ build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
>    re_token_t br_token;
>    bin_tree_t *tree;
>  
> -  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
> +  sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));
>  #ifdef RE_ENABLE_I18N
> -  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
> +  mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t));
>  #endif /* RE_ENABLE_I18N */
>  
>  #ifdef RE_ENABLE_I18N
> diff --git a/compat/regex/regex_internal.c b/compat/regex/regex_internal.c
> index d4121f2..9d9e986 100644
> --- a/compat/regex/regex_internal.c
> +++ b/compat/regex/regex_internal.c
> @@ -1629,7 +1629,7 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
>    reg_errcode_t err;
>    re_dfastate_t *newstate;
>  
> -  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
> +  newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t));
>    if (BE (newstate == NULL, 0))
>      return NULL;
>    err = re_node_set_init_copy (&newstate->nodes, nodes);
> @@ -1679,7 +1679,7 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
>    reg_errcode_t err;
>    re_dfastate_t *newstate;
>  
> -  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
> +  newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t));
>    if (BE (newstate == NULL, 0))
>      return NULL;
>    err = re_node_set_init_copy (&newstate->nodes, nodes);
> diff --git a/compat/regex/regexec.c b/compat/regex/regexec.c
> index eb5e1d4..edfe0c6 100644
> --- a/compat/regex/regexec.c
> +++ b/compat/regex/regexec.c
> @@ -2797,8 +2797,8 @@ get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
>  	    continue; /* No.  */
>  	  if (sub_top->path == NULL)
>  	    {
> -	      sub_top->path = calloc (sizeof (state_array_t),
> -				      sl_str - sub_top->str_idx + 1);
> +	      sub_top->path = calloc (sl_str - sub_top->str_idx + 1,
> +				      sizeof (state_array_t));
>  	      if (sub_top->path == NULL)
>  		return REG_ESPACE;
>  	    }
> @@ -3362,7 +3362,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
>        if (ndests == 0)
>  	{
>  	  state->trtable = (re_dfastate_t **)
> -	    calloc (sizeof (re_dfastate_t *), SBC_MAX);
> +	    calloc (SBC_MAX, sizeof (re_dfastate_t *));
>  	  return 1;
>  	}
>        return 0;
> @@ -3458,7 +3458,7 @@ out_free:
>  	 discern by looking at the character code: allocate a
>  	 256-entry transition table.  */
>        trtable = state->trtable =
> -	(re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
> +	(re_dfastate_t **) calloc (SBC_MAX, sizeof (re_dfastate_t *));
>        if (BE (trtable == NULL, 0))
>  	goto out_free;
>  
> @@ -3489,7 +3489,7 @@ out_free:
>  	 transition tables, one starting at trtable[0] and one
>  	 starting at trtable[SBC_MAX].  */
>        trtable = state->word_trtable =
> -	(re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
> +	(re_dfastate_t **) calloc (2 * SBC_MAX, sizeof (re_dfastate_t *));
>        if (BE (trtable == NULL, 0))
>  	goto out_free;
>  
> diff --git a/imap-send.c b/imap-send.c
> index 524fbab..02eb3e0 100644
> --- a/imap-send.c
> +++ b/imap-send.c
> @@ -954,7 +954,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
>  
>  	ctx = xcalloc(1, sizeof(*ctx));
>  
> -	ctx->imap = imap = xcalloc(sizeof(*imap), 1);
> +	ctx->imap = imap = xcalloc(1, sizeof(*imap));
>  	imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
>  	imap->in_progress_append = &imap->in_progress;