{"thread":{"id":"58894","subject":"[PATCH] maintenance: use xcalloc instead of xmalloc where possible","startedAt":"2022-12-05T14:48:55Z","lastAt":"2024-12-18T16:48:42Z","messageCount":16,"participants":["Rose via GitGitGadget","Jeff Hostetler","Junio C Hamano","Ævar Arnfjörð Bjarmason","Bagas Sanjaya","AreaZR via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"468519","messageId":"pull.1390.git.git.1670251713061.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":null,"subject":"[PATCH] maintenance: use xcalloc instead of xmalloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-05T14:48:32Z","receivedAt":"2022-12-05T14:48:55Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling xcalloc directly\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    maintenance: use xcalloc instead of xmalloc where possible\n    \n    We can avoid having to call memset by calling xcalloc directly\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v1\nPull-Request: https://github.com/git/git/pull/1390\n\n remote.c    | 4 +---\n submodule.c | 3 +--\n 2 files changed, 2 insertions(+), 5 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..75315f3563f 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,7 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n-\n-\tmemset(r, 0, sizeof(*r));\n+\tstruct remote_state *r = xcalloc(1, sizeof(*r));\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..4ca4f6c6590 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1464,8 +1464,7 @@ static const struct submodule *get_non_gitmodules_submodule(const char *path)\n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tret = xcalloc(1, sizeof(*ret));\n \tret->path = name;\n \tret->name = name;\n \n\nbase-commit: 805265fcf7a737664a8321aaf4a0587b78435184\n-- \ngitgitgadget\n"},{"id":"468520","messageId":"b90fb6ba-a94d-144c-a209-d3544c8b2ec6@jeffhostetler.com","threadId":"58894","inReplyTo":"pull.1390.git.git.1670251713061.gitgitgadget@gmail.com","subject":"Re: [PATCH] maintenance: use xcalloc instead of xmalloc where possible","fromName":"Jeff Hostetler","fromEmail":"git@jeffhostetler.com","sentAt":"2022-12-05T15:01:42Z","receivedAt":"2022-12-05T15:01:52Z","isPatch":true,"sender":{"key":"git@jeffhostetler.com","avatar":null},"body":"\n\nOn 12/5/22 9:48 AM, Rose via GitGitGadget wrote:\n> From: Seija <doremylover123@gmail.com>\n> \n> We can avoid having to call memset by calling xcalloc directly\n> \n> Signed-off-by: Seija doremylover123@gmail.com\n> ---\n>      maintenance: use xcalloc instead of xmalloc where possible\n>      \n>      We can avoid having to call memset by calling xcalloc directly\n>      \n>      Signed-off-by: Seija doremylover123@gmail.com\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v1\n> Pull-Request: https://github.com/git/git/pull/1390\n> \n>   remote.c    | 4 +---\n>   submodule.c | 3 +--\n>   2 files changed, 2 insertions(+), 5 deletions(-)\n> \n> diff --git a/remote.c b/remote.c\n> index 60869beebe7..75315f3563f 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -2741,9 +2741,7 @@ void apply_push_cas(struct push_cas_option *cas,\n>   \n>   struct remote_state *remote_state_new(void)\n>   {\n> -\tstruct remote_state *r = xmalloc(sizeof(*r));\n> -\n> -\tmemset(r, 0, sizeof(*r));\n> +\tstruct remote_state *r = xcalloc(1, sizeof(*r));\n>   \n\nWe have a macro to make this easier and hide the messy details:\n\n\tstruct remote_state *r;\n\n\tCALLOC_ARRAY(r, 1);\n\n\nJeff\n"},{"id":"468523","messageId":"pull.1390.v2.git.git.1670254388605.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.git.git.1670251713061.gitgitgadget@gmail.com","subject":"[PATCH v2] maintenance: use xcalloc instead of xmalloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-05T15:33:08Z","receivedAt":"2022-12-05T15:34:08Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling xcalloc directly\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    maintenance: use xcalloc instead of xmalloc where possible\n    \n    We can avoid having to call memset by calling xcalloc directly\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v2\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v1:\n\n 1:  f56282194a7 ! 1:  ee8a7af6435 maintenance: use xcalloc instead of xmalloc where possible\n     @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n       struct remote_state *remote_state_new(void)\n       {\n      -\tstruct remote_state *r = xmalloc(sizeof(*r));\n     --\n     ++\tstruct remote_state *r;\n     + \n      -\tmemset(r, 0, sizeof(*r));\n     -+\tstruct remote_state *r = xcalloc(1, sizeof(*r));\n     ++\tCALLOC_ARRAY(r, 1);\n       \n       \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n       \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\n      \n       ## submodule.c ##\n     -@@ submodule.c: static const struct submodule *get_non_gitmodules_submodule(const char *path)\n     +@@ submodule.c: struct fetch_task {\n     +  */\n     + static const struct submodule *get_non_gitmodules_submodule(const char *path)\n     + {\n     +-\tstruct submodule *ret = NULL;\n     ++\tstruct submodule *ret;\n     + \tconst char *name = default_name_or_path(path);\n     + \n       \tif (!name)\n       \t\treturn NULL;\n       \n      -\tret = xmalloc(sizeof(*ret));\n      -\tmemset(ret, 0, sizeof(*ret));\n     -+\tret = xcalloc(1, sizeof(*ret));\n     ++\tCALLOC_ARRAY(ret, 1);\n       \tret->path = name;\n       \tret->name = name;\n       \n\n\n remote.c    | 4 ++--\n submodule.c | 5 ++---\n 2 files changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..475a1d18af0 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..d43774c7527 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1458,14 +1458,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n\nbase-commit: 805265fcf7a737664a8321aaf4a0587b78435184\n-- \ngitgitgadget\n"},{"id":"468525","messageId":"pull.1390.v3.git.git.1670256084172.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v2.git.git.1670254388605.gitgitgadget@gmail.com","subject":"[PATCH v3] maintenance: use xcalloc instead of xmalloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-05T16:01:23Z","receivedAt":"2022-12-05T16:01:31Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling xcalloc directly\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    maintenance: use xcalloc instead of xmalloc where possible\n    \n    We can avoid having to call memset by calling xcalloc directly\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v3\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v2:\n\n 1:  ee8a7af6435 ! 1:  b5cfec60048 maintenance: use xcalloc instead of xmalloc where possible\n     @@ Commit message\n      \n          Signed-off-by: Seija doremylover123@gmail.com\n      \n     + ## builtin/pack-redundant.c ##\n     +@@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     + \t\tnew_item = free_nodes;\n     + \t\tfree_nodes = free_nodes->next;\n     + \t} else {\n     +-\t\tint i = 1;\n     ++\t\tsize_t i = 1;\n     + \t\tALLOC_ARRAY(new_item, BLKSIZE);\n     + \t\tfor (; i < BLKSIZE; i++)\n     + \t\t\tllist_item_put(&new_item[i]);\n     +@@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     + \n     + static inline void llist_init(struct llist **list)\n     + {\n     +-\t*list = xmalloc(sizeof(struct llist));\n     +-\t(*list)->front = (*list)->back = NULL;\n     +-\t(*list)->size = 0;\n     ++\tCALLOC_ARRAY(*list, 1);\n     + }\n     + \n     + static struct llist * llist_copy(struct llist *list)\n     +\n       ## remote.c ##\n      @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n       \n     @@ submodule.c: struct fetch_task {\n       \tret->path = name;\n       \tret->name = name;\n       \n     +@@ submodule.c: static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n     + \t\t\t\t\t    const char *path,\n     + \t\t\t\t\t    const struct object_id *treeish_name)\n     + {\n     +-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n     +-\tmemset(task, 0, sizeof(*task));\n     ++\tstruct fetch_task *task;\n     ++\n     ++\tCALLOC_ARRAY(task, 1);\n     + \n     + \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n     + \n     +\n     + ## xdiff/xutils.c ##\n     +@@ xdiff/xutils.c: void *xdl_cha_alloc(chastore_t *cha) {\n     + \tvoid *data;\n     + \n     + \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n     +-\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n     ++\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n     + \n     + \t\t\treturn NULL;\n     + \t\t}\n     +-\t\tancur->icurr = 0;\n     +-\t\tancur->next = NULL;\n     + \t\tif (cha->tail)\n     + \t\t\tcha->tail->next = ancur;\n     + \t\tif (!cha->head)\n\n\n builtin/pack-redundant.c |  6 ++----\n remote.c                 |  4 ++--\n submodule.c              | 10 +++++-----\n xdiff/xutils.c           |  4 +---\n 4 files changed, 10 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex ecd49ca268f..0e184bb5212 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -51,7 +51,7 @@ static inline struct llist_item *llist_item_get(void)\n \t\tnew_item = free_nodes;\n \t\tfree_nodes = free_nodes->next;\n \t} else {\n-\t\tint i = 1;\n+\t\tsize_t i = 1;\n \t\tALLOC_ARRAY(new_item, BLKSIZE);\n \t\tfor (; i < BLKSIZE; i++)\n \t\t\tllist_item_put(&new_item[i]);\n@@ -61,9 +61,7 @@ static inline struct llist_item *llist_item_get(void)\n \n static inline void llist_init(struct llist **list)\n {\n-\t*list = xmalloc(sizeof(struct llist));\n-\t(*list)->front = (*list)->back = NULL;\n-\t(*list)->size = 0;\n+\tCALLOC_ARRAY(*list, 1);\n }\n \n static struct llist * llist_copy(struct llist *list)\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..475a1d18af0 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..015102a83d6 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1458,14 +1458,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n \ndiff --git a/xdiff/xutils.c b/xdiff/xutils.c\nindex 9e36f24875d..c19bc441a96 100644\n--- a/xdiff/xutils.c\n+++ b/xdiff/xutils.c\n@@ -98,12 +98,10 @@ void *xdl_cha_alloc(chastore_t *cha) {\n \tvoid *data;\n \n \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n-\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n+\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n \n \t\t\treturn NULL;\n \t\t}\n-\t\tancur->icurr = 0;\n-\t\tancur->next = NULL;\n \t\tif (cha->tail)\n \t\t\tcha->tail->next = ancur;\n \t\tif (!cha->head)\n\nbase-commit: 805265fcf7a737664a8321aaf4a0587b78435184\n-- \ngitgitgadget\n"},{"id":"468526","messageId":"pull.1390.v4.git.git.1670256724311.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v3.git.git.1670256084172.gitgitgadget@gmail.com","subject":"[PATCH v4] maintenance: use calloc instead of malloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-05T16:12:04Z","receivedAt":"2022-12-05T16:12:13Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling calloc directly\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    maintenance: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc directly\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v4\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v3:\n\n 1:  b5cfec60048 ! 1:  3cd6b1eab13 maintenance: use xcalloc instead of xmalloc where possible\n     @@ Metadata\n      Author: Seija <doremylover123@gmail.com>\n      \n       ## Commit message ##\n     -    maintenance: use xcalloc instead of xmalloc where possible\n     +    maintenance: use calloc instead of malloc where possible\n      \n     -    We can avoid having to call memset by calling xcalloc directly\n     +    We can avoid having to call memset by calling calloc directly\n      \n          Signed-off-by: Seija doremylover123@gmail.com\n      \n\n\n builtin/pack-redundant.c |  6 ++----\n remote.c                 |  4 ++--\n submodule.c              | 10 +++++-----\n xdiff/xutils.c           |  4 +---\n 4 files changed, 10 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex ecd49ca268f..0e184bb5212 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -51,7 +51,7 @@ static inline struct llist_item *llist_item_get(void)\n \t\tnew_item = free_nodes;\n \t\tfree_nodes = free_nodes->next;\n \t} else {\n-\t\tint i = 1;\n+\t\tsize_t i = 1;\n \t\tALLOC_ARRAY(new_item, BLKSIZE);\n \t\tfor (; i < BLKSIZE; i++)\n \t\t\tllist_item_put(&new_item[i]);\n@@ -61,9 +61,7 @@ static inline struct llist_item *llist_item_get(void)\n \n static inline void llist_init(struct llist **list)\n {\n-\t*list = xmalloc(sizeof(struct llist));\n-\t(*list)->front = (*list)->back = NULL;\n-\t(*list)->size = 0;\n+\tCALLOC_ARRAY(*list, 1);\n }\n \n static struct llist * llist_copy(struct llist *list)\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..475a1d18af0 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..015102a83d6 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1458,14 +1458,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n \ndiff --git a/xdiff/xutils.c b/xdiff/xutils.c\nindex 9e36f24875d..c19bc441a96 100644\n--- a/xdiff/xutils.c\n+++ b/xdiff/xutils.c\n@@ -98,12 +98,10 @@ void *xdl_cha_alloc(chastore_t *cha) {\n \tvoid *data;\n \n \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n-\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n+\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n \n \t\t\treturn NULL;\n \t\t}\n-\t\tancur->icurr = 0;\n-\t\tancur->next = NULL;\n \t\tif (cha->tail)\n \t\t\tcha->tail->next = ancur;\n \t\tif (!cha->head)\n\nbase-commit: 805265fcf7a737664a8321aaf4a0587b78435184\n-- \ngitgitgadget\n"},{"id":"468605","messageId":"xmqqpmcxqg2z.fsf@gitster.g","threadId":"58894","inReplyTo":"pull.1390.v4.git.git.1670256724311.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] maintenance: use calloc instead of malloc where possible","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-12-06T05:03:16Z","receivedAt":"2022-12-06T05:03:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Rose via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Seija <doremylover123@gmail.com>\n>\n> We can avoid having to call memset by calling calloc directly\n\nWhat is not explained here is how that makes the code better in what\nway.  Reduced line count?  Reduced cycle count?  Reduced line count?\n\nThe reason I ask is because the patch touches codepaths that do not\ncall memset() after malloc(), which the above may explain.\n\nAlso, the patch does not use calloc() directly, but uses\nCALLOC_ARRAY() doesn't it?\n\n>      +    maintenance: use calloc instead of malloc where possible\n\nThis is not about \"git maintenance\", is it?  Why is that subsystem\nspecifically named here?\n\nThis is not about the patch, because Signed-off-by: line has a legal\nmeaning (also see SubmittingPatches[[real-name]]), would you mind\nexplaining what is going on with your name?  The e-mailed patches\ncome from \"Rose\", but the patch author identifies themselves as\n\"Seija\".\n\n> diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\n> index ecd49ca268f..0e184bb5212 100644\n> --- a/builtin/pack-redundant.c\n> +++ b/builtin/pack-redundant.c\n> @@ -51,7 +51,7 @@ static inline struct llist_item *llist_item_get(void)\n>  \t\tnew_item = free_nodes;\n>  \t\tfree_nodes = free_nodes->next;\n>  \t} else {\n> -\t\tint i = 1;\n> +\t\tsize_t i = 1;\n>  \t\tALLOC_ARRAY(new_item, BLKSIZE);\n>  \t\tfor (; i < BLKSIZE; i++)\n>  \t\t\tllist_item_put(&new_item[i]);\n\nThis is unrelated change, isn't it?\n\n> @@ -61,9 +61,7 @@ static inline struct llist_item *llist_item_get(void)\n>  \n>  static inline void llist_init(struct llist **list)\n>  {\n> -\t*list = xmalloc(sizeof(struct llist));\n> -\t(*list)->front = (*list)->back = NULL;\n> -\t(*list)->size = 0;\n> +\tCALLOC_ARRAY(*list, 1);\n>  }\n\nI am somewhat torn on this one.\n\nThe original makes it crystal clear that the initial state of the\nllist data structure is that the .front and the .back pointers that\npoint at the head and the tail of a linearly linked list point at\nNULL and the .size member indicates there is zero elements on the\nlist.  IOW, unlike a mindless \"memset()\" in a\n\n\tx = malloc(sizeof(...));\n\tmemset(x, '\\0', sizeof(...));\n\nsequence, the way members are cleared was meaningful in the\noriginal.\n\nUsing CALLOC_ARRAY() makes it as bad as use of memset() that blindly\nfills the memory reason with NUL bytes.  Surely a NULL pointer may\nhave the same bit representation as a region of memory filled with\nNUL, and a size_t integer whose value is zero may also share the\nsame bit representation, but it lost clarity of the original.\n\nOn the other hand, if we are willing to accept the conciseness, and\naccept the \"\\0 filled memory region is the naturally initialized\nstate for this structure\" convention, then I do not see the value of\nhaving a separate llist_init() helper function.  It is used only in\na handful places in this single file, so getting rid of the helper\nand writing CALLOC_ARRAY(x, 1) to clear each instance of the structure\nit is used to clear may not be a bad thing.\n\nAnd the presented solution is neither.  Again, I'd prefer to keep\nthe original, but if we must use CALLOC_ARRAY() to replace it, then\nI'd prefer to see that the helper function to be removed.\n\n> diff --git a/remote.c b/remote.c\n> index 60869beebe7..475a1d18af0 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n>  \n>  struct remote_state *remote_state_new(void)\n>  {\n> -\tstruct remote_state *r = xmalloc(sizeof(*r));\n> +\tstruct remote_state *r;\n>  \n> -\tmemset(r, 0, sizeof(*r));\n> +\tCALLOC_ARRAY(r, 1);\n\nThis IS an improvement.  We do the mindless clearing either way, it\nis just shorter and easier to follow.\n\n> diff --git a/submodule.c b/submodule.c\n> index 8ac2fca855d..015102a83d6 100644\n> --- a/submodule.c\n> +++ b/submodule.c\n> @@ -1458,14 +1458,13 @@ struct fetch_task {\n>   */\n>  static const struct submodule *get_non_gitmodules_submodule(const char *path)\n>  {\n> -\tstruct submodule *ret = NULL;\n> +\tstruct submodule *ret;\n>  \tconst char *name = default_name_or_path(path);\n>  \n>  \tif (!name)\n>  \t\treturn NULL;\n>  \n> -\tret = xmalloc(sizeof(*ret));\n> -\tmemset(ret, 0, sizeof(*ret));\n> +\tCALLOC_ARRAY(ret, 1);\n\nDitto.\n\n> @@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n>  \t\t\t\t\t    const char *path,\n>  \t\t\t\t\t    const struct object_id *treeish_name)\n>  {\n> -\tstruct fetch_task *task = xmalloc(sizeof(*task));\n> -\tmemset(task, 0, sizeof(*task));\n> +\tstruct fetch_task *task;\n> +\n> +\tCALLOC_ARRAY(task, 1);\n>  \n>  \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n\nDitto.\n\n> diff --git a/xdiff/xutils.c b/xdiff/xutils.c\n> index 9e36f24875d..c19bc441a96 100644\n> --- a/xdiff/xutils.c\n> +++ b/xdiff/xutils.c\n> @@ -98,12 +98,10 @@ void *xdl_cha_alloc(chastore_t *cha) {\n>  \tvoid *data;\n>  \n>  \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n> -\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n> +\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n>  \n>  \t\t\treturn NULL;\n>  \t\t}\n> -\t\tancur->icurr = 0;\n> -\t\tancur->next = NULL;\n\nI am somewhat negative on this for the same reason why I'd prefer to\nkeep the llist thing intact.  Also xdiff code is a borrowed code and\nI'd rather see us not to touch it unnecessarily.\n\n>  \t\tif (cha->tail)\n>  \t\t\tcha->tail->next = ancur;\n>  \t\tif (!cha->head)\n>\n> base-commit: 805265fcf7a737664a8321aaf4a0587b78435184\n\nThanks.\n"},{"id":"468630","messageId":"pull.1390.v5.git.git.1670348301601.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v4.git.git.1670256724311.gitgitgadget@gmail.com","subject":"[PATCH v5] revision: use calloc instead of malloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-06T17:38:21Z","receivedAt":"2022-12-06T17:40:26Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling calloc directly\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    revision: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc directly\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v5\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v4:\n\n 1:  3cd6b1eab13 ! 1:  8072fa30e4f maintenance: use calloc instead of malloc where possible\n     @@ Metadata\n      Author: Seija <doremylover123@gmail.com>\n      \n       ## Commit message ##\n     -    maintenance: use calloc instead of malloc where possible\n     +    revision: use calloc instead of malloc where possible\n      \n          We can avoid having to call memset by calling calloc directly\n      \n     @@ Commit message\n      \n       ## builtin/pack-redundant.c ##\n      @@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     - \t\tnew_item = free_nodes;\n     - \t\tfree_nodes = free_nodes->next;\n     - \t} else {\n     --\t\tint i = 1;\n     -+\t\tsize_t i = 1;\n     - \t\tALLOC_ARRAY(new_item, BLKSIZE);\n     - \t\tfor (; i < BLKSIZE; i++)\n     - \t\t\tllist_item_put(&new_item[i]);\n     -@@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     + \treturn new_item;\n     + }\n       \n     - static inline void llist_init(struct llist **list)\n     - {\n     +-static inline void llist_init(struct llist **list)\n     +-{\n      -\t*list = xmalloc(sizeof(struct llist));\n      -\t(*list)->front = (*list)->back = NULL;\n      -\t(*list)->size = 0;\n     -+\tCALLOC_ARRAY(*list, 1);\n     - }\n     - \n     +-}\n     +-\n       static struct llist * llist_copy(struct llist *list)\n     + {\n     + \tstruct llist *ret;\n     + \tstruct llist_item *new_item, *old_item, *prev;\n     + \n     +-\tllist_init(&ret);\n     ++\tCALLOC_ARRAY(ret, 1);\n     + \n     + \tif ((ret->size = list->size) == 0)\n     + \t\treturn ret;\n     +@@ builtin/pack-redundant.c: static void load_all_objects(void)\n     + \tstruct pack_list *pl = local_packs;\n     + \tstruct llist_item *hint, *l;\n     + \n     +-\tllist_init(&all_objects);\n     ++\tCALLOC_ARRAY(all_objects, 1);\n     + \n     + \twhile (pl) {\n     + \t\thint = NULL;\n     +@@ builtin/pack-redundant.c: static void cmp_local_packs(void)\n     + \n     + \t/* only one packfile */\n     + \tif (!pl->next) {\n     +-\t\tllist_init(&pl->unique_objects);\n     ++\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n     + \t\treturn;\n     + \t}\n     + \n     +@@ builtin/pack-redundant.c: static struct pack_list * add_pack(struct packed_git *p)\n     + \t\treturn NULL;\n     + \n     + \tl.pack = p;\n     +-\tllist_init(&l.remaining_objects);\n     ++\tCALLOC_ARRAY(l.remaining_objects, 1);\n     + \n     + \tif (open_pack_index(p))\n     + \t\treturn NULL;\n     +@@ builtin/pack-redundant.c: int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n     + \t\tscan_alt_odb_packs();\n     + \n     + \t/* ignore objects given on stdin */\n     +-\tllist_init(&ignore);\n     ++\tCALLOC_ARRAY(ignore, 1);\n     + \tif (!isatty(0)) {\n     + \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n     + \t\t\toid = xmalloc(sizeof(*oid));\n      \n       ## remote.c ##\n      @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n     @@ submodule.c: static struct fetch_task *fetch_task_create(struct submodule_parall\n       \n       \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n       \n     -\n     - ## xdiff/xutils.c ##\n     -@@ xdiff/xutils.c: void *xdl_cha_alloc(chastore_t *cha) {\n     - \tvoid *data;\n     - \n     - \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n     --\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n     -+\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n     - \n     - \t\t\treturn NULL;\n     - \t\t}\n     --\t\tancur->icurr = 0;\n     --\t\tancur->next = NULL;\n     - \t\tif (cha->tail)\n     - \t\t\tcha->tail->next = ancur;\n     - \t\tif (!cha->head)\n\n\n builtin/pack-redundant.c | 17 +++++------------\n remote.c                 |  4 ++--\n submodule.c              | 10 +++++-----\n 3 files changed, 12 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex ecd49ca268f..ce5be807cf0 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -59,19 +59,12 @@ static inline struct llist_item *llist_item_get(void)\n \treturn new_item;\n }\n \n-static inline void llist_init(struct llist **list)\n-{\n-\t*list = xmalloc(sizeof(struct llist));\n-\t(*list)->front = (*list)->back = NULL;\n-\t(*list)->size = 0;\n-}\n-\n static struct llist * llist_copy(struct llist *list)\n {\n \tstruct llist *ret;\n \tstruct llist_item *new_item, *old_item, *prev;\n \n-\tllist_init(&ret);\n+\tCALLOC_ARRAY(ret, 1);\n \n \tif ((ret->size = list->size) == 0)\n \t\treturn ret;\n@@ -448,7 +441,7 @@ static void load_all_objects(void)\n \tstruct pack_list *pl = local_packs;\n \tstruct llist_item *hint, *l;\n \n-\tllist_init(&all_objects);\n+\tCALLOC_ARRAY(all_objects, 1);\n \n \twhile (pl) {\n \t\thint = NULL;\n@@ -475,7 +468,7 @@ static void cmp_local_packs(void)\n \n \t/* only one packfile */\n \tif (!pl->next) {\n-\t\tllist_init(&pl->unique_objects);\n+\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n \t\treturn;\n \t}\n \n@@ -512,7 +505,7 @@ static struct pack_list * add_pack(struct packed_git *p)\n \t\treturn NULL;\n \n \tl.pack = p;\n-\tllist_init(&l.remaining_objects);\n+\tCALLOC_ARRAY(l.remaining_objects, 1);\n \n \tif (open_pack_index(p))\n \t\treturn NULL;\n@@ -620,7 +613,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n \t\tscan_alt_odb_packs();\n \n \t/* ignore objects given on stdin */\n-\tllist_init(&ignore);\n+\tCALLOC_ARRAY(ignore, 1);\n \tif (!isatty(0)) {\n \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n \t\t\toid = xmalloc(sizeof(*oid));\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..475a1d18af0 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..015102a83d6 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1458,14 +1458,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n \n\nbase-commit: 2e71cbbddd64695d43383c25c7a054ac4ff86882\n-- \ngitgitgadget\n"},{"id":"468635","messageId":"221206.86k034b98o.gmgdl@evledraar.gmail.com","threadId":"58894","inReplyTo":"pull.1390.v5.git.git.1670348301601.gitgitgadget@gmail.com","subject":"Re: [PATCH v5] revision: use calloc instead of malloc where possible","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-12-06T19:44:11Z","receivedAt":"2022-12-06T19:52:52Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Dec 06 2022, Rose via GitGitGadget wrote:\n\n> From: Seija <doremylover123@gmail.com>\n>\n> We can avoid having to call memset by calling calloc directly\n>\n> Signed-off-by: Seija doremylover123@gmail.com\n> ---\n>     revision: use calloc instead of malloc where possible\n>     \n>     We can avoid having to call memset by calling calloc directly\n>     \n>     Signed-off-by: Seija doremylover123@gmail.com\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v5\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v5\n> Pull-Request: https://github.com/git/git/pull/1390\n>\n> Range-diff vs v4:\n>\n>  1:  3cd6b1eab13 ! 1:  8072fa30e4f maintenance: use calloc instead of malloc where possible\n>      @@ Metadata\n>       Author: Seija <doremylover123@gmail.com>\n>       \n>        ## Commit message ##\n>      -    maintenance: use calloc instead of malloc where possible\n>      +    revision: use calloc instead of malloc where possible\n>       \n>           We can avoid having to call memset by calling calloc directly\n>       \n>      @@ Commit message\n>       \n>        ## builtin/pack-redundant.c ##\n>       @@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n>      - \t\tnew_item = free_nodes;\n>      - \t\tfree_nodes = free_nodes->next;\n>      - \t} else {\n>      --\t\tint i = 1;\n>      -+\t\tsize_t i = 1;\n>      - \t\tALLOC_ARRAY(new_item, BLKSIZE);\n>      - \t\tfor (; i < BLKSIZE; i++)\n>      - \t\t\tllist_item_put(&new_item[i]);\n>      -@@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n>      + \treturn new_item;\n>      + }\n>        \n>      - static inline void llist_init(struct llist **list)\n>      - {\n>      +-static inline void llist_init(struct llist **list)\n>      +-{\n>       -\t*list = xmalloc(sizeof(struct llist));\n>       -\t(*list)->front = (*list)->back = NULL;\n>       -\t(*list)->size = 0;\n>      -+\tCALLOC_ARRAY(*list, 1);\n>      - }\n>      - \n>      +-}\n>      +-\n>        static struct llist * llist_copy(struct llist *list)\n>      + {\n>      + \tstruct llist *ret;\n>      + \tstruct llist_item *new_item, *old_item, *prev;\n>      + \n>      +-\tllist_init(&ret);\n>      ++\tCALLOC_ARRAY(ret, 1);\n>      + \n>      + \tif ((ret->size = list->size) == 0)\n>      + \t\treturn ret;\n>      +@@ builtin/pack-redundant.c: static void load_all_objects(void)\n>      + \tstruct pack_list *pl = local_packs;\n>      + \tstruct llist_item *hint, *l;\n>      + \n>      +-\tllist_init(&all_objects);\n>      ++\tCALLOC_ARRAY(all_objects, 1);\n>      + \n>      + \twhile (pl) {\n>      + \t\thint = NULL;\n>      +@@ builtin/pack-redundant.c: static void cmp_local_packs(void)\n>      + \n>      + \t/* only one packfile */\n>      + \tif (!pl->next) {\n>      +-\t\tllist_init(&pl->unique_objects);\n>      ++\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n>      + \t\treturn;\n>      + \t}\n>      + \n>      +@@ builtin/pack-redundant.c: static struct pack_list * add_pack(struct packed_git *p)\n>      + \t\treturn NULL;\n>      + \n>      + \tl.pack = p;\n>      +-\tllist_init(&l.remaining_objects);\n>      ++\tCALLOC_ARRAY(l.remaining_objects, 1);\n>      + \n>      + \tif (open_pack_index(p))\n>      + \t\treturn NULL;\n>      +@@ builtin/pack-redundant.c: int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n>      + \t\tscan_alt_odb_packs();\n>      + \n>      + \t/* ignore objects given on stdin */\n>      +-\tllist_init(&ignore);\n>      ++\tCALLOC_ARRAY(ignore, 1);\n>      + \tif (!isatty(0)) {\n>      + \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n>      + \t\t\toid = xmalloc(sizeof(*oid));\n>       \n>        ## remote.c ##\n>       @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n>      @@ submodule.c: static struct fetch_task *fetch_task_create(struct submodule_parall\n>        \n>        \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n>        \n>      -\n>      - ## xdiff/xutils.c ##\n>      -@@ xdiff/xutils.c: void *xdl_cha_alloc(chastore_t *cha) {\n>      - \tvoid *data;\n>      - \n>      - \tif (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {\n>      --\t\tif (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {\n>      -+\t\tif (!(ancur = (chanode_t *) xdl_calloc(1, sizeof(chanode_t) + cha->nsize))) {\n>      - \n>      - \t\t\treturn NULL;\n>      - \t\t}\n>      --\t\tancur->icurr = 0;\n>      --\t\tancur->next = NULL;\n>      - \t\tif (cha->tail)\n>      - \t\t\tcha->tail->next = ancur;\n>      - \t\tif (!cha->head)\n>\n>\n>  builtin/pack-redundant.c | 17 +++++------------\n>  remote.c                 |  4 ++--\n>  submodule.c              | 10 +++++-----\n>  3 files changed, 12 insertions(+), 19 deletions(-)\n>\n> diff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\n> index ecd49ca268f..ce5be807cf0 100644\n> --- a/builtin/pack-redundant.c\n> +++ b/builtin/pack-redundant.c\n> @@ -59,19 +59,12 @@ static inline struct llist_item *llist_item_get(void)\n>  \treturn new_item;\n>  }\n>  \n> -static inline void llist_init(struct llist **list)\n> -{\n> -\t*list = xmalloc(sizeof(struct llist));\n> -\t(*list)->front = (*list)->back = NULL;\n> -\t(*list)->size = 0;\n> -}\n> -\n>  static struct llist * llist_copy(struct llist *list)\n>  {\n>  \tstruct llist *ret;\n>  \tstruct llist_item *new_item, *old_item, *prev;\n>  \n> -\tllist_init(&ret);\n> +\tCALLOC_ARRAY(ret, 1);\n>  \n>  \tif ((ret->size = list->size) == 0)\n>  \t\treturn ret;\n> @@ -448,7 +441,7 @@ static void load_all_objects(void)\n>  \tstruct pack_list *pl = local_packs;\n>  \tstruct llist_item *hint, *l;\n>  \n> -\tllist_init(&all_objects);\n> +\tCALLOC_ARRAY(all_objects, 1);\n>  \n>  \twhile (pl) {\n>  \t\thint = NULL;\n> @@ -475,7 +468,7 @@ static void cmp_local_packs(void)\n>  \n>  \t/* only one packfile */\n>  \tif (!pl->next) {\n> -\t\tllist_init(&pl->unique_objects);\n> +\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n>  \t\treturn;\n>  \t}\n>  \n> @@ -512,7 +505,7 @@ static struct pack_list * add_pack(struct packed_git *p)\n>  \t\treturn NULL;\n>  \n>  \tl.pack = p;\n> -\tllist_init(&l.remaining_objects);\n> +\tCALLOC_ARRAY(l.remaining_objects, 1);\n>  \n>  \tif (open_pack_index(p))\n>  \t\treturn NULL;\n> @@ -620,7 +613,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n>  \t\tscan_alt_odb_packs();\n>  \n>  \t/* ignore objects given on stdin */\n> -\tllist_init(&ignore);\n> +\tCALLOC_ARRAY(ignore, 1);\n>  \tif (!isatty(0)) {\n>  \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n>  \t\t\toid = xmalloc(sizeof(*oid));\n> diff --git a/remote.c b/remote.c\n> index 60869beebe7..475a1d18af0 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n>  \n>  struct remote_state *remote_state_new(void)\n>  {\n> -\tstruct remote_state *r = xmalloc(sizeof(*r));\n> +\tstruct remote_state *r;\n>  \n> -\tmemset(r, 0, sizeof(*r));\n> +\tCALLOC_ARRAY(r, 1);\n>  \n>  \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n>  \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\n> diff --git a/submodule.c b/submodule.c\n> index 8ac2fca855d..015102a83d6 100644\n> --- a/submodule.c\n> +++ b/submodule.c\n> @@ -1458,14 +1458,13 @@ struct fetch_task {\n>   */\n>  static const struct submodule *get_non_gitmodules_submodule(const char *path)\n>  {\n> -\tstruct submodule *ret = NULL;\n> +\tstruct submodule *ret;\n>  \tconst char *name = default_name_or_path(path);\n>  \n>  \tif (!name)\n>  \t\treturn NULL;\n>  \n> -\tret = xmalloc(sizeof(*ret));\n> -\tmemset(ret, 0, sizeof(*ret));\n> +\tCALLOC_ARRAY(ret, 1);\n>  \tret->path = name;\n>  \tret->name = name;\n>  \n> @@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n>  \t\t\t\t\t    const char *path,\n>  \t\t\t\t\t    const struct object_id *treeish_name)\n>  {\n> -\tstruct fetch_task *task = xmalloc(sizeof(*task));\n> -\tmemset(task, 0, sizeof(*task));\n> +\tstruct fetch_task *task;\n> +\n> +\tCALLOC_ARRAY(task, 1);\n>  \n>  \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n>  \n>\n> base-commit: 2e71cbbddd64695d43383c25c7a054ac4ff86882\n\nThis is partially some of the sentiments Junio did in the v4 review\n(although I looked at this before reading that).\n\nI think calloc-ing or memset-ing a struct is going in the opposite\ndirection of where we've been trending, which is to have explicit\ninitializers. Sometimes it's justified, but in the \"pack-redundant.c\"\ncase I think it's probably better & more future-proof to keep it, and\nget rid of the odd patter nof passing in a ** to an init function to\nhave it alloc for us.\n\nI.e. this (there's some stray new free() in there from testing, sorry,\nbut those are also bugs we should fix...):\n\n\tdiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\n\tindex ecd49ca268f..af91d643dd2 100644\n\t--- a/builtin/pack-redundant.c\n\t+++ b/builtin/pack-redundant.c\n\t@@ -27,6 +27,7 @@ static struct llist {\n\t \tstruct llist_item *back;\n\t \tsize_t size;\n\t } *all_objects; /* all objects which must be present in local packfiles */\n\t+#define LLIST_INIT { 0 }\n\t \n\t static struct pack_list {\n\t \tstruct pack_list *next;\n\t@@ -59,19 +60,18 @@ static inline struct llist_item *llist_item_get(void)\n\t \treturn new_item;\n\t }\n\t \n\t-static inline void llist_init(struct llist **list)\n\t+static inline void llist_init(struct llist *list)\n\t {\n\t-\t*list = xmalloc(sizeof(struct llist));\n\t-\t(*list)->front = (*list)->back = NULL;\n\t-\t(*list)->size = 0;\n\t+\tstruct llist blank = LLIST_INIT;\n\t+\tmemcpy(list, &blank, sizeof(*list));\n\t }\n\t \n\t static struct llist * llist_copy(struct llist *list)\n\t {\n\t-\tstruct llist *ret;\n\t+\tstruct llist *ret = xmalloc(sizeof(struct llist));\n\t \tstruct llist_item *new_item, *old_item, *prev;\n\t \n\t-\tllist_init(&ret);\n\t+\tllist_init(ret);\n\t \n\t \tif ((ret->size = list->size) == 0)\n\t \t\treturn ret;\n\t@@ -420,6 +420,7 @@ static void minimize(struct pack_list **min)\n\t \n\t \tunique_pack_objects = llist_copy(all_objects);\n\t \tllist_sorted_difference_inplace(unique_pack_objects, missing);\n\t+\tfree(missing);\n\t \n\t \t/* remove unique pack objects from the non_unique packs */\n\t \tpl = non_unique;\n\t@@ -447,8 +448,9 @@ static void load_all_objects(void)\n\t {\n\t \tstruct pack_list *pl = local_packs;\n\t \tstruct llist_item *hint, *l;\n\t+\tall_objects = xmalloc(sizeof(struct llist));\n\t \n\t-\tllist_init(&all_objects);\n\t+\tllist_init(all_objects);\n\t \n\t \twhile (pl) {\n\t \t\thint = NULL;\n\t@@ -475,7 +477,8 @@ static void cmp_local_packs(void)\n\t \n\t \t/* only one packfile */\n\t \tif (!pl->next) {\n\t-\t\tllist_init(&pl->unique_objects);\n\t+\t\tpl->unique_objects = xmalloc(sizeof(struct llist));\n\t+\t\tllist_init(pl->unique_objects);\n\t \t\treturn;\n\t \t}\n\t \n\t@@ -512,7 +515,8 @@ static struct pack_list * add_pack(struct packed_git *p)\n\t \t\treturn NULL;\n\t \n\t \tl.pack = p;\n\t-\tllist_init(&l.remaining_objects);\n\t+\tl.remaining_objects = xmalloc(sizeof(struct llist));\n\t+\tllist_init(l.remaining_objects);\n\t \n\t \tif (open_pack_index(p))\n\t \t\treturn NULL;\n\t@@ -562,7 +566,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n\t \tint i;\n\t \tint i_still_use_this = 0;\n\t \tstruct pack_list *min = NULL, *red, *pl;\n\t-\tstruct llist *ignore;\n\t+\tstruct llist *ignore = xmalloc(sizeof(struct llist));\n\t \tstruct object_id *oid;\n\t \tchar buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \\n + \\0 */\n\t \n\t@@ -620,7 +624,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n\t \t\tscan_alt_odb_packs();\n\t \n\t \t/* ignore objects given on stdin */\n\t-\tllist_init(&ignore);\n\t+\tllist_init(ignore);\n\t \tif (!isatty(0)) {\n\t \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n\t \t\t\toid = xmalloc(sizeof(*oid));\n\t@@ -635,6 +639,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n\t \t\tllist_sorted_difference_inplace(pl->remaining_objects, ignore);\n\t \t\tpl = pl->next;\n\t \t}\n\t+\tfree(ignore);\n\t \n\t \tcmp_local_packs();\n\nNow, for:\n\n> @@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n>  \t\t\t\t\t    const char *path,\n>  \t\t\t\t\t    const struct object_id *treeish_name)\n>  {\n> -\tstruct fetch_task *task = xmalloc(sizeof(*task));\n> -\tmemset(task, 0, sizeof(*task));\n> +\tstruct fetch_task *task;\n> +\n> +\tCALLOC_ARRAY(task, 1);\n>  \n>  \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n\nThis is no worse than before, but I think we're just converting here\nfrom one bad pattern to another.\n\nThe \"struct fetch_task\" contains a \"struct strvec\", for many of our\nstructs we play it fast and loose with whether you can calloc() it, but\nfor \"struct strbuf\", \"struct strvec\" etc. we have an \"empty_strvec\" (or\n\"slopbuf\" etc.), so we *really* want those to be properly init'd.\n\nAFAICT we're just lucky that it happens to work with the strvec API in\nthis case, but if this was a strbuf it could easily segfault etc.\n\nSo the better fix here since we're spending review time on it isn't to\nmove from one memset() equivalent pattern to another, but actually to\nstart properly initializing this.\n\nFinally, this commit just seems to be all over the place in what it's\nchanging. We have a bunch of:\n\n\tx = xmalloc(...);\n\tmemset(x, 0, ...);\n\nIn our tree, it's not clear why these are being picked out in\nparticular, or what they have to do with each other.\n\nI think that if we're proposing to refactor these doing so with\ncoccinelle is a much better thing to do. There's a parallel thread about\nthat over at:\nhttps://lore.kernel.org/git/6694c52b38674859eb0390c7f62da1209a8d8ec3.1670266373.git.me@ttaylorr.com/\n"},{"id":"468636","messageId":"pull.1390.v6.git.git.1670356394394.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v5.git.git.1670348301601.gitgitgadget@gmail.com","subject":"[PATCH v6] revision: use calloc instead of malloc where possible","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-12-06T19:53:14Z","receivedAt":"2022-12-06T19:54:02Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling calloc\n\nSigned-off-by: Seija doremylover123@gmail.com\n---\n    revision: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAtariDreams%2Fcalloc-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AtariDreams/calloc-v6\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v5:\n\n 1:  8072fa30e4f ! 1:  e012cf5c158 revision: use calloc instead of malloc where possible\n     @@ Metadata\n       ## Commit message ##\n          revision: use calloc instead of malloc where possible\n      \n     -    We can avoid having to call memset by calling calloc directly\n     +    We can avoid having to call memset by calling calloc\n      \n          Signed-off-by: Seija doremylover123@gmail.com\n      \n\n\n builtin/pack-redundant.c | 17 +++++------------\n remote.c                 |  4 ++--\n submodule.c              | 10 +++++-----\n 3 files changed, 12 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex ecd49ca268f..ce5be807cf0 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -59,19 +59,12 @@ static inline struct llist_item *llist_item_get(void)\n \treturn new_item;\n }\n \n-static inline void llist_init(struct llist **list)\n-{\n-\t*list = xmalloc(sizeof(struct llist));\n-\t(*list)->front = (*list)->back = NULL;\n-\t(*list)->size = 0;\n-}\n-\n static struct llist * llist_copy(struct llist *list)\n {\n \tstruct llist *ret;\n \tstruct llist_item *new_item, *old_item, *prev;\n \n-\tllist_init(&ret);\n+\tCALLOC_ARRAY(ret, 1);\n \n \tif ((ret->size = list->size) == 0)\n \t\treturn ret;\n@@ -448,7 +441,7 @@ static void load_all_objects(void)\n \tstruct pack_list *pl = local_packs;\n \tstruct llist_item *hint, *l;\n \n-\tllist_init(&all_objects);\n+\tCALLOC_ARRAY(all_objects, 1);\n \n \twhile (pl) {\n \t\thint = NULL;\n@@ -475,7 +468,7 @@ static void cmp_local_packs(void)\n \n \t/* only one packfile */\n \tif (!pl->next) {\n-\t\tllist_init(&pl->unique_objects);\n+\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n \t\treturn;\n \t}\n \n@@ -512,7 +505,7 @@ static struct pack_list * add_pack(struct packed_git *p)\n \t\treturn NULL;\n \n \tl.pack = p;\n-\tllist_init(&l.remaining_objects);\n+\tCALLOC_ARRAY(l.remaining_objects, 1);\n \n \tif (open_pack_index(p))\n \t\treturn NULL;\n@@ -620,7 +613,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n \t\tscan_alt_odb_packs();\n \n \t/* ignore objects given on stdin */\n-\tllist_init(&ignore);\n+\tCALLOC_ARRAY(ignore, 1);\n \tif (!isatty(0)) {\n \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n \t\t\toid = xmalloc(sizeof(*oid));\ndiff --git a/remote.c b/remote.c\nindex 60869beebe7..475a1d18af0 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2741,9 +2741,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 8ac2fca855d..015102a83d6 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1458,14 +1458,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1504,8 +1503,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n \n\nbase-commit: 2e71cbbddd64695d43383c25c7a054ac4ff86882\n-- \ngitgitgadget\n"},{"id":"468700","messageId":"Y5BScRmR1fY/SQCq@debian.me","threadId":"58894","inReplyTo":"pull.1390.v6.git.git.1670356394394.gitgitgadget@gmail.com","subject":"Re: [PATCH v6] revision: use calloc instead of malloc where possible","fromName":"Bagas Sanjaya","fromEmail":"bagasdotme@gmail.com","sentAt":"2022-12-07T08:44:33Z","receivedAt":"2022-12-07T08:46:05Z","isPatch":true,"sender":{"key":"bagasdotme@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40219486?v=4"},"body":"On Tue, Dec 06, 2022 at 07:53:14PM +0000, Rose via GitGitGadget wrote:\n> From: Seija <doremylover123@gmail.com>\n> \n> We can avoid having to call memset by calling calloc\n\nWho are \"we\"?\n\nPlease avoid using first-person pronouns (I and we), since these are\nambiguous in context of many entities (individuals/companies)\nparticipating in development. Instead, write in imperative mood and\npassive voice, e.g. \"Avoid the need to call memset by allocating with\ncalloc() instead of malloc().\".\n\nEven then, what are justifications of malloc() -> calloc() conversion\nother than the described above?\n\n> \n> Signed-off-by: Seija doremylover123@gmail.com\n\nThe SoB doesn't look OK. It should have been\n\"Signed-off-by: Your Real Name <yourname@what.domain>\".\n\nThanks.\n\n-- \nAn old man doll... just what I always wanted! - Clara\n"},{"id":"509249","messageId":"pull.1390.v7.git.git.1734481847156.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v6.git.git.1670356394394.gitgitgadget@gmail.com","subject":"[PATCH v7] revision: use calloc instead of malloc where possible","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T00:30:46Z","receivedAt":"2024-12-18T00:30:50Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling calloc\n\nSigned-off-by: Seija <doremylover123@gmail.com>\n---\n    revision: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAreaZR%2Fcalloc-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AreaZR/calloc-v7\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v6:\n\n 1:  e012cf5c158 ! 1:  e91cea8dea8 revision: use calloc instead of malloc where possible\n     @@\n       ## Metadata ##\n     -Author: Seija <doremylover123@gmail.com>\n     +Author: Seija Kijin <doremylover123@gmail.com>\n      \n       ## Commit message ##\n          revision: use calloc instead of malloc where possible\n      \n          We can avoid having to call memset by calling calloc\n      \n     -    Signed-off-by: Seija doremylover123@gmail.com\n     +    Signed-off-by: Seija <doremylover123@gmail.com>\n      \n       ## builtin/pack-redundant.c ##\n      @@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     @@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n      -\t(*list)->size = 0;\n      -}\n      -\n     - static struct llist * llist_copy(struct llist *list)\n     + static void llist_free(struct llist *list)\n       {\n     + \tfor (struct llist_item *i = list->front, *next; i; i = next) {\n     +@@ builtin/pack-redundant.c: static struct llist * llist_copy(struct llist *list)\n       \tstruct llist *ret;\n       \tstruct llist_item *new_item, *old_item, *prev;\n       \n     @@ builtin/pack-redundant.c: static struct pack_list * add_pack(struct packed_git *\n       \n       \tif (open_pack_index(p))\n       \t\treturn NULL;\n     -@@ builtin/pack-redundant.c: int cmd_pack_redundant(int argc, const char **argv, const char *prefix)\n     +@@ builtin/pack-redundant.c: int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s\n       \t\tscan_alt_odb_packs();\n       \n       \t/* ignore objects given on stdin */\n      -\tllist_init(&ignore);\n      +\tCALLOC_ARRAY(ignore, 1);\n       \tif (!isatty(0)) {\n     + \t\tstruct object_id oid;\n       \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n     - \t\t\toid = xmalloc(sizeof(*oid));\n      \n       ## remote.c ##\n      @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n     @@ submodule.c: static struct fetch_task *fetch_task_create(struct submodule_parall\n      +\n      +\tCALLOC_ARRAY(task, 1);\n       \n     - \ttask->sub = submodule_from_path(spf->r, treeish_name, path);\n     - \n     + \tif (validate_submodule_path(path) < 0)\n     + \t\texit(128);\n\n\n builtin/pack-redundant.c | 17 +++++------------\n remote.c                 |  4 ++--\n submodule.c              | 10 +++++-----\n 3 files changed, 12 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/pack-redundant.c b/builtin/pack-redundant.c\nindex bc61990a933..7015a85bf78 100644\n--- a/builtin/pack-redundant.c\n+++ b/builtin/pack-redundant.c\n@@ -63,13 +63,6 @@ static inline struct llist_item *llist_item_get(void)\n \treturn new_item;\n }\n \n-static inline void llist_init(struct llist **list)\n-{\n-\t*list = xmalloc(sizeof(struct llist));\n-\t(*list)->front = (*list)->back = NULL;\n-\t(*list)->size = 0;\n-}\n-\n static void llist_free(struct llist *list)\n {\n \tfor (struct llist_item *i = list->front, *next; i; i = next) {\n@@ -84,7 +77,7 @@ static struct llist * llist_copy(struct llist *list)\n \tstruct llist *ret;\n \tstruct llist_item *new_item, *old_item, *prev;\n \n-\tllist_init(&ret);\n+\tCALLOC_ARRAY(ret, 1);\n \n \tif ((ret->size = list->size) == 0)\n \t\treturn ret;\n@@ -480,7 +473,7 @@ static void load_all_objects(void)\n \tstruct pack_list *pl = local_packs;\n \tstruct llist_item *hint, *l;\n \n-\tllist_init(&all_objects);\n+\tCALLOC_ARRAY(all_objects, 1);\n \n \twhile (pl) {\n \t\thint = NULL;\n@@ -507,7 +500,7 @@ static void cmp_local_packs(void)\n \n \t/* only one packfile */\n \tif (!pl->next) {\n-\t\tllist_init(&pl->unique_objects);\n+\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n \t\treturn;\n \t}\n \n@@ -544,7 +537,7 @@ static struct pack_list * add_pack(struct packed_git *p)\n \t\treturn NULL;\n \n \tl.pack = p;\n-\tllist_init(&l.remaining_objects);\n+\tCALLOC_ARRAY(l.remaining_objects, 1);\n \n \tif (open_pack_index(p))\n \t\treturn NULL;\n@@ -650,7 +643,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s\n \t\tscan_alt_odb_packs();\n \n \t/* ignore objects given on stdin */\n-\tllist_init(&ignore);\n+\tCALLOC_ARRAY(ignore, 1);\n \tif (!isatty(0)) {\n \t\tstruct object_id oid;\n \t\twhile (fgets(buf, sizeof(buf), stdin)) {\ndiff --git a/remote.c b/remote.c\nindex 10104d11e3c..462ff105273 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2854,9 +2854,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 7ec564854d0..7707c6f48f0 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1489,14 +1489,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1536,8 +1535,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \tif (validate_submodule_path(path) < 0)\n \t\texit(128);\n\nbase-commit: 2ccc89b0c16c51561da90d21cfbb4b58cc877bf6\n-- \ngitgitgadget\n"},{"id":"509254","messageId":"pull.1390.v8.git.git.1734482895877.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v7.git.git.1734481847156.gitgitgadget@gmail.com","subject":"[PATCH v8] git: use calloc instead of malloc where possible","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T00:48:15Z","receivedAt":"2024-12-18T00:48:19Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nWe can avoid having to call memset by calling calloc.\n\nSigned-off-by: Seija <doremylover123@gmail.com>\n---\n    git: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAreaZR%2Fcalloc-v8\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AreaZR/calloc-v8\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v7:\n\n 1:  e91cea8dea8 ! 1:  b78f068b3a3 revision: use calloc instead of malloc where possible\n     @@ Metadata\n      Author: Seija Kijin <doremylover123@gmail.com>\n      \n       ## Commit message ##\n     -    revision: use calloc instead of malloc where possible\n     +    git: use calloc instead of malloc where possible\n      \n     -    We can avoid having to call memset by calling calloc\n     +    We can avoid having to call memset by calling calloc.\n      \n          Signed-off-by: Seija <doremylover123@gmail.com>\n      \n     - ## builtin/pack-redundant.c ##\n     -@@ builtin/pack-redundant.c: static inline struct llist_item *llist_item_get(void)\n     - \treturn new_item;\n     - }\n     - \n     --static inline void llist_init(struct llist **list)\n     --{\n     --\t*list = xmalloc(sizeof(struct llist));\n     --\t(*list)->front = (*list)->back = NULL;\n     --\t(*list)->size = 0;\n     --}\n     --\n     - static void llist_free(struct llist *list)\n     - {\n     - \tfor (struct llist_item *i = list->front, *next; i; i = next) {\n     -@@ builtin/pack-redundant.c: static struct llist * llist_copy(struct llist *list)\n     - \tstruct llist *ret;\n     - \tstruct llist_item *new_item, *old_item, *prev;\n     - \n     --\tllist_init(&ret);\n     -+\tCALLOC_ARRAY(ret, 1);\n     - \n     - \tif ((ret->size = list->size) == 0)\n     - \t\treturn ret;\n     -@@ builtin/pack-redundant.c: static void load_all_objects(void)\n     - \tstruct pack_list *pl = local_packs;\n     - \tstruct llist_item *hint, *l;\n     - \n     --\tllist_init(&all_objects);\n     -+\tCALLOC_ARRAY(all_objects, 1);\n     - \n     - \twhile (pl) {\n     - \t\thint = NULL;\n     -@@ builtin/pack-redundant.c: static void cmp_local_packs(void)\n     - \n     - \t/* only one packfile */\n     - \tif (!pl->next) {\n     --\t\tllist_init(&pl->unique_objects);\n     -+\t\tCALLOC_ARRAY(pl->unique_objects, 1);\n     - \t\treturn;\n     - \t}\n     - \n     -@@ builtin/pack-redundant.c: static struct pack_list * add_pack(struct packed_git *p)\n     - \t\treturn NULL;\n     - \n     - \tl.pack = p;\n     --\tllist_init(&l.remaining_objects);\n     -+\tCALLOC_ARRAY(l.remaining_objects, 1);\n     - \n     - \tif (open_pack_index(p))\n     - \t\treturn NULL;\n     -@@ builtin/pack-redundant.c: int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s\n     - \t\tscan_alt_odb_packs();\n     - \n     - \t/* ignore objects given on stdin */\n     --\tllist_init(&ignore);\n     -+\tCALLOC_ARRAY(ignore, 1);\n     - \tif (!isatty(0)) {\n     - \t\tstruct object_id oid;\n     - \t\twhile (fgets(buf, sizeof(buf), stdin)) {\n     -\n       ## remote.c ##\n      @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n       \n\n\n remote.c    |  4 ++--\n submodule.c | 10 +++++-----\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 10104d11e3c..462ff105273 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2854,9 +2854,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 7ec564854d0..7707c6f48f0 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1489,14 +1489,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1536,8 +1535,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \tif (validate_submodule_path(path) < 0)\n \t\texit(128);\n\nbase-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086\n-- \ngitgitgadget\n"},{"id":"509258","messageId":"pull.1390.v9.git.git.1734483511642.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v8.git.git.1734482895877.gitgitgadget@gmail.com","subject":"[PATCH v9] git: use calloc instead of malloc where possible","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T00:58:31Z","receivedAt":"2024-12-18T00:58:35Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nAvoid having to calling malloc+memset by calling calloc.\n\nSigned-off-by: Seija <doremylover123@gmail.com>\n---\n    git: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAreaZR%2Fcalloc-v9\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AreaZR/calloc-v9\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v8:\n\n 1:  b78f068b3a3 ! 1:  35994dfb8af git: use calloc instead of malloc where possible\n     @@ Metadata\n       ## Commit message ##\n          git: use calloc instead of malloc where possible\n      \n     -    We can avoid having to call memset by calling calloc.\n     +    Avoid having to calling malloc+memset by calling calloc.\n      \n          Signed-off-by: Seija <doremylover123@gmail.com>\n      \n\n\n remote.c    |  4 ++--\n submodule.c | 10 +++++-----\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 10104d11e3c..462ff105273 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2854,9 +2854,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 7ec564854d0..7707c6f48f0 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1489,14 +1489,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1536,8 +1535,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \tif (validate_submodule_path(path) < 0)\n \t\texit(128);\n\nbase-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086\n-- \ngitgitgadget\n"},{"id":"509262","messageId":"pull.1390.v10.git.git.1734485187081.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v9.git.git.1734483511642.gitgitgadget@gmail.com","subject":"[PATCH v10] git: use calloc instead of malloc + memset where possible","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T01:26:26Z","receivedAt":"2024-12-18T01:26:30Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nAvoid calling malloc + memset by calling calloc.\n\nSigned-off-by: Seija <doremylover123@gmail.com>\n---\n    git: use calloc instead of malloc where possible\n    \n    We can avoid having to call memset by calling calloc\n    \n    Signed-off-by: Seija doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAreaZR%2Fcalloc-v10\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AreaZR/calloc-v10\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v9:\n\n 1:  35994dfb8af ! 1:  d7b7959be0a git: use calloc instead of malloc where possible\n     @@ Metadata\n      Author: Seija Kijin <doremylover123@gmail.com>\n      \n       ## Commit message ##\n     -    git: use calloc instead of malloc where possible\n     +    git: use calloc instead of malloc + memset where possible\n      \n     -    Avoid having to calling malloc+memset by calling calloc.\n     +    Avoid calling malloc + memset by calling calloc.\n      \n          Signed-off-by: Seija <doremylover123@gmail.com>\n      \n\n\n remote.c    |  4 ++--\n submodule.c | 10 +++++-----\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 10104d11e3c..462ff105273 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2854,9 +2854,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 7ec564854d0..7707c6f48f0 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1489,14 +1489,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1536,8 +1535,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \tif (validate_submodule_path(path) < 0)\n \t\texit(128);\n\nbase-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086\n-- \ngitgitgadget\n"},{"id":"509303","messageId":"xmqqr065t2yp.fsf@gitster.g","threadId":"58894","inReplyTo":"pull.1390.v10.git.git.1734485187081.gitgitgadget@gmail.com","subject":"Re: [PATCH v10] git: use calloc instead of malloc + memset where possible","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-12-18T15:39:42Z","receivedAt":"2024-12-18T15:39:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"AreaZR via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Seija Kijin <doremylover123@gmail.com>\n>\n> Avoid calling malloc + memset by calling calloc.\n>\n> Signed-off-by: Seija <doremylover123@gmail.com>\n\nName mismatch.\n\n>  remote.c    |  4 ++--\n>  submodule.c | 10 +++++-----\n>  2 files changed, 7 insertions(+), 7 deletions(-)\n\nI somehow hoped to see that this shrinks the line count, as you are\nessentially replacing two calls (malloc + memset) with one, but that\nis not what is happening, which is a bit disapointing.\n\nBut this is a strict improvement, I guess.  The reader needs to be\nfamiliar with the CALLOC_ARRAY() macro to understand it, which may\nmake the code less newbie-friendly (as opposed to malloc+memset any\nreasonably competent C programmer would understand without any prior\nknowledge of the conventions used by this project).\n\n"},{"id":"509308","messageId":"pull.1390.v11.git.git.1734540512582.gitgitgadget@gmail.com","threadId":"58894","inReplyTo":"pull.1390.v10.git.git.1734485187081.gitgitgadget@gmail.com","subject":"[PATCH v11] git: use calloc instead of malloc + memset where possible","fromName":"AreaZR via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2024-12-18T16:48:32Z","receivedAt":"2024-12-18T16:48:42Z","isPatch":true,"sender":{"key":"name:AreaZR","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nAvoid calling malloc + memset by calling calloc.\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    git: use calloc instead of malloc + memset where possible\n    \n    Avoid calling malloc + memset by calling calloc. cc: Jeff Hostetler\n    git@jeffhostetler.com cc: Ævar Arnfjörð Bjarmason avarab@gmail.com cc:\n    Bagas Sanjaya bagasdotme@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1390%2FAreaZR%2Fcalloc-v11\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1390/AreaZR/calloc-v11\nPull-Request: https://github.com/git/git/pull/1390\n\nRange-diff vs v10:\n\n 1:  d7b7959be0a ! 1:  cd18ee951cc git: use calloc instead of malloc + memset where possible\n     @@ Commit message\n      \n          Avoid calling malloc + memset by calling calloc.\n      \n     -    Signed-off-by: Seija <doremylover123@gmail.com>\n     +    Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n      \n       ## remote.c ##\n      @@ remote.c: void apply_push_cas(struct push_cas_option *cas,\n\n\n remote.c    |  4 ++--\n submodule.c | 10 +++++-----\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 10104d11e3c..462ff105273 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -2854,9 +2854,9 @@ void apply_push_cas(struct push_cas_option *cas,\n \n struct remote_state *remote_state_new(void)\n {\n-\tstruct remote_state *r = xmalloc(sizeof(*r));\n+\tstruct remote_state *r;\n \n-\tmemset(r, 0, sizeof(*r));\n+\tCALLOC_ARRAY(r, 1);\n \n \thashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);\n \thashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);\ndiff --git a/submodule.c b/submodule.c\nindex 7ec564854d0..7707c6f48f0 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1489,14 +1489,13 @@ struct fetch_task {\n  */\n static const struct submodule *get_non_gitmodules_submodule(const char *path)\n {\n-\tstruct submodule *ret = NULL;\n+\tstruct submodule *ret;\n \tconst char *name = default_name_or_path(path);\n \n \tif (!name)\n \t\treturn NULL;\n \n-\tret = xmalloc(sizeof(*ret));\n-\tmemset(ret, 0, sizeof(*ret));\n+\tCALLOC_ARRAY(ret, 1);\n \tret->path = name;\n \tret->name = name;\n \n@@ -1536,8 +1535,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf\n \t\t\t\t\t    const char *path,\n \t\t\t\t\t    const struct object_id *treeish_name)\n {\n-\tstruct fetch_task *task = xmalloc(sizeof(*task));\n-\tmemset(task, 0, sizeof(*task));\n+\tstruct fetch_task *task;\n+\n+\tCALLOC_ARRAY(task, 1);\n \n \tif (validate_submodule_path(path) < 0)\n \t\texit(128);\n\nbase-commit: d882f382b3d939d90cfa58d17b17802338f05d66\n-- \ngitgitgadget\n"}]}