Skip to content

Commit 3b8e8af

Browse files
committed
Merge branch 'jk/xstrfmt'
* jk/xstrfmt: setup_git_env(): introduce git_path_from_env() helper unique_path: fix unlikely heap overflow walker_fetch: fix minor memory leak merge: use argv_array when spawning merge strategy sequencer: use argv_array_pushf setup_git_env: use git_pathdup instead of xmalloc + sprintf use xstrfmt to replace xmalloc + strcpy/strcat use xstrfmt to replace xmalloc + sprintf use xstrdup instead of xmalloc + strcpy use xstrfmt in favor of manual size calculations strbuf: add xstrfmt helper
2 parents e91ae32 + cb6c38d commit 3b8e8af

20 files changed

+122
-163
lines changed

builtin/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,9 +1281,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
12811281
*/
12821282
patch->def_name = git_header_name(line, len);
12831283
if (patch->def_name && root) {
1284-
char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
1285-
strcpy(s, root);
1286-
strcpy(s + root_len, patch->def_name);
1284+
char *s = xstrfmt("%s%s", root, patch->def_name);
12871285
free(patch->def_name);
12881286
patch->def_name = s;
12891287
}

builtin/fetch.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,16 +1082,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
10821082
refs = xcalloc(argc + 1, sizeof(const char *));
10831083
for (i = 0; i < argc; i++) {
10841084
if (!strcmp(argv[i], "tag")) {
1085-
char *ref;
10861085
i++;
10871086
if (i >= argc)
10881087
die(_("You need to specify a tag name."));
1089-
ref = xmalloc(strlen(argv[i]) * 2 + 22);
1090-
strcpy(ref, "refs/tags/");
1091-
strcat(ref, argv[i]);
1092-
strcat(ref, ":refs/tags/");
1093-
strcat(ref, argv[i]);
1094-
refs[j++] = ref;
1088+
refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1089+
argv[i], argv[i]);
10951090
} else
10961091
refs[j++] = argv[i];
10971092
}

builtin/fmt-merge-msg.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
178178
int len = strlen(origin);
179179
if (origin[0] == '\'' && origin[len - 1] == '\'')
180180
origin = xmemdupz(origin + 1, len - 2);
181-
} else {
182-
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
183-
sprintf(new_origin, "%s of %s", origin, src);
184-
origin = new_origin;
185-
}
181+
} else
182+
origin = xstrfmt("%s of %s", origin, src);
186183
if (strcmp(".", src))
187184
origin_data->is_local_branch = 0;
188185
string_list_append(&origins, origin)->util = origin_data;

builtin/name-rev.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ static void name_rev(struct commit *commit,
3333
return;
3434

3535
if (deref) {
36-
char *new_name = xmalloc(strlen(tip_name)+3);
37-
strcpy(new_name, tip_name);
38-
strcat(new_name, "^0");
39-
tip_name = new_name;
36+
tip_name = xstrfmt("%s^0", tip_name);
4037

4138
if (generation)
4239
die("generation: %d, but deref?", generation);

builtin/receive-pack.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,9 @@ static void run_update_post_hook(struct command *commands)
614614
argv[0] = hook;
615615

616616
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
617-
char *p;
618617
if (cmd->error_string || cmd->did_not_exist)
619618
continue;
620-
p = xmalloc(strlen(cmd->ref_name) + 1);
621-
strcpy(p, cmd->ref_name);
622-
argv[argc] = p;
619+
argv[argc] = xstrdup(cmd->ref_name);
623620
argc++;
624621
}
625622
argv[argc] = NULL;

builtin/show-branch.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
755755
}
756756

757757
for (i = 0; i < reflog; i++) {
758-
char *logmsg, *m;
758+
char *logmsg;
759759
const char *msg;
760760
unsigned long timestamp;
761761
int tz;
@@ -770,11 +770,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
770770
msg = "(none)";
771771
else
772772
msg++;
773-
m = xmalloc(strlen(msg) + 200);
774-
sprintf(m, "(%s) %s",
775-
show_date(timestamp, tz, 1),
776-
msg);
777-
reflog_msg[i] = m;
773+
reflog_msg[i] = xstrfmt("(%s) %s",
774+
show_date(timestamp, tz, 1),
775+
msg);
778776
free(logmsg);
779777
sprintf(nth_desc, "%s@{%d}", *av, base+i);
780778
append_ref(nth_desc, sha1, 1);

environment.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ static char *expand_namespace(const char *raw_namespace)
124124
return strbuf_detach(&buf, NULL);
125125
}
126126

127+
static char *git_path_from_env(const char *envvar, const char *path)
128+
{
129+
const char *value = getenv(envvar);
130+
return value ? xstrdup(value) : git_pathdup("%s", path);
131+
}
132+
127133
static void setup_git_env(void)
128134
{
129135
const char *gitfile;
@@ -134,19 +140,9 @@ static void setup_git_env(void)
134140
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
135141
gitfile = read_gitfile(git_dir);
136142
git_dir = xstrdup(gitfile ? gitfile : git_dir);
137-
git_object_dir = getenv(DB_ENVIRONMENT);
138-
if (!git_object_dir) {
139-
git_object_dir = xmalloc(strlen(git_dir) + 9);
140-
sprintf(git_object_dir, "%s/objects", git_dir);
141-
}
142-
git_index_file = getenv(INDEX_ENVIRONMENT);
143-
if (!git_index_file) {
144-
git_index_file = xmalloc(strlen(git_dir) + 7);
145-
sprintf(git_index_file, "%s/index", git_dir);
146-
}
147-
git_graft_file = getenv(GRAFT_ENVIRONMENT);
148-
if (!git_graft_file)
149-
git_graft_file = git_pathdup("info/grafts");
143+
git_object_dir = git_path_from_env(DB_ENVIRONMENT, "objects");
144+
git_index_file = git_path_from_env(INDEX_ENVIRONMENT, "index");
145+
git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, "info/grafts");
150146
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
151147
check_replace_refs = 0;
152148
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));

