{"thread":{"id":"9896","subject":"xmemdup patches","startedAt":"2007-09-15T21:53:05Z","lastAt":"2007-09-19T08:26:11Z","messageCount":8,"participants":["Pierre Habouzit","Johannes Schindelin","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"53326","messageId":"20070917161141.CC8AA344A49@madism.org","threadId":"9896","inReplyTo":"20070917161113.GB460@artemis.corp","subject":"[PATCH 1/2] Add xmemdup that duplicates a block of memory, and NUL terminates it.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-15T21:53:05Z","receivedAt":"2007-09-15T21:53:05Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"Implement xstrndup using it.\n\nSigned-off-by: Pierre Habouzit <madcoder@debian.org>\n---\n git-compat-util.h |   17 +++++++++--------\n 1 files changed, 9 insertions(+), 8 deletions(-)\n\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 1bfbdeb..429b0a3 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -211,19 +211,20 @@ static inline void *xmalloc(size_t size)\n \treturn ret;\n }\n \n-static inline char *xstrndup(const char *str, size_t len)\n+static inline void *xmemdup(const void *data, size_t len)\n {\n-\tchar *p;\n-\n-\tp = memchr(str, '\\0', len);\n-\tif (p)\n-\t\tlen = p - str;\n-\tp = xmalloc(len + 1);\n-\tmemcpy(p, str, len);\n+\tchar *p = xmalloc(len + 1);\n+\tmemcpy(p, data, len);\n \tp[len] = '\\0';\n \treturn p;\n }\n \n+static inline char *xstrndup(const char *str, size_t len)\n+{\n+\tchar *p = memchr(str, '\\0', len);\n+\treturn xmemdup(str, p ? p - str : len);\n+}\n+\n static inline void *xrealloc(void *ptr, size_t size)\n {\n \tvoid *ret = realloc(ptr, size);\n-- \n1.5.3.1\n"},{"id":"53327","messageId":"20070917161142.D3C9A344A49@madism.org","threadId":"9896","inReplyTo":"20070917161113.GB460@artemis.corp","subject":"[PATCH 2/2] Use xmemdup in many places.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-15T22:32:36Z","receivedAt":"2007-09-15T22:32:36Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"Signed-off-by: Pierre Habouzit <madcoder@debian.org>\n---\n attr.c                  |    7 +------\n builtin-add.c           |    8 ++------\n builtin-apply.c         |   11 ++---------\n builtin-fetch--tool.c   |    6 +-----\n builtin-fmt-merge-msg.c |   17 ++++++-----------\n builtin-for-each-ref.c  |   40 +++++++++-------------------------------\n builtin-log.c           |   12 ++----------\n builtin-ls-files.c      |    9 +--------\n builtin-mv.c            |    5 +----\n builtin-revert.c        |    4 +---\n builtin-shortlog.c      |   11 ++---------\n commit.c                |   16 ++++++----------\n connect.c               |    4 +---\n convert.c               |    7 +------\n diff.c                  |   13 ++-----------\n diffcore-order.c        |    7 ++-----\n fast-import.c           |    4 +---\n http-push.c             |    9 ++-------\n imap-send.c             |   20 +++++---------------\n merge-recursive.c       |   19 ++++---------------\n refs.c                  |   12 ++++--------\n sha1_file.c             |   12 +++---------\n tag.c                   |    4 +---\n 23 files changed, 60 insertions(+), 197 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 1293993..fbfc2eb 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -160,12 +160,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,\n \t\telse if (!equals)\n \t\t\te->setto = ATTR__TRUE;\n \t\telse {\n-\t\t\tchar *value;\n-\t\t\tint vallen = ep - equals;\n-\t\t\tvalue = xmalloc(vallen);\n-\t\t\tmemcpy(value, equals+1, vallen-1);\n-\t\t\tvalue[vallen-1] = 0;\n-\t\t\te->setto = value;\n+\t\t\te->setto = xmemdup(equals + 1, ep - equals - 1);\n \t\t}\n \t\te->attr = git_attr(cp, len);\n \t}\ndiff --git a/builtin-add.c b/builtin-add.c\nindex 0d7d0ce..55eedd8 100644\n--- a/builtin-add.c\n+++ b/builtin-add.c\n@@ -71,12 +71,8 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec,\n \tbaselen = common_prefix(pathspec);\n \tpath = \".\";\n \tbase = \"\";\n-\tif (baselen) {\n-\t\tchar *common = xmalloc(baselen + 1);\n-\t\tmemcpy(common, *pathspec, baselen);\n-\t\tcommon[baselen] = 0;\n-\t\tpath = base = common;\n-\t}\n+\tif (baselen)\n+\t\tpath = base = xmemdup(*pathspec, baselen);\n \n \t/* Read the directory and prune it */\n \tread_directory(dir, path, base, baselen, pathspec);\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 05011bb..900d0a7 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -293,11 +293,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)\n \t\t\treturn def;\n \t}\n \n-\tname = xmalloc(len + 1);\n-\tmemcpy(name, start, len);\n-\tname[len] = 0;\n-\tfree(def);\n-\treturn name;\n+\treturn xmemdup(start, len);\n }\n \n static int count_slashes(const char *cp)\n@@ -687,10 +683,7 @@ static char *git_header_name(char *line, int llen)\n \t\t\t\t\tbreak;\n \t\t\t}\n \t\t\tif (second[len] == '\\n' && !memcmp(name, second, len)) {\n-\t\t\t\tchar *ret = xmalloc(len + 1);\n-\t\t\t\tmemcpy(ret, name, len);\n-\t\t\t\tret[len] = 0;\n-\t\t\t\treturn ret;\n+\t\t\t\treturn xmemdup(name, len);\n \t\t\t}\n \t\t}\n \t}\ndiff --git a/builtin-fetch--tool.c b/builtin-fetch--tool.c\nindex 514a3cc..327f555 100644\n--- a/builtin-fetch--tool.c\n+++ b/builtin-fetch--tool.c\n@@ -222,19 +222,15 @@ static char *find_local_name(const char *remote_name, const char *refs,\n \t\t}\n \t\tif (!strncmp(remote_name, ref, len) && ref[len] == ':') {\n \t\t\tconst char *local_part = ref + len + 1;\n-\t\t\tchar *ret;\n \t\t\tint retlen;\n \n \t\t\tif (!next)\n \t\t\t\tretlen = strlen(local_part);\n \t\t\telse\n \t\t\t\tretlen = next - local_part;\n-\t\t\tret = xmalloc(retlen + 1);\n-\t\t\tmemcpy(ret, local_part, retlen);\n-\t\t\tret[retlen] = 0;\n \t\t\t*force_p = single_force;\n \t\t\t*not_for_merge_p = not_for_merge;\n-\t\t\treturn ret;\n+\t\t\treturn xmemdup(local_part, retlen);\n \t\t}\n \t\tref = next;\n \t}\ndiff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c\nindex ae60fcc..6dd217a 100644\n--- a/builtin-fmt-merge-msg.c\n+++ b/builtin-fmt-merge-msg.c\n@@ -140,12 +140,10 @@ static int handle_line(char *line)\n \tif (!strcmp(\".\", src) || !strcmp(src, origin)) {\n \t\tint len = strlen(origin);\n \t\tif (origin[0] == '\\'' && origin[len - 1] == '\\'') {\n-\t\t\tchar *new_origin = xmalloc(len - 1);\n-\t\t\tmemcpy(new_origin, origin + 1, len - 2);\n-\t\t\tnew_origin[len - 2] = 0;\n-\t\t\torigin = new_origin;\n-\t\t} else\n+\t\t\torigin = xmemdup(origin + 1, len - 2);\n+\t\t} else {\n \t\t\torigin = xstrdup(origin);\n+\t\t}\n \t} else {\n \t\tchar *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);\n \t\tsprintf(new_origin, \"%s of %s\", origin, src);\n@@ -211,14 +209,11 @@ static void shortlog(const char *name, unsigned char *sha1,\n \n \t\tbol += 2;\n \t\teol = strchr(bol, '\\n');\n-\n \t\tif (eol) {\n-\t\t\tint len = eol - bol;\n-\t\t\toneline = xmalloc(len + 1);\n-\t\t\tmemcpy(oneline, bol, len);\n-\t\t\toneline[len] = 0;\n-\t\t} else\n+\t\t\toneline = xmemdup(bol, eol - bol);\n+\t\t} else {\n \t\t\toneline = xstrdup(bol);\n+\t\t}\n \t\tappend_to_list(&subjects, oneline, NULL);\n \t}\n \ndiff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c\nindex 0afa1c5..287d52a 100644\n--- a/builtin-for-each-ref.c\n+++ b/builtin-for-each-ref.c\n@@ -87,7 +87,6 @@ static int used_atom_cnt, sort_atom_limit, need_tagged;\n static int parse_atom(const char *atom, const char *ep)\n {\n \tconst char *sp;\n-\tchar *n;\n \tint i, at;\n \n \tsp = atom;\n@@ -120,10 +119,7 @@ static int parse_atom(const char *atom, const char *ep)\n \t\t\t     (sizeof *used_atom) * used_atom_cnt);\n \tused_atom_type = xrealloc(used_atom_type,\n \t\t\t\t  (sizeof(*used_atom_type) * used_atom_cnt));\n-\tn = xmalloc(ep - atom + 1);\n-\tmemcpy(n, atom, ep - atom);\n-\tn[ep-atom] = 0;\n-\tused_atom[at] = n;\n+\tused_atom[at] = xmemdup(atom, ep - atom);\n \tused_atom_type[at] = valid_atom[i].cmp_type;\n \treturn at;\n }\n@@ -305,46 +301,28 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un\n static const char *copy_line(const char *buf)\n {\n \tconst char *eol = strchr(buf, '\\n');\n-\tchar *line;\n-\tint len;\n \tif (!eol)\n \t\treturn \"\";\n-\tlen = eol - buf;\n-\tline = xmalloc(len + 1);\n-\tmemcpy(line, buf, len);\n-\tline[len] = 0;\n-\treturn line;\n+\treturn xmemdup(buf, eol - buf);\n }\n \n static const char *copy_name(const char *buf)\n {\n-\tconst char *eol = strchr(buf, '\\n');\n-\tconst char *eoname = strstr(buf, \" <\");\n-\tchar *line;\n-\tint len;\n-\tif (!(eoname && eol && eoname < eol))\n-\t\treturn \"\";\n-\tlen = eoname - buf;\n-\tline = xmalloc(len + 1);\n-\tmemcpy(line, buf, len);\n-\tline[len] = 0;\n-\treturn line;\n+\tconst char *cp;\n+\tfor (cp = buf; *cp != '\\n'; cp++) {\n+\t\tif (!strncmp(cp, \" <\", 2))\n+\t\t\treturn xmemdup(buf, cp - buf);\n+\t}\n+\treturn \"\";\n }\n \n static const char *copy_email(const char *buf)\n {\n \tconst char *email = strchr(buf, '<');\n \tconst char *eoemail = strchr(email, '>');\n-\tchar *line;\n-\tint len;\n \tif (!email || !eoemail)\n \t\treturn \"\";\n-\teoemail++;\n-\tlen = eoemail - email;\n-\tline = xmalloc(len + 1);\n-\tmemcpy(line, email, len);\n-\tline[len] = 0;\n-\treturn line;\n+\treturn xmemdup(email, eoemail + 1 - email);\n }\n \n static void grab_date(const char *buf, struct atom_value *v)\ndiff --git a/builtin-log.c b/builtin-log.c\nindex 60819d1..f61a568 100644\n--- a/builtin-log.c\n+++ b/builtin-log.c\n@@ -441,8 +441,6 @@ static const char *clean_message_id(const char *msg_id)\n {\n \tchar ch;\n \tconst char *a, *z, *m;\n-\tchar *n;\n-\tsize_t len;\n \n \tm = msg_id;\n \twhile ((ch = *m) && (isspace(ch) || (ch == '<')))\n@@ -458,11 +456,7 @@ static const char *clean_message_id(const char *msg_id)\n \t\tdie(\"insane in-reply-to: %s\", msg_id);\n \tif (++z == m)\n \t\treturn a;\n-\tlen = z - a;\n-\tn = xmalloc(len + 1);\n-\tmemcpy(n, a, len);\n-\tn[len] = 0;\n-\treturn n;\n+\treturn xmemdup(a, z - a);\n }\n \n int cmd_format_patch(int argc, const char **argv, const char *prefix)\n@@ -541,9 +535,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)\n \t\t\tendpos = strchr(committer, '>');\n \t\t\tif (!endpos)\n \t\t\t\tdie(\"bogos committer info %s\\n\", committer);\n-\t\t\tadd_signoff = xmalloc(endpos - committer + 2);\n-\t\t\tmemcpy(add_signoff, committer, endpos - committer + 1);\n-\t\t\tadd_signoff[endpos - committer + 1] = 0;\n+\t\t\tadd_signoff = xmemdup(committer, endpos - committer + 1);\n \t\t}\n \t\telse if (!strcmp(argv[i], \"--attach\")) {\n \t\t\trev.mime_boundary = git_version_string;\ndiff --git a/builtin-ls-files.c b/builtin-ls-files.c\nindex 6c1db86..64f0011 100644\n--- a/builtin-ls-files.c\n+++ b/builtin-ls-files.c\n@@ -299,7 +299,6 @@ static void prune_cache(const char *prefix)\n static const char *verify_pathspec(const char *prefix)\n {\n \tconst char **p, *n, *prev;\n-\tchar *real_prefix;\n \tunsigned long max;\n \n \tprev = NULL;\n@@ -326,14 +325,8 @@ static const char *verify_pathspec(const char *prefix)\n \tif (prefix_offset > max || memcmp(prev, prefix, prefix_offset))\n \t\tdie(\"git-ls-files: cannot generate relative filenames containing '..'\");\n \n-\treal_prefix = NULL;\n \tprefix_len = max;\n-\tif (max) {\n-\t\treal_prefix = xmalloc(max + 1);\n-\t\tmemcpy(real_prefix, prev, max);\n-\t\treal_prefix[max] = 0;\n-\t}\n-\treturn real_prefix;\n+\treturn max ? xmemdup(prev, max) : NULL;\n }\n \n /*\ndiff --git a/builtin-mv.c b/builtin-mv.c\nindex b95b7d2..9dacc95 100644\n--- a/builtin-mv.c\n+++ b/builtin-mv.c\n@@ -22,10 +22,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,\n \tfor (i = 0; i < count; i++) {\n \t\tint length = strlen(result[i]);\n \t\tif (length > 0 && result[i][length - 1] == '/') {\n-\t\t\tchar *without_slash = xmalloc(length);\n-\t\t\tmemcpy(without_slash, result[i], length - 1);\n-\t\t\twithout_slash[length - 1] = '\\0';\n-\t\t\tresult[i] = without_slash;\n+\t\t\tresult[i] = xmemdup(result[i], length - 1);\n \t\t}\n \t\tif (base_name) {\n \t\t\tconst char *last_slash = strrchr(result[i], '/');\ndiff --git a/builtin-revert.c b/builtin-revert.c\nindex 499bbe7..4d96d3f 100644\n--- a/builtin-revert.c\n+++ b/builtin-revert.c\n@@ -168,9 +168,7 @@ static void set_author_ident_env(const char *message)\n \t\t\tchar *line, *pend, *email, *timestamp;\n \n \t\t\tp += 7;\n-\t\t\tline = xmalloc(eol + 1 - p);\n-\t\t\tmemcpy(line, p, eol - p);\n-\t\t\tline[eol - p] = '\\0';\n+\t\t\tline = xmemdup(p, eol - p);\n \t\t\temail = strchr(line, '<');\n \t\t\tif (!email)\n \t\t\t\tdie (\"Could not extract author email from %s\",\ndiff --git a/builtin-shortlog.c b/builtin-shortlog.c\nindex 16af619..a6cbe39 100644\n--- a/builtin-shortlog.c\n+++ b/builtin-shortlog.c\n@@ -39,10 +39,7 @@ static void insert_author_oneline(struct path_list *list,\n \twhile (authorlen > 0 && isspace(author[authorlen - 1]))\n \t\tauthorlen--;\n \n-\tbuffer = xmalloc(authorlen + 1);\n-\tmemcpy(buffer, author, authorlen);\n-\tbuffer[authorlen] = '\\0';\n-\n+\tbuffer = xmemdup(author, authorlen);\n \titem = path_list_insert(buffer, list);\n \tif (item->util == NULL)\n \t\titem->util = xcalloc(1, sizeof(struct path_list));\n@@ -66,13 +63,9 @@ static void insert_author_oneline(struct path_list *list,\n \t\toneline++;\n \t\tonelinelen--;\n \t}\n-\n \twhile (onelinelen > 0 && isspace(oneline[onelinelen - 1]))\n \t\tonelinelen--;\n-\n-\tbuffer = xmalloc(onelinelen + 1);\n-\tmemcpy(buffer, oneline, onelinelen);\n-\tbuffer[onelinelen] = '\\0';\n+\tbuffer = xmemdup(oneline, onelinelen);\n \n \tif (dot3) {\n \t\tint dot3len = strlen(dot3);\ndiff --git a/commit.c b/commit.c\nindex 85889f9..2778621 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -628,11 +628,7 @@ static char *get_header(const struct commit *commit, const char *key)\n \t\tif (eol - line > key_len &&\n \t\t    !strncmp(line, key, key_len) &&\n \t\t    line[key_len] == ' ') {\n-\t\t\tint len = eol - line - key_len;\n-\t\t\tchar *ret = xmalloc(len);\n-\t\t\tmemcpy(ret, line + key_len + 1, len - 1);\n-\t\t\tret[len - 1] = '\\0';\n-\t\t\treturn ret;\n+\t\t\treturn xmemdup(line + key_len + 1, eol - line - key_len - 1);\n \t\t}\n \t\tline = next;\n \t}\n@@ -709,7 +705,7 @@ static void fill_person(struct interp *table, const char *msg, int len)\n \tstart = end + 1;\n \twhile (end > 0 && isspace(msg[end - 1]))\n \t\tend--;\n-\ttable[0].value = xstrndup(msg, end);\n+\ttable[0].value = xmemdup(msg, end);\n \n \tif (start >= len)\n \t\treturn;\n@@ -721,7 +717,7 @@ static void fill_person(struct interp *table, const char *msg, int len)\n \tif (end >= len)\n \t\treturn;\n \n-\ttable[1].value = xstrndup(msg + start, end - start);\n+\ttable[1].value = xmemdup(msg + start, end - start);\n \n \t/* parse date */\n \tfor (start = end + 1; start < len && isspace(msg[start]); start++)\n@@ -732,7 +728,7 @@ static void fill_person(struct interp *table, const char *msg, int len)\n \tif (msg + start == ep)\n \t\treturn;\n \n-\ttable[5].value = xstrndup(msg + start, ep - (msg + start));\n+\ttable[5].value = xmemdup(msg + start, ep - (msg + start));\n \n \t/* parse tz */\n \tfor (start = ep - msg + 1; start < len && isspace(msg[start]); start++)\n@@ -859,7 +855,7 @@ void format_commit_message(const struct commit *commit,\n \t\t\t; /* do nothing */\n \n \t\tif (state == SUBJECT) {\n-\t\t\ttable[ISUBJECT].value = xstrndup(msg + i, eol - i);\n+\t\t\ttable[ISUBJECT].value = xmemdup(msg + i, eol - i);\n \t\t\ti = eol;\n \t\t}\n \t\tif (i == eol) {\n@@ -875,7 +871,7 @@ void format_commit_message(const struct commit *commit,\n \t\t\t\t\tmsg + i + 10, eol - i - 10);\n \t\telse if (!prefixcmp(msg + i, \"encoding \"))\n \t\t\ttable[IENCODING].value =\n-\t\t\t\txstrndup(msg + i + 9, eol - i - 9);\n+\t\t\t\txmemdup(msg + i + 9, eol - i - 9);\n \t\ti = eol;\n \t}\n \tif (msg[i])\ndiff --git a/connect.c b/connect.c\nindex 8b1e993..779de85 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -393,9 +393,7 @@ static int git_proxy_command_options(const char *var, const char *value)\n \t\t\tif (matchlen == 4 &&\n \t\t\t    !memcmp(value, \"none\", 4))\n \t\t\t\tmatchlen = 0;\n-\t\t\tgit_proxy_command = xmalloc(matchlen + 1);\n-\t\t\tmemcpy(git_proxy_command, value, matchlen);\n-\t\t\tgit_proxy_command[matchlen] = 0;\n+\t\t\tgit_proxy_command = xmemdup(value, matchlen);\n \t\t}\n \t\treturn 0;\n \t}\ndiff --git a/convert.c b/convert.c\nindex 508d30b..3a0a9ed 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -323,13 +323,8 @@ static int read_convert_config(const char *var, const char *value)\n \t\tif (!strncmp(drv->name, name, namelen) && !drv->name[namelen])\n \t\t\tbreak;\n \tif (!drv) {\n-\t\tchar *namebuf;\n \t\tdrv = xcalloc(1, sizeof(struct convert_driver));\n-\t\tnamebuf = xmalloc(namelen + 1);\n-\t\tmemcpy(namebuf, name, namelen);\n-\t\tnamebuf[namelen] = 0;\n-\t\tdrv->name = namebuf;\n-\t\tdrv->next = NULL;\n+\t\tdrv->name = xmemdup(name, namelen);\n \t\t*user_convert_tail = drv;\n \t\tuser_convert_tail = &(drv->next);\n \t}\ndiff --git a/diff.c b/diff.c\nindex a5b69ed..f2e9a44 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -83,13 +83,8 @@ static int parse_lldiff_command(const char *var, const char *ep, const char *val\n \t\tif (!strncmp(drv->name, name, namelen) && !drv->name[namelen])\n \t\t\tbreak;\n \tif (!drv) {\n-\t\tchar *namebuf;\n \t\tdrv = xcalloc(1, sizeof(struct ll_diff_driver));\n-\t\tnamebuf = xmalloc(namelen + 1);\n-\t\tmemcpy(namebuf, name, namelen);\n-\t\tnamebuf[namelen] = 0;\n-\t\tdrv->name = namebuf;\n-\t\tdrv->next = NULL;\n+\t\tdrv->name = xmemdup(name, namelen);\n \t\tif (!user_diff_tail)\n \t\t\tuser_diff_tail = &user_diff;\n \t\t*user_diff_tail = drv;\n@@ -126,12 +121,8 @@ static int parse_funcname_pattern(const char *var, const char *ep, const char *v\n \t\tif (!strncmp(pp->name, name, namelen) && !pp->name[namelen])\n \t\t\tbreak;\n \tif (!pp) {\n-\t\tchar *namebuf;\n \t\tpp = xcalloc(1, sizeof(*pp));\n-\t\tnamebuf = xmalloc(namelen + 1);\n-\t\tmemcpy(namebuf, name, namelen);\n-\t\tnamebuf[namelen] = 0;\n-\t\tpp->name = namebuf;\n+\t\tpp->name = xmemdup(name, namelen);\n \t\tpp->next = funcname_pattern_list;\n \t\tfuncname_pattern_list = pp;\n \t}\ndiff --git a/diffcore-order.c b/diffcore-order.c\nindex 2a4bd82..e20ab98 100644\n--- a/diffcore-order.c\n+++ b/diffcore-order.c\n@@ -48,11 +48,8 @@ static void prepare_order(const char *orderfile)\n \t\t\t\tif (*ep == '\\n') {\n \t\t\t\t\t*ep = 0;\n \t\t\t\t\torder[cnt] = cp;\n-\t\t\t\t}\n-\t\t\t\telse {\n-\t\t\t\t\torder[cnt] = xmalloc(ep-cp+1);\n-\t\t\t\t\tmemcpy(order[cnt], cp, ep-cp);\n-\t\t\t\t\torder[cnt][ep-cp] = 0;\n+\t\t\t\t} else {\n+\t\t\t\t\torder[cnt] = xmemdup(cp, ep - cp);\n \t\t\t\t}\n \t\t\t\tcnt++;\n \t\t\t}\ndiff --git a/fast-import.c b/fast-import.c\nindex e2a4834..d5e4b1d 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1864,9 +1864,7 @@ static void file_change_cr(struct branch *b, int rename)\n \t\tendp = strchr(s, ' ');\n \t\tif (!endp)\n \t\t\tdie(\"Missing space after source: %s\", command_buf.buf);\n-\t\ts_uq = xmalloc(endp - s + 1);\n-\t\tmemcpy(s_uq, s, endp - s);\n-\t\ts_uq[endp - s] = 0;\n+\t\ts_uq = xmemdup(s, endp - s);\n \t}\n \ts = s_uq;\n \ndiff --git a/http-push.c b/http-push.c\nindex 7c3720f..f5e4266 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -1271,10 +1271,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)\n {\n \tstruct xml_ctx *ctx = (struct xml_ctx *)userData;\n \tfree(ctx->cdata);\n-\tctx->cdata = xmalloc(len + 1);\n-\t/* NB: 's' is not null-terminated, can not use strlcpy here */\n-\tmemcpy(ctx->cdata, s, len);\n-\tctx->cdata[len] = '\\0';\n+\tctx->cdata = xmemdup(s, len);\n }\n \n static struct remote_lock *lock_remote(const char *path, long timeout)\n@@ -2172,9 +2169,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)\n \n \t/* If it's a symref, set the refname; otherwise try for a sha1 */\n \tif (!prefixcmp((char *)buffer.buffer, \"ref: \")) {\n-\t\t*symref = xmalloc(buffer.posn - 5);\n-\t\tmemcpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 6);\n-\t\t(*symref)[buffer.posn - 6] = '\\0';\n+\t\t*symref = xmemdup(buffer.buffer + 5, buffer.posn - 6);\n \t} else {\n \t\tget_sha1_hex(buffer.buffer, sha1);\n \t}\ndiff --git a/imap-send.c b/imap-send.c\nindex 86e4a0f..b07decc 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -623,9 +623,7 @@ parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )\n \t\t\t\t\tgoto bail;\n \t\t\tcur->len = s - p;\n \t\t\ts++;\n-\t\t\tcur->val = xmalloc( cur->len + 1 );\n-\t\t\tmemcpy( cur->val, p, cur->len );\n-\t\t\tcur->val[cur->len] = 0;\n+\t\t\tcur->val = xmemdup(p, cur->len);\n \t\t} else {\n \t\t\t/* atom */\n \t\t\tp = s;\n@@ -633,12 +631,10 @@ parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )\n \t\t\t\tif (level && *s == ')')\n \t\t\t\t\tbreak;\n \t\t\tcur->len = s - p;\n-\t\t\tif (cur->len == 3 && !memcmp (\"NIL\", p, 3))\n+\t\t\tif (cur->len == 3 && !memcmp (\"NIL\", p, 3)) {\n \t\t\t\tcur->val = NIL;\n-\t\t\telse {\n-\t\t\t\tcur->val = xmalloc( cur->len + 1 );\n-\t\t\t\tmemcpy( cur->val, p, cur->len );\n-\t\t\t\tcur->val[cur->len] = 0;\n+\t\t\t} else {\n+\t\t\t\tcur->val = xmemdup(p, cur->len);\n \t\t\t}\n \t\t}\n \n@@ -1221,13 +1217,7 @@ split_msg( msg_data_t *all_msgs, msg_data_t *msg, int *ofs )\n \tif (p)\n \t\tmsg->len = &p[1] - data;\n \n-\tmsg->data = xmalloc( msg->len + 1 );\n-\tif (!msg->data)\n-\t\treturn 0;\n-\n-\tmemcpy( msg->data, data, msg->len );\n-\tmsg->data[ msg->len ] = 0;\n-\n+\tmsg->data = xmemdup(data, msg->len);\n \t*ofs += msg->len;\n \treturn 1;\n }\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 19d5f3b..e38011b 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -432,19 +432,15 @@ static int update_stages(const char *path, struct diff_filespec *o,\n \n static int remove_path(const char *name)\n {\n-\tint ret, len;\n+\tint ret;\n \tchar *slash, *dirs;\n \n \tret = unlink(name);\n \tif (ret)\n \t\treturn ret;\n-\tlen = strlen(name);\n-\tdirs = xmalloc(len+1);\n-\tmemcpy(dirs, name, len);\n-\tdirs[len] = '\\0';\n+\tdirs = xstrdup(name);\n \twhile ((slash = strrchr(name, '/'))) {\n \t\t*slash = '\\0';\n-\t\tlen = slash - name;\n \t\tif (rmdir(name) != 0)\n \t\t\tbreak;\n \t}\n@@ -578,9 +574,7 @@ static void update_file_flags(const unsigned char *sha,\n \t\t\tflush_buffer(fd, buf, size);\n \t\t\tclose(fd);\n \t\t} else if (S_ISLNK(mode)) {\n-\t\t\tchar *lnk = xmalloc(size + 1);\n-\t\t\tmemcpy(lnk, buf, size);\n-\t\t\tlnk[size] = '\\0';\n+\t\t\tchar *lnk = xmemdup(buf, size);\n \t\t\tmkdir_p(path, 0777);\n \t\t\tunlink(path);\n \t\t\tsymlink(lnk, path);\n@@ -872,14 +866,9 @@ static int read_merge_config(const char *var, const char *value)\n \t\tif (!strncmp(fn->name, name, namelen) && !fn->name[namelen])\n \t\t\tbreak;\n \tif (!fn) {\n-\t\tchar *namebuf;\n \t\tfn = xcalloc(1, sizeof(struct ll_merge_driver));\n-\t\tnamebuf = xmalloc(namelen + 1);\n-\t\tmemcpy(namebuf, name, namelen);\n-\t\tnamebuf[namelen] = 0;\n-\t\tfn->name = namebuf;\n+\t\tfn->name = xmemdup(name, namelen);\n \t\tfn->fn = ll_ext_merge;\n-\t\tfn->next = NULL;\n \t\t*ll_user_merge_tail = fn;\n \t\tll_user_merge_tail = &(fn->next);\n \t}\ndiff --git a/refs.c b/refs.c\nindex 7fb3350..64363ce 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1246,15 +1246,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master,\n static char *ref_msg(const char *line, const char *endp)\n {\n \tconst char *ep;\n-\tchar *msg;\n-\n \tline += 82;\n-\tfor (ep = line; ep < endp && *ep != '\\n'; ep++)\n-\t\t;\n-\tmsg = xmalloc(ep - line + 1);\n-\tmemcpy(msg, line, ep - line);\n-\tmsg[ep - line] = 0;\n-\treturn msg;\n+\tep = memchr(line, '\\n', endp - line);\n+\tif (!ep)\n+\t\tep = endp;\n+\treturn xmemdup(line, ep - line);\n }\n \n int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 59325d4..135bc02 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1491,11 +1491,8 @@ found_cache_entry:\n \t\tent->lru.next->prev = ent->lru.prev;\n \t\tent->lru.prev->next = ent->lru.next;\n \t\tdelta_base_cached -= ent->size;\n-\t}\n-\telse {\n-\t\tret = xmalloc(ent->size + 1);\n-\t\tmemcpy(ret, ent->data, ent->size);\n-\t\t((char *)ret)[ent->size] = 0;\n+\t} else {\n+\t\tret = xmemdup(ent->data, ent->size);\n \t}\n \t*type = ent->type;\n \t*base_size = ent->size;\n@@ -1872,12 +1869,9 @@ void *read_sha1_file(const unsigned char *sha1, enum object_type *type,\n \n \tco = find_cached_object(sha1);\n \tif (co) {\n-\t\tbuf = xmalloc(co->size + 1);\n-\t\tmemcpy(buf, co->buf, co->size);\n-\t\t((char*)buf)[co->size] = 0;\n \t\t*type = co->type;\n \t\t*size = co->size;\n-\t\treturn buf;\n+\t\treturn xmemdup(co->buf, co->size);\n \t}\n \n \tbuf = read_packed_sha1(sha1, type, size);\ndiff --git a/tag.c b/tag.c\nindex bbacd59..f82a668 100644\n--- a/tag.c\n+++ b/tag.c\n@@ -68,9 +68,7 @@ int parse_tag_buffer(struct tag *item, void *data, unsigned long size)\n \tmemcpy(type, type_line + 5, typelen);\n \ttype[typelen] = '\\0';\n \ttaglen = sig_line - tag_line - strlen(\"tag \\n\");\n-\titem->tag = xmalloc(taglen + 1);\n-\tmemcpy(item->tag, tag_line + 4, taglen);\n-\titem->tag[taglen] = '\\0';\n+\titem->tag = xmemdup(tag_line + 4, taglen);\n \n \tif (!strcmp(type, blob_type)) {\n \t\titem->tagged = &lookup_blob(sha1)->object;\n-- \n1.5.3.1\n"},{"id":"53325","messageId":"20070917161113.GB460@artemis.corp","threadId":"9896","inReplyTo":null,"subject":"xmemdup patches","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-17T16:11:13Z","receivedAt":"2007-09-17T16:11:13Z","isPatch":false,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"  I noticed a lot of places in git's code use code like:\n\n  char *res;\n\n  ...\n\n  res = xmalloc(len + 1);\n  memcpy(res, src, len);\n  res[len] = '\\0';\n  return res;\n\n  I've added a \"xmemdup\" function that duplicates a portion of memory,\nalso adding an extra NUL after the end of the buffer. There was a\nxstrndup already, doing almost the same, except that it worked like\nstrndup, meaning that it duplicates the memory areay up to len or the\nfirst embeded NUL. The extra scan costs, and is often not necessary (as\nwe want to extract a token from a buffer we just validated e.g.).\n\n  There were 41 of those places.\n\n\n  I'm not a huge fan of \"xmemdup\" as I would not have supposed that a\nfunction called like that would add the extra NUL, so I'm 100% okay with\nsomeone coming up with any better name.\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"53328","messageId":"20070917161420.GC460@artemis.corp","threadId":"9896","inReplyTo":"20070917161113.GB460@artemis.corp","subject":"Re: xmemdup patches","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-17T16:14:20Z","receivedAt":"2007-09-17T16:14:20Z","isPatch":false,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"On lun, sep 17, 2007 at 04:11:13 +0000, Pierre Habouzit wrote:\n>   There were 41 of those places.\n\n  I forgot to say, I replaced _some_ xstrndup uses with a xmemdup when\nit was clear to me that xmemdup was Okay. I left some alone when it\nwasn't obvious in the first 10 seconds that xmemdup would work the same\nway.\n\n  The real goal is code brevity and readability, xstrndup or xmemdup are\nthe same in that regard, and I don't think the small performance gain is\nreally worth the headache.\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"53331","messageId":"Pine.LNX.4.64.0709171739010.28586@racer.site","threadId":"9896","inReplyTo":"20070917161113.GB460@artemis.corp","subject":"Re: xmemdup patches","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-09-17T16:39:23Z","receivedAt":"2007-09-17T16:39:23Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 17 Sep 2007, Pierre Habouzit wrote:\n\n>   I'm not a huge fan of \"xmemdup\" as I would not have supposed that a\n> function called like that would add the extra NUL, so I'm 100% okay with\n> someone coming up with any better name.\n\nxmemdupz()?\n\nCiao,\nDscho\n"},{"id":"53336","messageId":"20070917174320.GA16893@artemis.corp","threadId":"9896","inReplyTo":"Pine.LNX.4.64.0709171739010.28586@racer.site","subject":"Re: xmemdup patches","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-17T17:43:20Z","receivedAt":"2007-09-17T17:43:20Z","isPatch":false,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"On Mon, Sep 17, 2007 at 04:39:23PM +0000, Johannes Schindelin wrote:\n> On Mon, 17 Sep 2007, Pierre Habouzit wrote:\n> >   I'm not a huge fan of \"xmemdup\" as I would not have supposed that a\n> > function called like that would add the extra NUL, so I'm 100% okay with\n> > someone coming up with any better name.\n>\n> xmemdupz()?\n\n  Why not, it's definitely better than xmemdupasstring ;)\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"},{"id":"53558","messageId":"7v6427qdqr.fsf@gitster.siamese.dyndns.org","threadId":"9896","inReplyTo":"20070917161142.D3C9A344A49@madism.org","subject":"Re: [PATCH 2/2] Use xmemdup in many places.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-19T08:08:12Z","receivedAt":"2007-09-19T08:08:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Pierre Habouzit <madcoder@debian.org> writes:\n\n> Signed-off-by: Pierre Habouzit <madcoder@debian.org>\n> ---\n>  attr.c                  |    7 +------\n>  builtin-add.c           |    8 ++------\n>  builtin-apply.c         |   11 ++---------\n>  builtin-fetch--tool.c   |    6 +-----\n>  builtin-fmt-merge-msg.c |   17 ++++++-----------\n>  builtin-for-each-ref.c  |   40 +++++++++-------------------------------\n>  builtin-log.c           |   12 ++----------\n>  builtin-ls-files.c      |    9 +--------\n>  builtin-mv.c            |    5 +----\n>  builtin-revert.c        |    4 +---\n>  builtin-shortlog.c      |   11 ++---------\n>  commit.c                |   16 ++++++----------\n>  connect.c               |    4 +---\n>  convert.c               |    7 +------\n>  diff.c                  |   13 ++-----------\n>  diffcore-order.c        |    7 ++-----\n>  fast-import.c           |    4 +---\n>  http-push.c             |    9 ++-------\n>  imap-send.c             |   20 +++++---------------\n>  merge-recursive.c       |   19 ++++---------------\n>  refs.c                  |   12 ++++--------\n>  sha1_file.c             |   12 +++---------\n>  tag.c                   |    4 +---\n>  23 files changed, 60 insertions(+), 197 deletions(-)\n> ...\n> diff --git a/builtin-apply.c b/builtin-apply.c\n> index 05011bb..900d0a7 100644\n> --- a/builtin-apply.c\n> +++ b/builtin-apply.c\n> @@ -293,11 +293,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)\n>  \t\t\treturn def;\n>  \t}\n>  \n> -\tname = xmalloc(len + 1);\n> -\tmemcpy(name, start, len);\n> -\tname[len] = 0;\n> -\tfree(def);\n> -\treturn name;\n> +\treturn xmemdup(start, len);\n>  }\n\nDid we start leaking \"def\" here? \n\n> diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c\n> index 0afa1c5..287d52a 100644\n> --- a/builtin-for-each-ref.c\n> +++ b/builtin-for-each-ref.c\n> ...\n> @@ -305,46 +301,28 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un\n> ...\n>  static const char *copy_name(const char *buf)\n>  {\n> -\tconst char *eol = strchr(buf, '\\n');\n> -\tconst char *eoname = strstr(buf, \" <\");\n> -\tchar *line;\n> -\tint len;\n> -\tif (!(eoname && eol && eoname < eol))\n> -\t\treturn \"\";\n> -\tlen = eoname - buf;\n> -\tline = xmalloc(len + 1);\n> -\tmemcpy(line, buf, len);\n> -\tline[len] = 0;\n> -\treturn line;\n> +\tconst char *cp;\n> +\tfor (cp = buf; *cp != '\\n'; cp++) {\n> +\t\tif (!strncmp(cp, \" <\", 2))\n> +\t\t\treturn xmemdup(buf, cp - buf);\n> +\t}\n> +\treturn \"\";\n>  }\n\nAt least the loop should terminate upon (!*cp); if you do not\nhave '\\n' in the buffer what happens?\n"},{"id":"53564","messageId":"20070919082611.GE28205@artemis.corp","threadId":"9896","inReplyTo":"7v6427qdqr.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 2/2] Use xmemdup in many places.","fromName":"Pierre Habouzit","fromEmail":"madcoder@debian.org","sentAt":"2007-09-19T08:26:11Z","receivedAt":"2007-09-19T08:26:11Z","isPatch":true,"sender":{"key":"madcoder@debian.org","avatar":"https://avatars.githubusercontent.com/u/44708?v=4"},"body":"On Wed, Sep 19, 2007 at 08:08:12AM +0000, Junio C Hamano wrote:\n> Pierre Habouzit <madcoder@debian.org> writes:\n> \n> > Signed-off-by: Pierre Habouzit <madcoder@debian.org>\n> > ---\n> >  attr.c                  |    7 +------\n> >  builtin-add.c           |    8 ++------\n> >  builtin-apply.c         |   11 ++---------\n> >  builtin-fetch--tool.c   |    6 +-----\n> >  builtin-fmt-merge-msg.c |   17 ++++++-----------\n> >  builtin-for-each-ref.c  |   40 +++++++++-------------------------------\n> >  builtin-log.c           |   12 ++----------\n> >  builtin-ls-files.c      |    9 +--------\n> >  builtin-mv.c            |    5 +----\n> >  builtin-revert.c        |    4 +---\n> >  builtin-shortlog.c      |   11 ++---------\n> >  commit.c                |   16 ++++++----------\n> >  connect.c               |    4 +---\n> >  convert.c               |    7 +------\n> >  diff.c                  |   13 ++-----------\n> >  diffcore-order.c        |    7 ++-----\n> >  fast-import.c           |    4 +---\n> >  http-push.c             |    9 ++-------\n> >  imap-send.c             |   20 +++++---------------\n> >  merge-recursive.c       |   19 ++++---------------\n> >  refs.c                  |   12 ++++--------\n> >  sha1_file.c             |   12 +++---------\n> >  tag.c                   |    4 +---\n> >  23 files changed, 60 insertions(+), 197 deletions(-)\n> > ...\n> > diff --git a/builtin-apply.c b/builtin-apply.c\n> > index 05011bb..900d0a7 100644\n> > --- a/builtin-apply.c\n> > +++ b/builtin-apply.c\n> > @@ -293,11 +293,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)\n> >  \t\t\treturn def;\n> >  \t}\n> >  \n> > -\tname = xmalloc(len + 1);\n> > -\tmemcpy(name, start, len);\n> > -\tname[len] = 0;\n> > -\tfree(def);\n> > -\treturn name;\n> > +\treturn xmemdup(start, len);\n> >  }\n> \n> Did we start leaking \"def\" here? \n\n  Hmm I fear we are. that has to be fixed indeed.\n\n-- \n·O·  Pierre Habouzit\n··O                                                madcoder@debian.org\nOOO                                                http://www.madism.org\n"}]}