{"thread":{"id":"61461","subject":"[PATCH] compat/regex: fix argument order to calloc(3)","startedAt":"2024-05-12T06:25:10Z","lastAt":"2024-05-13T10:00:17Z","messageCount":2,"participants":["Junio C Hamano","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"494555","messageId":"xmqqbk5bim2n.fsf@gitster.g","threadId":"61461","inReplyTo":null,"subject":"[PATCH] compat/regex: fix argument order to calloc(3)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-05-12T06:25:04Z","receivedAt":"2024-05-12T06:25:10Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Windows compiler suddenly started complaining that calloc(3) takes\nits arguments in <nmemb, size> order.  Indeed, there are many calls\nthat has their arguments in a _wrong_ order.\n\nFix them all.\n\nA sample breakage can be seen at\n\n  https://github.com/git/git/actions/runs/9046793153/job/24857988702#step:4:272\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n * I wonder if there is an easier way to deal with toolchain and/or\n   platform changes (I am assuming that is what caused this\n   breakage) than just seeing and then reacting to a breakage after\n   it happens.\n\n compat/regex/regcomp.c        | 12 ++++++------\n compat/regex/regex_internal.c |  4 ++--\n compat/regex/regexec.c        | 10 +++++-----\n 3 files changed, 13 insertions(+), 13 deletions(-)\n\ndiff --git a/compat/regex/regcomp.c b/compat/regex/regcomp.c\nindex d1bc09e49b..2bc0f1187a 100644\n--- a/compat/regex/regcomp.c\n+++ b/compat/regex/regcomp.c\n@@ -868,7 +868,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)\n     if (table_size > pat_len)\n       break;\n \n-  dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);\n+  dfa->state_table = calloc (table_size, sizeof (struct re_state_table_entry));\n   dfa->state_hash_mask = table_size - 1;\n \n   dfa->mb_cur_max = MB_CUR_MAX;\n@@ -936,7 +936,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)\n \t{\n \t  int i, j, ch;\n \n-\t  dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);\n+\t  dfa->sb_char = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));\n \t  if (BE (dfa->sb_char == NULL, 0))\n \t    return REG_ESPACE;\n \n@@ -3079,9 +3079,9 @@ parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,\n \t\t\t\t\t\t   _NL_COLLATE_SYMB_EXTRAMB);\n     }\n #endif\n-  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);\n+  sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));\n #ifdef RE_ENABLE_I18N\n-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);\n+  mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t));\n #endif /* RE_ENABLE_I18N */\n #ifdef RE_ENABLE_I18N\n   if (BE (sbcset == NULL || mbcset == NULL, 0))\n@@ -3626,9 +3626,9 @@ build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,\n   re_token_t br_token;\n   bin_tree_t *tree;\n \n-  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);\n+  sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t));\n #ifdef RE_ENABLE_I18N\n-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);\n+  mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t));\n #endif /* RE_ENABLE_I18N */\n \n #ifdef RE_ENABLE_I18N\ndiff --git a/compat/regex/regex_internal.c b/compat/regex/regex_internal.c\nindex ec51cf3446..ec5cc5d2dd 100644\n--- a/compat/regex/regex_internal.c\n+++ b/compat/regex/regex_internal.c\n@@ -1628,7 +1628,7 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,\n   reg_errcode_t err;\n   re_dfastate_t *newstate;\n \n-  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);\n+  newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t));\n   if (BE (newstate == NULL, 0))\n     return NULL;\n   err = re_node_set_init_copy (&newstate->nodes, nodes);\n@@ -1678,7 +1678,7 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,\n   reg_errcode_t err;\n   re_dfastate_t *newstate;\n \n-  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);\n+  newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t));\n   if (BE (newstate == NULL, 0))\n     return NULL;\n   err = re_node_set_init_copy (&newstate->nodes, nodes);\ndiff --git a/compat/regex/regexec.c b/compat/regex/regexec.c\nindex 49358ae475..e92be5741d 100644\n--- a/compat/regex/regexec.c\n+++ b/compat/regex/regexec.c\n@@ -2796,8 +2796,8 @@ get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)\n \t    continue; /* No.  */\n \t  if (sub_top->path == NULL)\n \t    {\n-\t      sub_top->path = calloc (sizeof (state_array_t),\n-\t\t\t\t      sl_str - sub_top->str_idx + 1);\n+\t      sub_top->path = calloc (sl_str - sub_top->str_idx + 1,\n+\t\t\t\t      sizeof (state_array_t));\n \t      if (sub_top->path == NULL)\n \t\treturn REG_ESPACE;\n \t    }\n@@ -3361,7 +3361,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)\n       if (ndests == 0)\n \t{\n \t  state->trtable = (re_dfastate_t **)\n-\t    calloc (sizeof (re_dfastate_t *), SBC_MAX);\n+\t    calloc (SBC_MAX, sizeof (re_dfastate_t *));\n \t  return 1;\n \t}\n       return 0;\n@@ -3457,7 +3457,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)\n \t discern by looking at the character code: allocate a\n \t 256-entry transition table.  */\n       trtable = state->trtable =\n-\t(re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);\n+\t(re_dfastate_t **) calloc (SBC_MAX, sizeof (re_dfastate_t *));\n       if (BE (trtable == NULL, 0))\n \tgoto out_free;\n \n@@ -3488,7 +3488,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)\n \t transition tables, one starting at trtable[0] and one\n \t starting at trtable[SBC_MAX].  */\n       trtable = state->word_trtable =\n-\t(re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);\n+\t(re_dfastate_t **) calloc (2 * SBC_MAX, sizeof (re_dfastate_t *));\n       if (BE (trtable == NULL, 0))\n \tgoto out_free;\n \n-- \n2.45.0-119-g0f3415f1f8\n\n"},{"id":"494603","messageId":"ZkHkiBa2hSbjcmZd@tanuki","threadId":"61461","inReplyTo":"xmqqbk5bim2n.fsf@gitster.g","subject":"Re: [PATCH] compat/regex: fix argument order to calloc(3)","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2024-05-13T10:00:10Z","receivedAt":"2024-05-13T10:00:17Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sat, May 11, 2024 at 11:25:04PM -0700, Junio C Hamano wrote:\n> Windows compiler suddenly started complaining that calloc(3) takes\n> its arguments in <nmemb, size> order.  Indeed, there are many calls\n> that has their arguments in a _wrong_ order.\n\nI think this is actually new to GCC 14. The release notes include this\nbit:\n\n    -Walloc-size and -Wcalloc-transposed-args warnings are enabled for C++ as well\n\nIt's a bit puzzling that this seemingly only changed for C++, but oh,\nwell.\n\n> Fix them all.\n> \n> A sample breakage can be seen at\n> \n>   https://github.com/git/git/actions/runs/9046793153/job/24857988702#step:4:272\n> \n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> ---\n> \n>  * I wonder if there is an easier way to deal with toolchain and/or\n>    platform changes (I am assuming that is what caused this\n>    breakage) than just seeing and then reacting to a breakage after\n>    it happens.\n\nI think the best thing we can do is to be proactive and be faster than\nour toolchain to enable (sensible) warnings. Over time all the compilers\ntend to become stricter, and if we are already as strict as possible\nthen chances are less likely that we'll have to do anything about\nupstream changes.\n\nBut other than that I don't really have much of an idea.\n\nThe patch itself looks obviously good to me, thanks!\n"}]}