http-push.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,13 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
763763

764764
if (tag_closed && ctx->cdata) {
765765
if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
766-
lock->owner = xmalloc(strlen(ctx->cdata) + 1);
767-
strcpy(lock->owner, ctx->cdata);
766+
lock->owner = xstrdup(ctx->cdata);
768767
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
769768
const char *arg;
770769
if (skip_prefix(ctx->cdata, "Second-", &arg))
771770
lock->timeout = strtol(arg, NULL, 10);
772771
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
773-
lock->token = xmalloc(strlen(ctx->cdata) + 1);
774-
strcpy(lock->token, ctx->cdata);
772+
lock->token = xstrdup(ctx->cdata);
775773

776774
git_SHA1_Init(&sha_ctx);
777775
git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
@@ -852,8 +850,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
852850
struct xml_ctx ctx;
853851
char *escaped;
854852

855-
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
856-
sprintf(url, "%s%s", repo->url, path);
853+
url = xstrfmt("%s%s", repo->url, path);
857854

858855
/* Make sure leading directories exist for the remote ref */
859856
ep = strchr(url + strlen(repo->url) + 1, '/');
@@ -1125,7 +1122,7 @@ static void remote_ls(const char *path, int flags,
11251122
void (*userFunc)(struct remote_ls_ctx *ls),
11261123
void *userData)
11271124
{
1128-
char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1125+
char *url = xstrfmt("%s%s", repo->url, path);
11291126
struct active_request_slot *slot;
11301127
struct slot_results results;
11311128
struct strbuf in_buffer = STRBUF_INIT;
@@ -1141,8 +1138,6 @@ static void remote_ls(const char *path, int flags,
11411138
ls.userData = userData;
11421139
ls.userFunc = userFunc;
11431140

1144-
sprintf(url, "%s%s", repo->url, path);
1145-
11461141
strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
11471142

11481143
dav_headers = curl_slist_append(dav_headers, "Depth: 1");
@@ -1544,10 +1539,9 @@ static void update_remote_info_refs(struct remote_lock *lock)
15441539

15451540
static int remote_exists(const char *path)
15461541
{
1547-
char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1542+
char *url = xstrfmt("%s%s", repo->url, path);
15481543
int ret;
15491544

1550-
sprintf(url, "%s%s", repo->url, path);
15511545

15521546
switch (http_get_strbuf(url, NULL, NULL)) {
15531547
case HTTP_OK:
@@ -1567,13 +1561,10 @@ static int remote_exists(const char *path)
15671561

15681562
static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
15691563
{
1570-
char *url;
1564+
char *url = xstrfmt("%s%s", repo->url, path);
15711565
struct strbuf buffer = STRBUF_INIT;
15721566
const char *name;
15731567

1574-
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1575-
sprintf(url, "%s%s", repo->url, path);
1576-
15771568
if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
15781569
die("Couldn't get %s for remote symref\n%s", url,
15791570
curl_errorstr);
@@ -1682,8 +1673,7 @@ static int delete_remote_branch(const char *pattern, int force)
16821673
fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
16831674
if (dry_run)
16841675
return 0;
1685-
url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1686-
sprintf(url, "%s%s", repo->url, remote_ref->name);
1676+
url = xstrfmt("%s%s", repo->url, remote_ref->name);
16871677
slot = get_active_slot();
16881678
slot->results = &results;
16891679
curl_setup_http_get(slot->curl, url, DAV_DELETE);

http-walker.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
341341
if (walker->get_verbosely)
342342
fprintf(stderr, "Getting alternates list for %s\n", base);
343343

344-
url = xmalloc(strlen(base) + 31);
345-
sprintf(url, "%s/objects/info/http-alternates", base);
344+
url = xstrfmt("%s/objects/info/http-alternates", base);
346345

347346
/*
348347
* Use a callback to process the result, since another request
@@ -566,8 +565,7 @@ struct walker *get_http_walker(const char *url)
566565
struct walker *walker = xmalloc(sizeof(struct walker));
567566

568567
data->alt = xmalloc(sizeof(*data->alt));
569-
data->alt->base = xmalloc(strlen(url) + 1);
570-
strcpy(data->alt->base, url);
568+
data->alt->base = xstrdup(url);
571569
for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
572570
*s = 0;
573571

match-trees.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,12 @@ static void match_trees(const unsigned char *hash1,
140140
goto next;
141141
score = score_trees(elem, hash2);
142142
if (*best_score < score) {
143-
char *newpath;
144-
newpath = xmalloc(strlen(base) + strlen(path) + 1);
145-
sprintf(newpath, "%s%s", base, path);
146143
free(*best_match);
147-
*best_match = newpath;
144+
*best_match = xstrfmt("%s%s", base, path);
148145
*best_score = score;
149146
}
150147
if (recurse_limit) {
151-
char *newbase;
152-
newbase = xmalloc(strlen(base) + strlen(path) + 2);
153-
sprintf(newbase, "%s%s/", base, path);
148+
char *newbase = xstrfmt("%s%s/", base, path);
154149
match_trees(elem, hash2, best_score, best_match,
155150
newbase, recurse_limit - 1);
156151
free(newbase);

0 commit comments

Comments
 (0)