{"thread":{"id":"5452","subject":"[PATCH] Replace uses of strdup with xstrdup.","startedAt":"2006-09-02T04:16:31Z","lastAt":"2006-09-02T04:16:31Z","messageCount":1,"participants":["Shawn Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"26207","messageId":"20060902041631.GA25093@spearce.org","threadId":"5452","inReplyTo":null,"subject":"[PATCH] Replace uses of strdup with xstrdup.","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-09-02T04:16:31Z","receivedAt":"2006-09-02T04:16:31Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Like xmalloc and xrealloc xstrdup dies with a useful message if\nthe native strdup() implementation returns NULL rather than a\nvalid pointer.\n\nI just tried to use xstrdup in new code and found it to be missing.\nHowever I expected it to be present as xmalloc and xrealloc are\nalready commonly used throughout the code.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n blame.c                 |    2 +-\n builtin-apply.c         |    2 +-\n builtin-fmt-merge-msg.c |   22 +++++++++++-----------\n builtin-grep.c          |    2 +-\n builtin-name-rev.c      |    2 +-\n builtin-prune.c         |    2 +-\n builtin-push.c          |   10 +++++-----\n builtin-repo-config.c   |    6 +++---\n builtin-rev-list.c      |    4 ++--\n builtin-rm.c            |    2 +-\n builtin-show-branch.c   |    6 +++---\n builtin-symbolic-ref.c  |    4 ++--\n builtin-tar-tree.c      |    2 +-\n builtin-upload-tar.c    |    2 +-\n builtin-zip-tree.c      |    4 ++--\n commit.c                |    2 +-\n config.c                |    8 ++++----\n connect.c               |    6 +++---\n diff.c                  |    4 ++--\n environment.c           |    2 +-\n fetch.c                 |    4 ++--\n fsck-objects.c          |    2 +-\n git-compat-util.h       |    8 ++++++++\n git.c                   |    2 +-\n http-fetch.c            |    2 +-\n http-push.c             |    6 +++---\n imap-send.c             |   12 ++++++------\n merge-file.c            |    2 +-\n merge-recursive.c       |   14 +++++++-------\n merge-tree.c            |    4 ++--\n path-list.c             |    2 +-\n refs.c                  |    4 ++--\n server-info.c           |    2 +-\n sha1_file.c             |    8 ++++----\n sha1_name.c             |    4 ++--\n 35 files changed, 89 insertions(+), 81 deletions(-)\n\ndiff --git a/blame.c b/blame.c\nindex 8968046..8cfd5d9 100644\n--- a/blame.c\n+++ b/blame.c\n@@ -617,7 +617,7 @@ static void simplify_commit(struct rev_i\n \t\t\t\tif (new_name) {\n \t\t\t\t\tstruct util_info* putil = get_util(p);\n \t\t\t\t\tif (!putil->pathname)\n-\t\t\t\t\t\tputil->pathname = strdup(new_name);\n+\t\t\t\t\t\tputil->pathname = xstrdup(new_name);\n \t\t\t\t} else {\n \t\t\t\t\t*pp = parent->next;\n \t\t\t\t\tcontinue;\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 1a1deaf..872c800 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -2449,7 +2449,7 @@ static int apply_patch(int fd, const cha\n static int git_apply_config(const char *var, const char *value)\n {\n \tif (!strcmp(var, \"apply.whitespace\")) {\n-\t\tapply_default_whitespace = strdup(value);\n+\t\tapply_default_whitespace = xstrdup(value);\n \t\treturn 0;\n \t}\n \treturn git_default_config(var, value);\ndiff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c\nindex ed59e77..b946b7d 100644\n--- a/builtin-fmt-merge-msg.c\n+++ b/builtin-fmt-merge-msg.c\n@@ -111,29 +111,29 @@ static int handle_line(char *line)\n \ti = find_in_list(&srcs, src);\n \tif (i < 0) {\n \t\ti = srcs.nr;\n-\t\tappend_to_list(&srcs, strdup(src),\n+\t\tappend_to_list(&srcs, xstrdup(src),\n \t\t\t\txcalloc(1, sizeof(struct src_data)));\n \t}\n \tsrc_data = srcs.payload[i];\n \n \tif (pulling_head) {\n-\t\torigin = strdup(src);\n+\t\torigin = xstrdup(src);\n \t\tsrc_data->head_status |= 1;\n \t} else if (!strncmp(line, \"branch \", 7)) {\n-\t\torigin = strdup(line + 7);\n+\t\torigin = xstrdup(line + 7);\n \t\tappend_to_list(&src_data->branch, origin, NULL);\n \t\tsrc_data->head_status |= 2;\n \t} else if (!strncmp(line, \"tag \", 4)) {\n \t\torigin = line;\n-\t\tappend_to_list(&src_data->tag, strdup(origin + 4), NULL);\n+\t\tappend_to_list(&src_data->tag, xstrdup(origin + 4), NULL);\n \t\tsrc_data->head_status |= 2;\n \t} else if (!strncmp(line, \"remote branch \", 14)) {\n-\t\torigin = strdup(line + 14);\n+\t\torigin = xstrdup(line + 14);\n \t\tappend_to_list(&src_data->r_branch, origin, NULL);\n \t\tsrc_data->head_status |= 2;\n \t} else {\n-\t\torigin = strdup(src);\n-\t\tappend_to_list(&src_data->generic, strdup(line), NULL);\n+\t\torigin = xstrdup(src);\n+\t\tappend_to_list(&src_data->generic, xstrdup(line), NULL);\n \t\tsrc_data->head_status |= 2;\n \t}\n \n@@ -145,7 +145,7 @@ static int handle_line(char *line)\n \t\t\tnew_origin[len - 1] = 0;\n \t\t\torigin = new_origin;\n \t\t} else\n-\t\t\torigin = strdup(origin);\n+\t\t\torigin = xstrdup(origin);\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@@ -203,7 +203,7 @@ static void shortlog(const char *name, u\n \n \t\tbol = strstr(commit->buffer, \"\\n\\n\");\n \t\tif (!bol) {\n-\t\t\tappend_to_list(&subjects, strdup(sha1_to_hex(\n+\t\t\tappend_to_list(&subjects, xstrdup(sha1_to_hex(\n \t\t\t\t\t\t\tcommit->object.sha1)),\n \t\t\t\t\tNULL);\n \t\t\tcontinue;\n@@ -218,7 +218,7 @@ static void shortlog(const char *name, u\n \t\t\tmemcpy(oneline, bol, len);\n \t\t\toneline[len] = 0;\n \t\t} else\n-\t\t\toneline = strdup(bol);\n+\t\t\toneline = xstrdup(bol);\n \t\tappend_to_list(&subjects, oneline, NULL);\n \t}\n \n@@ -277,7 +277,7 @@ int cmd_fmt_merge_msg(int argc, const ch\n \t\tusage(fmt_merge_msg_usage);\n \n \t/* get current branch */\n-\thead = strdup(git_path(\"HEAD\"));\n+\thead = xstrdup(git_path(\"HEAD\"));\n \tcurrent_branch = resolve_ref(head, head_sha1, 1);\n \tcurrent_branch += strlen(head) - 4;\n \tfree((char *)head);\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 8213ce2..6430f6d 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -1048,7 +1048,7 @@ int cmd_grep(int argc, const char **argv\n \t\t\t\t/* ignore empty line like grep does */\n \t\t\t\tif (!buf[0])\n \t\t\t\t\tcontinue;\n-\t\t\t\tadd_pattern(&opt, strdup(buf), argv[1], ++lno,\n+\t\t\t\tadd_pattern(&opt, xstrdup(buf), argv[1], ++lno,\n \t\t\t\t\t    GREP_PATTERN);\n \t\t\t}\n \t\t\tfclose(patterns);\ndiff --git a/builtin-name-rev.c b/builtin-name-rev.c\nindex d44e782..52886b6 100644\n--- a/builtin-name-rev.c\n+++ b/builtin-name-rev.c\n@@ -100,7 +100,7 @@ static int name_ref(const char *path, co\n \t\telse if (!strncmp(path, \"refs/\", 5))\n \t\t\tpath = path + 5;\n \n-\t\tname_rev(commit, strdup(path), 0, 0, deref);\n+\t\tname_rev(commit, xstrdup(path), 0, 0, deref);\n \t}\n \treturn 0;\n }\ndiff --git a/builtin-prune.c b/builtin-prune.c\nindex fc885ce..6228c79 100644\n--- a/builtin-prune.c\n+++ b/builtin-prune.c\n@@ -106,7 +106,7 @@ static void process_tree(struct tree *tr\n \tobj->flags |= SEEN;\n \tif (parse_tree(tree) < 0)\n \t\tdie(\"bad tree object %s\", sha1_to_hex(obj->sha1));\n-\tname = strdup(name);\n+\tname = xstrdup(name);\n \tadd_object(obj, p, path, name);\n \tme.up = path;\n \tme.elem = name;\ndiff --git a/builtin-push.c b/builtin-push.c\nindex ada8338..c43f256 100644\n--- a/builtin-push.c\n+++ b/builtin-push.c\n@@ -33,7 +33,7 @@ static int expand_one_ref(const char *re\n \tref += 5;\n \n \tif (!strncmp(ref, \"tags/\", 5))\n-\t\tadd_refspec(strdup(ref));\n+\t\tadd_refspec(xstrdup(ref));\n \treturn 0;\n }\n \n@@ -100,12 +100,12 @@ static int get_remotes_uri(const char *r\n \n \t\tif (!is_refspec) {\n \t\t\tif (n < MAX_URI)\n-\t\t\t\turi[n++] = strdup(s);\n+\t\t\t\turi[n++] = xstrdup(s);\n \t\t\telse\n \t\t\t\terror(\"more than %d URL's specified, ignoring the rest\", MAX_URI);\n \t\t}\n \t\telse if (is_refspec && !has_explicit_refspec)\n-\t\t\tadd_refspec(strdup(s));\n+\t\t\tadd_refspec(xstrdup(s));\n \t}\n \tfclose(f);\n \tif (!n)\n@@ -125,13 +125,13 @@ static int get_remote_config(const char*\n \t    !strncmp(key + 7, config_repo, config_repo_len)) {\n \t\tif (!strcmp(key + 7 + config_repo_len, \".url\")) {\n \t\t\tif (config_current_uri < MAX_URI)\n-\t\t\t\tconfig_uri[config_current_uri++] = strdup(value);\n+\t\t\t\tconfig_uri[config_current_uri++] = xstrdup(value);\n \t\t\telse\n \t\t\t\terror(\"more than %d URL's specified, ignoring the rest\", MAX_URI);\n \t\t}\n \t\telse if (config_get_refspecs &&\n \t\t\t !strcmp(key + 7 + config_repo_len, \".push\"))\n-\t\t\tadd_refspec(strdup(value));\n+\t\t\tadd_refspec(xstrdup(value));\n \t}\n \treturn 0;\n }\ndiff --git a/builtin-repo-config.c b/builtin-repo-config.c\nindex d19f7dc..f60cee1 100644\n--- a/builtin-repo-config.c\n+++ b/builtin-repo-config.c\n@@ -72,12 +72,12 @@ static int get_value(const char* key_, c\n \t\tconst char *home = getenv(\"HOME\");\n \t\tlocal = getenv(\"GIT_CONFIG_LOCAL\");\n \t\tif (!local)\n-\t\t\tlocal = repo_config = strdup(git_path(\"config\"));\n+\t\t\tlocal = repo_config = xstrdup(git_path(\"config\"));\n \t\tif (home)\n-\t\t\tglobal = strdup(mkpath(\"%s/.gitconfig\", home));\n+\t\t\tglobal = xstrdup(mkpath(\"%s/.gitconfig\", home));\n \t}\n \n-\tkey = strdup(key_);\n+\tkey = xstrdup(key_);\n \tfor (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)\n \t\t*tl = tolower(*tl);\n \tfor (tl=key; *tl && *tl != '.'; ++tl)\ndiff --git a/builtin-rev-list.c b/builtin-rev-list.c\nindex 402af8e..8437454 100644\n--- a/builtin-rev-list.c\n+++ b/builtin-rev-list.c\n@@ -109,7 +109,7 @@ static void process_blob(struct blob *bl\n \tif (obj->flags & (UNINTERESTING | SEEN))\n \t\treturn;\n \tobj->flags |= SEEN;\n-\tname = strdup(name);\n+\tname = xstrdup(name);\n \tadd_object(obj, p, path, name);\n }\n \n@@ -130,7 +130,7 @@ static void process_tree(struct tree *tr\n \tif (parse_tree(tree) < 0)\n \t\tdie(\"bad tree object %s\", sha1_to_hex(obj->sha1));\n \tobj->flags |= SEEN;\n-\tname = strdup(name);\n+\tname = xstrdup(name);\n \tadd_object(obj, p, path, name);\n \tme.up = path;\n \tme.elem = name;\ndiff --git a/builtin-rm.c b/builtin-rm.c\nindex 593d867..33d04bd 100644\n--- a/builtin-rm.c\n+++ b/builtin-rm.c\n@@ -32,7 +32,7 @@ static int remove_file(const char *name)\n \n \tret = unlink(name);\n \tif (!ret && (slash = strrchr(name, '/'))) {\n-\t\tchar *n = strdup(name);\n+\t\tchar *n = xstrdup(name);\n \t\tdo {\n \t\t\tn[slash - name] = 0;\n \t\t\tname = n;\ndiff --git a/builtin-show-branch.c b/builtin-show-branch.c\nindex d7de18e..578c9fa 100644\n--- a/builtin-show-branch.c\n+++ b/builtin-show-branch.c\n@@ -163,7 +163,7 @@ static void name_commits(struct commit_l\n \t\t\t\t\ten += sprintf(en, \"^\");\n \t\t\t\telse\n \t\t\t\t\ten += sprintf(en, \"^%d\", nth);\n-\t\t\t\tname_commit(p, strdup(newname), 0);\n+\t\t\t\tname_commit(p, xstrdup(newname), 0);\n \t\t\t\ti++;\n \t\t\t\tname_first_parent_chain(p);\n \t\t\t}\n@@ -364,7 +364,7 @@ static int append_ref(const char *refnam\n \t\t\trefname, MAX_REVS);\n \t\treturn 0;\n \t}\n-\tref_name[ref_name_cnt++] = strdup(refname);\n+\tref_name[ref_name_cnt++] = xstrdup(refname);\n \tref_name[ref_name_cnt] = NULL;\n \treturn 0;\n }\n@@ -521,7 +521,7 @@ static int git_show_branch_config(const \n \t\t\tdefault_alloc = default_alloc * 3 / 2 + 20;\n \t\t\tdefault_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);\n \t\t}\n-\t\tdefault_arg[default_num++] = strdup(value);\n+\t\tdefault_arg[default_num++] = xstrdup(value);\n \t\tdefault_arg[default_num] = NULL;\n \t\treturn 0;\n \t}\ndiff --git a/builtin-symbolic-ref.c b/builtin-symbolic-ref.c\nindex b4ec6f2..1d3a5e2 100644\n--- a/builtin-symbolic-ref.c\n+++ b/builtin-symbolic-ref.c\n@@ -7,7 +7,7 @@ static const char git_symbolic_ref_usage\n static void check_symref(const char *HEAD)\n {\n \tunsigned char sha1[20];\n-\tconst char *git_HEAD = strdup(git_path(\"%s\", HEAD));\n+\tconst char *git_HEAD = xstrdup(git_path(\"%s\", HEAD));\n \tconst char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);\n \tif (git_refs_heads_master) {\n \t\t/* we want to strip the .git/ part */\n@@ -26,7 +26,7 @@ int cmd_symbolic_ref(int argc, const cha\n \t\tcheck_symref(argv[1]);\n \t\tbreak;\n \tcase 3:\n-\t\tcreate_symref(strdup(git_path(\"%s\", argv[1])), argv[2]);\n+\t\tcreate_symref(xstrdup(git_path(\"%s\", argv[1])), argv[2]);\n \t\tbreak;\n \tdefault:\n \t\tusage(git_symbolic_ref_usage);\ndiff --git a/builtin-tar-tree.c b/builtin-tar-tree.c\nindex 61a4135..fa666f7 100644\n--- a/builtin-tar-tree.c\n+++ b/builtin-tar-tree.c\n@@ -351,7 +351,7 @@ static int remote_tar(int argc, const ch\n \t\tusage(tar_tree_usage);\n \n \t/* --remote=<repo> */\n-\turl = strdup(argv[1]+9);\n+\turl = xstrdup(argv[1]+9);\n \tpid = git_connect(fd, url, exec);\n \tif (pid < 0)\n \t\treturn 1;\ndiff --git a/builtin-upload-tar.c b/builtin-upload-tar.c\nindex 7b401bb..06a945a 100644\n--- a/builtin-upload-tar.c\n+++ b/builtin-upload-tar.c\n@@ -53,7 +53,7 @@ int cmd_upload_tar(int argc, const char \n \t\t\treturn nak(\"expected (optional) base\");\n \t\tif (buf[len-1] == '\\n')\n \t\t\tbuf[--len] = 0;\n-\t\tbase = strdup(buf + 5);\n+\t\tbase = xstrdup(buf + 5);\n \t\tlen = packet_read_line(0, buf, sizeof(buf));\n \t}\n \tif (len)\ndiff --git a/builtin-zip-tree.c b/builtin-zip-tree.c\nindex a5b834d..1c1f683 100644\n--- a/builtin-zip-tree.c\n+++ b/builtin-zip-tree.c\n@@ -311,11 +311,11 @@ int cmd_zip_tree(int argc, const char **\n \n \tswitch (argc) {\n \tcase 3:\n-\t\tbase = strdup(argv[2]);\n+\t\tbase = xstrdup(argv[2]);\n \t\tbaselen = strlen(base);\n \t\tbreak;\n \tcase 2:\n-\t\tbase = strdup(\"\");\n+\t\tbase = xstrdup(\"\");\n \t\tbaselen = 0;\n \t\tbreak;\n \tdefault:\ndiff --git a/commit.c b/commit.c\nindex 3f4bc31..e5aa22e 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -233,7 +233,7 @@ static void prepare_commit_graft(void)\n \t}\n \tif (last_graft_file)\n \t\tfree(last_graft_file);\n-\tlast_graft_file = strdup(graft_file);\n+\tlast_graft_file = xstrdup(graft_file);\n \n \tread_graft_file(graft_file);\n \tcommit_graft_prepared = 1;\ndiff --git a/config.c b/config.c\nindex c0897cc..e8f0caf 100644\n--- a/config.c\n+++ b/config.c\n@@ -350,11 +350,11 @@ int git_config(config_fn_t fn)\n \t\thome = getenv(\"HOME\");\n \t\tfilename = getenv(\"GIT_CONFIG_LOCAL\");\n \t\tif (!filename)\n-\t\t\tfilename = repo_config = strdup(git_path(\"config\"));\n+\t\t\tfilename = repo_config = xstrdup(git_path(\"config\"));\n \t}\n \n \tif (home) {\n-\t\tchar *user_config = strdup(mkpath(\"%s/.gitconfig\", home));\n+\t\tchar *user_config = xstrdup(mkpath(\"%s/.gitconfig\", home));\n \t\tif (!access(user_config, R_OK))\n \t\t\tret = git_config_from_file(fn, user_config);\n \t\tfree(user_config);\n@@ -545,8 +545,8 @@ int git_config_set_multivar(const char* \n \t\tif (!config_filename)\n \t\t\tconfig_filename  = git_path(\"config\");\n \t}\n-\tconfig_filename = strdup(config_filename);\n-\tlock_file = strdup(mkpath(\"%s.lock\", config_filename));\n+\tconfig_filename = xstrdup(config_filename);\n+\tlock_file = xstrdup(mkpath(\"%s.lock\", config_filename));\n \n \t/*\n \t * Since \"key\" actually contains the section name and the real\ndiff --git a/connect.c b/connect.c\nindex e501ccc..06ef387 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -69,7 +69,7 @@ struct ref **get_remote_heads(int in, st\n \t\tif (len != name_len + 41) {\n \t\t\tif (server_capabilities)\n \t\t\t\tfree(server_capabilities);\n-\t\t\tserver_capabilities = strdup(name + name_len + 1);\n+\t\t\tserver_capabilities = xstrdup(name + name_len + 1);\n \t\t}\n \n \t\tif (!check_ref(name, name_len, flags))\n@@ -661,7 +661,7 @@ int git_connect(int fd[2], char *url, co\n \t\tif (path[1] == '~')\n \t\t\tpath++;\n \t\telse {\n-\t\t\tpath = strdup(ptr);\n+\t\t\tpath = xstrdup(ptr);\n \t\t\tfree_path = 1;\n \t\t}\n \n@@ -672,7 +672,7 @@ int git_connect(int fd[2], char *url, co\n \t\t/* These underlying connection commands die() if they\n \t\t * cannot connect.\n \t\t */\n-\t\tchar *target_host = strdup(host);\n+\t\tchar *target_host = xstrdup(host);\n \t\tif (git_use_proxy(host))\n \t\t\tgit_proxy_connect(fd, host);\n \t\telse\ndiff --git a/diff.c b/diff.c\nindex 70699fd..9dcbda3 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -216,7 +216,7 @@ static char *quote_one(const char *str)\n \t\treturn NULL;\n \tneedlen = quote_c_style(str, NULL, NULL, 0);\n \tif (!needlen)\n-\t\treturn strdup(str);\n+\t\treturn xstrdup(str);\n \txp = xmalloc(needlen + 1);\n \tquote_c_style(str, xp, NULL, 0);\n \treturn xp;\n@@ -658,7 +658,7 @@ static struct diffstat_file *diffstat_ad\n \t\tx->is_renamed = 1;\n \t}\n \telse\n-\t\tx->name = strdup(name_a);\n+\t\tx->name = xstrdup(name_a);\n \treturn x;\n }\n \ndiff --git a/environment.c b/environment.c\nindex cd276b0..0ab3385 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -67,7 +67,7 @@ void setup_git(char *new_git_dir, char *\n \t\tfree(git_graft_file);\n \tgit_graft_file = new_git_graft_file;\n \tif (!git_graft_file) {\n-\t\tgit_graft_file = strdup(git_path(\"info/grafts\"));\n+\t\tgit_graft_file = xstrdup(git_path(\"info/grafts\"));\n \t\tdyn_git_graft_file = 1;\n \t} else {\n \t\tdyn_git_graft_file = 0;\ndiff --git a/fetch.c b/fetch.c\nindex 7d3812c..34df8d3 100644\n--- a/fetch.c\n+++ b/fetch.c\n@@ -234,8 +234,8 @@ int pull_targets_stdin(char ***target, c\n \t\t\t*target = xrealloc(*target, targets_alloc * sizeof(**target));\n \t\t\t*write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));\n \t\t}\n-\t\t(*target)[targets] = strdup(tg_one);\n-\t\t(*write_ref)[targets] = rf_one ? strdup(rf_one) : NULL;\n+\t\t(*target)[targets] = xstrdup(tg_one);\n+\t\t(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;\n \t\ttargets++;\n \t}\n \treturn targets;\ndiff --git a/fsck-objects.c b/fsck-objects.c\nindex 24286de..4d994f3 100644\n--- a/fsck-objects.c\n+++ b/fsck-objects.c\n@@ -458,7 +458,7 @@ static void fsck_object_dir(const char *\n static int fsck_head_link(void)\n {\n \tunsigned char sha1[20];\n-\tconst char *git_HEAD = strdup(git_path(\"HEAD\"));\n+\tconst char *git_HEAD = xstrdup(git_path(\"HEAD\"));\n \tconst char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);\n \tint pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */\n \ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 91f2b0d..552b8ec 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -84,6 +84,14 @@ #define strlcpy gitstrlcpy\n extern size_t gitstrlcpy(char *, const char *, size_t);\n #endif\n \n+static inline char* xstrdup(const char *str)\n+{\n+\tchar *ret = strdup(str);\n+\tif (!ret)\n+\t\tdie(\"Out of memory, strdup failed\");\n+\treturn ret;\n+}\n+\n static inline void *xmalloc(size_t size)\n {\n \tvoid *ret = malloc(size);\ndiff --git a/git.c b/git.c\nindex 403fb3a..1d00111 100644\n--- a/git.c\n+++ b/git.c\n@@ -97,7 +97,7 @@ static char *alias_string;\n static int git_alias_config(const char *var, const char *value)\n {\n \tif (!strncmp(var, \"alias.\", 6) && !strcmp(var + 6, alias_command)) {\n-\t\talias_string = strdup(value);\n+\t\talias_string = xstrdup(value);\n \t}\n \treturn 0;\n }\ndiff --git a/http-fetch.c b/http-fetch.c\nindex 6806f36..fac1760 100644\n--- a/http-fetch.c\n+++ b/http-fetch.c\n@@ -787,7 +787,7 @@ static int remote_ls(struct alt_base *re\n \n \tls.flags = flags;\n \tls.repo = repo;\n-\tls.path = strdup(path);\n+\tls.path = xstrdup(path);\n \tls.dentry_name = NULL;\n \tls.dentry_flags = 0;\n \tls.userData = userData;\ndiff --git a/http-push.c b/http-push.c\nindex 7814666..670ff00 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -1539,7 +1539,7 @@ static void remote_ls(const char *path, \n \tstruct remote_ls_ctx ls;\n \n \tls.flags = flags;\n-\tls.path = strdup(path);\n+\tls.path = xstrdup(path);\n \tls.dentry_name = NULL;\n \tls.dentry_flags = 0;\n \tls.userData = userData;\n@@ -1738,7 +1738,7 @@ static struct object_list **process_tree\n \t\tdie(\"bad tree object %s\", sha1_to_hex(obj->sha1));\n \n \tobj->flags |= SEEN;\n-\tname = strdup(name);\n+\tname = xstrdup(name);\n \tp = add_one_object(obj, p);\n \tme.up = path;\n \tme.elem = name;\n@@ -2467,7 +2467,7 @@ int main(int argc, char **argv)\n \n \t\t/* Set up revision info for this refspec */\n \t\tcommit_argc = 3;\n-\t\tnew_sha1_hex = strdup(sha1_to_hex(ref->new_sha1));\n+\t\tnew_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));\n \t\told_sha1_hex = NULL;\n \t\tcommit_argv[1] = \"--objects\";\n \t\tcommit_argv[2] = new_sha1_hex;\ndiff --git a/imap-send.c b/imap-send.c\nindex 6a52dbd..362e474 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -1007,7 +1007,7 @@ imap_open_store( imap_server_conf_t *srv\n \t\t\t * getpass() returns a pointer to a static buffer.  make a copy\n \t\t\t * for long term storage.\n \t\t\t */\n-\t\t\tsrvc->pass = strdup( arg );\n+\t\t\tsrvc->pass = xstrdup( arg );\n \t\t}\n \t\tif (CAP(NOLOGIN)) {\n \t\t\tfprintf( stderr, \"Skipping account %s@%s, server forbids LOGIN\\n\", srvc->user, srvc->host );\n@@ -1263,7 +1263,7 @@ git_imap_config(const char *key, const c\n \tkey += sizeof imap_key - 1;\n \n \tif (!strcmp( \"folder\", key )) {\n-\t\timap_folder = strdup( val );\n+\t\timap_folder = xstrdup( val );\n \t} else if (!strcmp( \"host\", key )) {\n \t\t{\n \t\t\tif (!strncmp( \"imap:\", val, 5 ))\n@@ -1273,16 +1273,16 @@ git_imap_config(const char *key, const c\n \t\t}\n \t\tif (!strncmp( \"//\", val, 2 ))\n \t\t\tval += 2;\n-\t\tserver.host = strdup( val );\n+\t\tserver.host = xstrdup( val );\n \t}\n \telse if (!strcmp( \"user\", key ))\n-\t\tserver.user = strdup( val );\n+\t\tserver.user = xstrdup( val );\n \telse if (!strcmp( \"pass\", key ))\n-\t\tserver.pass = strdup( val );\n+\t\tserver.pass = xstrdup( val );\n \telse if (!strcmp( \"port\", key ))\n \t\tserver.port = git_config_int( key, val );\n \telse if (!strcmp( \"tunnel\", key ))\n-\t\tserver.tunnel = strdup( val );\n+\t\tserver.tunnel = xstrdup( val );\n \treturn 0;\n }\n \ndiff --git a/merge-file.c b/merge-file.c\nindex f32c653..fc9b148 100644\n--- a/merge-file.c\n+++ b/merge-file.c\n@@ -21,7 +21,7 @@ static const char *write_temp_file(mmfil\n \tfd = mkstemp(filename);\n \tif (fd < 0)\n \t\treturn NULL;\n-\tfilename = strdup(filename);\n+\tfilename = xstrdup(filename);\n \tif (f->size != xwrite(fd, f->ptr, f->size)) {\n \t\trm_temp_file(filename);\n \t\treturn NULL;\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 48b2763..611cd95 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -513,8 +513,8 @@ static char *unique_path(const char *pat\n \n static int mkdir_p(const char *path, unsigned long mode)\n {\n-\t/* path points to cache entries, so strdup before messing with it */\n-\tchar *buf = strdup(path);\n+\t/* path points to cache entries, so xstrdup before messing with it */\n+\tchar *buf = xstrdup(path);\n \tint result = safe_create_leading_directories(buf);\n \tfree(buf);\n \treturn result;\n@@ -668,9 +668,9 @@ static struct merge_file_info merge_file\n \t\t\tgit_unpack_file(a->sha1, src1);\n \t\t\tgit_unpack_file(b->sha1, src2);\n \n-\t\t\targv[2] = la = strdup(mkpath(\"%s/%s\", branch1, a->path));\n-\t\t\targv[6] = lb = strdup(mkpath(\"%s/%s\", branch2, b->path));\n-\t\t\targv[4] = lo = strdup(mkpath(\"orig/%s\", o->path));\n+\t\t\targv[2] = la = xstrdup(mkpath(\"%s/%s\", branch1, a->path));\n+\t\t\targv[6] = lb = xstrdup(mkpath(\"%s/%s\", branch2, b->path));\n+\t\t\targv[4] = lo = xstrdup(mkpath(\"orig/%s\", o->path));\n \t\t\targv[7] = src1;\n \t\t\targv[8] = orig;\n \t\t\targv[9] = src2,\n@@ -1314,9 +1314,9 @@ int main(int argc, char *argv[])\n \toriginal_index_file = getenv(\"GIT_INDEX_FILE\");\n \n \tif (!original_index_file)\n-\t\toriginal_index_file = strdup(git_path(\"index\"));\n+\t\toriginal_index_file = xstrdup(git_path(\"index\"));\n \n-\ttemporary_index_file = strdup(git_path(\"mrg-rcrsv-tmp-idx\"));\n+\ttemporary_index_file = xstrdup(git_path(\"mrg-rcrsv-tmp-idx\"));\n \n \tif (argc < 4)\n \t\tdie(\"Usage: %s <base>... -- <head> <remote> ...\\n\", argv[0]);\ndiff --git a/merge-tree.c b/merge-tree.c\nindex c2e9a86..60df758 100644\n--- a/merge-tree.c\n+++ b/merge-tree.c\n@@ -177,7 +177,7 @@ static void resolve(const char *base, st\n \tif (!branch1)\n \t\treturn;\n \n-\tpath = strdup(mkpath(\"%s%s\", base, result->path));\n+\tpath = xstrdup(mkpath(\"%s%s\", base, result->path));\n \torig = create_entry(2, branch1->mode, branch1->sha1, path);\n \tfinal = create_entry(0, result->mode, result->sha1, path);\n \n@@ -233,7 +233,7 @@ static struct merge_list *link_entry(uns\n \tif (entry)\n \t\tpath = entry->path;\n \telse\n-\t\tpath = strdup(mkpath(\"%s%s\", base, n->path));\n+\t\tpath = xstrdup(mkpath(\"%s%s\", base, n->path));\n \tlink = create_entry(stage, n->mode, n->sha1, path);\n \tlink->link = entry;\n \treturn link;\ndiff --git a/path-list.c b/path-list.c\nindex b1ee72d..0c332dc 100644\n--- a/path-list.c\n+++ b/path-list.c\n@@ -45,7 +45,7 @@ static int add_entry(struct path_list *l\n \t\t\t\t(list->nr - index)\n \t\t\t\t* sizeof(struct path_list_item));\n \tlist->items[index].path = list->strdup_paths ?\n-\t\tstrdup(path) : (char *)path;\n+\t\txstrdup(path) : (char *)path;\n \tlist->items[index].util = NULL;\n \tlist->nr++;\n \ndiff --git a/refs.c b/refs.c\nindex aab14fc..5e65314 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -313,8 +313,8 @@ static struct ref_lock *lock_ref_sha1_ba\n \t}\n \tlock->lk = xcalloc(1, sizeof(struct lock_file));\n \n-\tlock->ref_file = strdup(path);\n-\tlock->log_file = strdup(git_path(\"logs/%s\", lock->ref_file + plen));\n+\tlock->ref_file = xstrdup(path);\n+\tlock->log_file = xstrdup(git_path(\"logs/%s\", lock->ref_file + plen));\n \tlock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;\n \n \tif (safe_create_leading_directories(lock->ref_file))\ndiff --git a/server-info.c b/server-info.c\nindex 7df628f..2fb8f57 100644\n--- a/server-info.c\n+++ b/server-info.c\n@@ -23,7 +23,7 @@ static int add_info_ref(const char *path\n \n static int update_info_refs(int force)\n {\n-\tchar *path0 = strdup(git_path(\"info/refs\"));\n+\tchar *path0 = xstrdup(git_path(\"info/refs\"));\n \tint len = strlen(path0);\n \tchar *path1 = xmalloc(len + 2);\n \ndiff --git a/sha1_file.c b/sha1_file.c\nindex fe9176a..feb55c6 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -115,7 +115,7 @@ static void fill_sha1_path(char *pathbuf\n \n /*\n  * NOTE! This returns a statically allocated buffer, so you have to be\n- * careful about using it. Do a \"strdup()\" if you need to save the\n+ * careful about using it. Do a \"xstrdup()\" if you need to save the\n  * filename.\n  *\n  * Also note that this returns the location for creating.  Reading\n@@ -141,7 +141,7 @@ char *sha1_file_name(const unsigned char\n \t\tname = base + len + 1;\n \t\tif (last_objdir)\n \t\t\tfree((char *) last_objdir);\n-\t\tlast_objdir = strdup(sha1_file_directory);\n+\t\tlast_objdir = xstrdup(sha1_file_directory);\n \t}\n \tfill_sha1_path(name, sha1);\n \treturn base;\n@@ -164,7 +164,7 @@ char *sha1_pack_name(const unsigned char\n \t\tname = base + len + 11;\n \t\tif (last_objdir)\n \t\t\tfree((char *) last_objdir);\n-\t\tlast_objdir = strdup(sha1_file_directory);\n+\t\tlast_objdir = xstrdup(sha1_file_directory);\n \t}\n \n \tbuf = name;\n@@ -195,7 +195,7 @@ char *sha1_pack_index_name(const unsigne\n \t\tname = base + len + 11;\n \t\tif (last_objdir)\n \t\t\tfree((char *) last_objdir);\n-\t\tlast_objdir = strdup(sha1_file_directory);\n+\t\tlast_objdir = xstrdup(sha1_file_directory);\n \t}\n \n \tbuf = name;\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 60a2c0b..8fcc271 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -26,7 +26,7 @@ static int find_short_object_filename(in\n \t\tfakeent->name[-1] = '/';\n \t\tif (last_objdir)\n \t\t\tfree((char *) last_objdir);\n-\t\tlast_objdir = strdup(objdir);\n+\t\tlast_objdir = xstrdup(objdir);\n \t}\n \tfakeent->next = alt_odb_list;\n \n@@ -285,7 +285,7 @@ static int get_sha1_basic(const char *st\n \t\tpathname = resolve_ref(git_path(*p, len, str), this_result, 1);\n \t\tif (pathname) {\n \t\t\tif (!refs_found++)\n-\t\t\t\treal_path = strdup(pathname);\n+\t\t\t\treal_path = xstrdup(pathname);\n \t\t\tif (!warn_ambiguous_refs)\n \t\t\t\tbreak;\n \t\t}\n-- \n1.4.2.ga2654\n\n\n-- \nVGER BF report: U 0.5\n"}]}