{"thread":{"id":"37168","subject":"[PATCH] use xcalloc() to allocate zero-initialized memory","startedAt":"2014-07-19T13:56:26Z","lastAt":"2014-07-19T13:56:26Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"246369","messageId":"53CA790A.20402@web.de","threadId":"37168","inReplyTo":null,"subject":"[PATCH] use xcalloc() to allocate zero-initialized memory","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-07-19T13:56:26Z","receivedAt":"2014-07-19T13:56:26Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Use xcalloc() instead of xmalloc() followed by memset() to allocate\nand zero out memory because it's shorter and avoids duplicating the\nfunction parameters.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/clean.c      | 3 +--\n builtin/index-pack.c | 3 +--\n compat/mingw.c       | 3 +--\n pathspec.c           | 3 +--\n 4 files changed, 4 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 27701d2..1032563 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -621,8 +621,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)\n \t\t\t\tnr += chosen[i];\n \t\t}\n \n-\t\tresult = xmalloc(sizeof(int) * (nr + 1));\n-\t\tmemset(result, 0, sizeof(int) * (nr + 1));\n+\t\tresult = xcalloc(nr + 1, sizeof(int));\n \t\tfor (i = 0; i < stuff->nr && j < nr; i++) {\n \t\t\tif (chosen[i])\n \t\t\t\tresult[j++] = i;\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex fc40411..5568a5b 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -362,8 +362,7 @@ static void set_thread_data(struct thread_local *data)\n \n static struct base_data *alloc_base_data(void)\n {\n-\tstruct base_data *base = xmalloc(sizeof(struct base_data));\n-\tmemset(base, 0, sizeof(*base));\n+\tstruct base_data *base = xcalloc(1, sizeof(struct base_data));\n \tbase->ref_last = -1;\n \tbase->ofs_last = -1;\n \treturn base;\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex 3baaa4d..ed4c61c 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -1230,8 +1230,7 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,\n \telse\n \t\tai->ai_canonname = NULL;\n \n-\tsin = xmalloc(ai->ai_addrlen);\n-\tmemset(sin, 0, ai->ai_addrlen);\n+\tsin = xcalloc(1, ai->ai_addrlen);\n \tsin->sin_family = AF_INET;\n \t/* Note: getaddrinfo is supposed to allow service to be a string,\n \t * which should be looked up using getservbyname. This is\ndiff --git a/pathspec.c b/pathspec.c\nindex 89f2c8f..9304ee3 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -389,8 +389,7 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\tif (!(flags & PATHSPEC_PREFER_CWD))\n \t\t\tdie(\"BUG: PATHSPEC_PREFER_CWD requires arguments\");\n \n-\t\tpathspec->items = item = xmalloc(sizeof(*item));\n-\t\tmemset(item, 0, sizeof(*item));\n+\t\tpathspec->items = item = xcalloc(1, sizeof(*item));\n \t\titem->match = prefix;\n \t\titem->original = prefix;\n \t\titem->nowildcard_len = item->len = strlen(prefix);\n-- \n2.0.2\n"}]}