Skip to content
/ git Public
forked from git/git

Commit

Permalink
Replace uses of strdup with xstrdup.
Browse files Browse the repository at this point in the history
Like xmalloc and xrealloc xstrdup dies with a useful message if
the native strdup() implementation returns NULL rather than a
valid pointer.

I just tried to use xstrdup in new code and found it to be missing.
However I expected it to be present as xmalloc and xrealloc are
already commonly used throughout the code.

[jc: removed the part that deals with last_XXX, which I am
 finding more and more dubious these days.]

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
spearce authored and Junio C Hamano committed Sep 2, 2006
1 parent ad1ed5e commit 9befac4
Show file tree
Hide file tree
Showing 34 changed files with 84 additions and 76 deletions.
2 changes: 1 addition & 1 deletion blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ static void simplify_commit(struct rev_info *revs, struct commit *commit)
if (new_name) {
struct util_info* putil = get_util(p);
if (!putil->pathname)
putil->pathname = strdup(new_name);
putil->pathname = xstrdup(new_name);
} else {
*pp = parent->next;
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
static int git_apply_config(const char *var, const char *value)
{
if (!strcmp(var, "apply.whitespace")) {
apply_default_whitespace = strdup(value);
apply_default_whitespace = xstrdup(value);
return 0;
}
return git_default_config(var, value);
Expand Down
22 changes: 11 additions & 11 deletions builtin-fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,29 @@ static int handle_line(char *line)
i = find_in_list(&srcs, src);
if (i < 0) {
i = srcs.nr;
append_to_list(&srcs, strdup(src),
append_to_list(&srcs, xstrdup(src),
xcalloc(1, sizeof(struct src_data)));
}
src_data = srcs.payload[i];

if (pulling_head) {
origin = strdup(src);
origin = xstrdup(src);
src_data->head_status |= 1;
} else if (!strncmp(line, "branch ", 7)) {
origin = strdup(line + 7);
origin = xstrdup(line + 7);
append_to_list(&src_data->branch, origin, NULL);
src_data->head_status |= 2;
} else if (!strncmp(line, "tag ", 4)) {
origin = line;
append_to_list(&src_data->tag, strdup(origin + 4), NULL);
append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
src_data->head_status |= 2;
} else if (!strncmp(line, "remote branch ", 14)) {
origin = strdup(line + 14);
origin = xstrdup(line + 14);
append_to_list(&src_data->r_branch, origin, NULL);
src_data->head_status |= 2;
} else {
origin = strdup(src);
append_to_list(&src_data->generic, strdup(line), NULL);
origin = xstrdup(src);
append_to_list(&src_data->generic, xstrdup(line), NULL);
src_data->head_status |= 2;
}

Expand All @@ -145,7 +145,7 @@ static int handle_line(char *line)
new_origin[len - 2] = 0;
origin = new_origin;
} else
origin = strdup(origin);
origin = xstrdup(origin);
} else {
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
sprintf(new_origin, "%s of %s", origin, src);
Expand Down Expand Up @@ -203,7 +203,7 @@ static void shortlog(const char *name, unsigned char *sha1,

bol = strstr(commit->buffer, "\n\n");
if (!bol) {
append_to_list(&subjects, strdup(sha1_to_hex(
append_to_list(&subjects, xstrdup(sha1_to_hex(
commit->object.sha1)),
NULL);
continue;
Expand All @@ -218,7 +218,7 @@ static void shortlog(const char *name, unsigned char *sha1,
memcpy(oneline, bol, len);
oneline[len] = 0;
} else
oneline = strdup(bol);
oneline = xstrdup(bol);
append_to_list(&subjects, oneline, NULL);
}

Expand Down Expand Up @@ -277,7 +277,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
usage(fmt_merge_msg_usage);

/* get current branch */
head = strdup(git_path("HEAD"));
head = xstrdup(git_path("HEAD"));
current_branch = resolve_ref(head, head_sha1, 1);
current_branch += strlen(head) - 4;
free((char *)head);
Expand Down
2 changes: 1 addition & 1 deletion builtin-grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
/* ignore empty line like grep does */
if (!buf[0])
continue;
add_pattern(&opt, strdup(buf), argv[1], ++lno,
add_pattern(&opt, xstrdup(buf), argv[1], ++lno,
GREP_PATTERN);
}
fclose(patterns);
Expand Down
2 changes: 1 addition & 1 deletion builtin-name-rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static int name_ref(const char *path, const unsigned char *sha1)
else if (!strncmp(path, "refs/", 5))
path = path + 5;

name_rev(commit, strdup(path), 0, 0, deref);
name_rev(commit, xstrdup(path), 0, 0, deref);
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-prune.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void process_tree(struct tree *tree,
obj->flags |= SEEN;
if (parse_tree(tree) < 0)
die("bad tree object %s", sha1_to_hex(obj->sha1));
name = strdup(name);
name = xstrdup(name);
add_object(obj, p, path, name);
me.up = path;
me.elem = name;
Expand Down
10 changes: 5 additions & 5 deletions builtin-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int expand_one_ref(const char *ref, const unsigned char *sha1)
ref += 5;

if (!strncmp(ref, "tags/", 5))
add_refspec(strdup(ref));
add_refspec(xstrdup(ref));
return 0;
}

Expand Down Expand Up @@ -100,12 +100,12 @@ static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])

if (!is_refspec) {
if (n < MAX_URI)
uri[n++] = strdup(s);
uri[n++] = xstrdup(s);
else
error("more than %d URL's specified, ignoring the rest", MAX_URI);
}
else if (is_refspec && !has_explicit_refspec)
add_refspec(strdup(s));
add_refspec(xstrdup(s));
}
fclose(f);
if (!n)
Expand All @@ -125,13 +125,13 @@ static int get_remote_config(const char* key, const char* value)
!strncmp(key + 7, config_repo, config_repo_len)) {
if (!strcmp(key + 7 + config_repo_len, ".url")) {
if (config_current_uri < MAX_URI)
config_uri[config_current_uri++] = strdup(value);
config_uri[config_current_uri++] = xstrdup(value);
else
error("more than %d URL's specified, ignoring the rest", MAX_URI);
}
else if (config_get_refspecs &&
!strcmp(key + 7 + config_repo_len, ".push"))
add_refspec(strdup(value));
add_refspec(xstrdup(value));
}
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions builtin-repo-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ static int get_value(const char* key_, const char* regex_)
const char *home = getenv("HOME");
local = getenv("GIT_CONFIG_LOCAL");
if (!local)
local = repo_config = strdup(git_path("config"));
local = repo_config = xstrdup(git_path("config"));
if (home)
global = strdup(mkpath("%s/.gitconfig", home));
global = xstrdup(mkpath("%s/.gitconfig", home));
}

key = strdup(key_);
key = xstrdup(key_);
for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
*tl = tolower(*tl);
for (tl=key; *tl && *tl != '.'; ++tl)
Expand Down
4 changes: 2 additions & 2 deletions builtin-rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static void process_blob(struct blob *blob,
if (obj->flags & (UNINTERESTING | SEEN))
return;
obj->flags |= SEEN;
name = strdup(name);
name = xstrdup(name);
add_object(obj, p, path, name);
}

Expand All @@ -130,7 +130,7 @@ static void process_tree(struct tree *tree,
if (parse_tree(tree) < 0)
die("bad tree object %s", sha1_to_hex(obj->sha1));
obj->flags |= SEEN;
name = strdup(name);
name = xstrdup(name);
add_object(obj, p, path, name);
me.up = path;
me.elem = name;
Expand Down
2 changes: 1 addition & 1 deletion builtin-rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static int remove_file(const char *name)

ret = unlink(name);
if (!ret && (slash = strrchr(name, '/'))) {
char *n = strdup(name);
char *n = xstrdup(name);
do {
n[slash - name] = 0;
name = n;
Expand Down
6 changes: 3 additions & 3 deletions builtin-show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void name_commits(struct commit_list *list,
en += sprintf(en, "^");
else
en += sprintf(en, "^%d", nth);
name_commit(p, strdup(newname), 0);
name_commit(p, xstrdup(newname), 0);
i++;
name_first_parent_chain(p);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ static int append_ref(const char *refname, const unsigned char *sha1)
refname, MAX_REVS);
return 0;
}
ref_name[ref_name_cnt++] = strdup(refname);
ref_name[ref_name_cnt++] = xstrdup(refname);
ref_name[ref_name_cnt] = NULL;
return 0;
}
Expand Down Expand Up @@ -521,7 +521,7 @@ static int git_show_branch_config(const char *var, const char *value)
default_alloc = default_alloc * 3 / 2 + 20;
default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
}
default_arg[default_num++] = strdup(value);
default_arg[default_num++] = xstrdup(value);
default_arg[default_num] = NULL;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions builtin-symbolic-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static const char git_symbolic_ref_usage[] =
static void check_symref(const char *HEAD)
{
unsigned char sha1[20];
const char *git_HEAD = strdup(git_path("%s", HEAD));
const char *git_HEAD = xstrdup(git_path("%s", HEAD));
const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
if (git_refs_heads_master) {
/* we want to strip the .git/ part */
Expand All @@ -26,7 +26,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
check_symref(argv[1]);
break;
case 3:
create_symref(strdup(git_path("%s", argv[1])), argv[2]);
create_symref(xstrdup(git_path("%s", argv[1])), argv[2]);
break;
default:
usage(git_symbolic_ref_usage);
Expand Down
2 changes: 1 addition & 1 deletion builtin-tar-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ static int remote_tar(int argc, const char **argv)
usage(tar_tree_usage);

/* --remote=<repo> */
url = strdup(argv[1]+9);
url = xstrdup(argv[1]+9);
pid = git_connect(fd, url, exec);
if (pid < 0)
return 1;
Expand Down
2 changes: 1 addition & 1 deletion builtin-upload-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int cmd_upload_tar(int argc, const char **argv, const char *prefix)
return nak("expected (optional) base");
if (buf[len-1] == '\n')
buf[--len] = 0;
base = strdup(buf + 5);
base = xstrdup(buf + 5);
len = packet_read_line(0, buf, sizeof(buf));
}
if (len)
Expand Down
4 changes: 2 additions & 2 deletions builtin-zip-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ int cmd_zip_tree(int argc, const char **argv, const char *prefix)

switch (argc) {
case 3:
base = strdup(argv[2]);
base = xstrdup(argv[2]);
baselen = strlen(base);
break;
case 2:
base = strdup("");
base = xstrdup("");
baselen = 0;
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ int git_config(config_fn_t fn)
home = getenv("HOME");
filename = getenv("GIT_CONFIG_LOCAL");
if (!filename)
filename = repo_config = strdup(git_path("config"));
filename = repo_config = xstrdup(git_path("config"));
}

if (home) {
char *user_config = strdup(mkpath("%s/.gitconfig", home));
char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
if (!access(user_config, R_OK))
ret = git_config_from_file(fn, user_config);
free(user_config);
Expand Down Expand Up @@ -545,8 +545,8 @@ int git_config_set_multivar(const char* key, const char* value,
if (!config_filename)
config_filename = git_path("config");
}
config_filename = strdup(config_filename);
lock_file = strdup(mkpath("%s.lock", config_filename));
config_filename = xstrdup(config_filename);
lock_file = xstrdup(mkpath("%s.lock", config_filename));

/*
* Since "key" actually contains the section name and the real
Expand Down
6 changes: 3 additions & 3 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct ref **get_remote_heads(int in, struct ref **list,
if (len != name_len + 41) {
if (server_capabilities)
free(server_capabilities);
server_capabilities = strdup(name + name_len + 1);
server_capabilities = xstrdup(name + name_len + 1);
}

if (!check_ref(name, name_len, flags))
Expand Down Expand Up @@ -661,7 +661,7 @@ int git_connect(int fd[2], char *url, const char *prog)
if (path[1] == '~')
path++;
else {
path = strdup(ptr);
path = xstrdup(ptr);
free_path = 1;
}

Expand All @@ -672,7 +672,7 @@ int git_connect(int fd[2], char *url, const char *prog)
/* These underlying connection commands die() if they
* cannot connect.
*/
char *target_host = strdup(host);
char *target_host = xstrdup(host);
if (git_use_proxy(host))
git_proxy_connect(fd, host);
else
Expand Down
4 changes: 2 additions & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static char *quote_one(const char *str)
return NULL;
needlen = quote_c_style(str, NULL, NULL, 0);
if (!needlen)
return strdup(str);
return xstrdup(str);
xp = xmalloc(needlen + 1);
quote_c_style(str, xp, NULL, 0);
return xp;
Expand Down Expand Up @@ -658,7 +658,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
x->is_renamed = 1;
}
else
x->name = strdup(name_a);
x->name = xstrdup(name_a);
return x;
}

Expand Down
2 changes: 1 addition & 1 deletion environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void setup_git_env(void)
}
git_graft_file = getenv(GRAFT_ENVIRONMENT);
if (!git_graft_file)
git_graft_file = strdup(git_path("info/grafts"));
git_graft_file = xstrdup(git_path("info/grafts"));
}

const char *get_git_dir(void)
Expand Down
4 changes: 2 additions & 2 deletions fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
*target = xrealloc(*target, targets_alloc * sizeof(**target));
*write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
}
(*target)[targets] = strdup(tg_one);
(*write_ref)[targets] = rf_one ? strdup(rf_one) : NULL;
(*target)[targets] = xstrdup(tg_one);
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
targets++;
}
return targets;
Expand Down
2 changes: 1 addition & 1 deletion fsck-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ static void fsck_object_dir(const char *path)
static int fsck_head_link(void)
{
unsigned char sha1[20];
const char *git_HEAD = strdup(git_path("HEAD"));
const char *git_HEAD = xstrdup(git_path("HEAD"));
const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */

Expand Down
8 changes: 8 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
extern size_t gitstrlcpy(char *, const char *, size_t);
#endif

static inline char* xstrdup(const char *str)
{
char *ret = strdup(str);
if (!ret)
die("Out of memory, strdup failed");
return ret;
}

static inline void *xmalloc(size_t size)
{
void *ret = malloc(size);
Expand Down
Loading

0 comments on commit 9befac4

Please sign in to comment.