Skip to content

Commit 75faa45

Browse files
peffgitster
authored andcommitted
replace trivial malloc + sprintf / strcpy calls with xstrfmt
It's a common pattern to do: foo = xmalloc(strlen(one) + strlen(two) + 1 + 1); sprintf(foo, "%s %s", one, two); (or possibly some variant with strcpy()s or a more complicated length computation). We can switch these to use xstrfmt, which is shorter, involves less error-prone manual computation, and removes many sprintf and strcpy calls which make it harder to audit the code for real buffer overflows. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7115a3 commit 75faa45

File tree

9 files changed

+20
-48
lines changed

9 files changed

+20
-48
lines changed

builtin/apply.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,7 @@ static char *find_name_common(const char *line, const char *def,
698698
}
699699

700700
if (root) {
701-
char *ret = xmalloc(root_len + len + 1);
702-
strcpy(ret, root);
703-
memcpy(ret + root_len, start, len);
704-
ret[root_len + len] = '\0';
701+
char *ret = xstrfmt("%s%.*s", root, len, start);
705702
return squash_slash(ret);
706703
}
707704

builtin/ls-remote.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
9393
if (argv[i]) {
9494
int j;
9595
pattern = xcalloc(argc - i + 1, sizeof(const char *));
96-
for (j = i; j < argc; j++) {
97-
int len = strlen(argv[j]);
98-
char *p = xmalloc(len + 3);
99-
sprintf(p, "*/%s", argv[j]);
100-
pattern[j - i] = p;
101-
}
96+
for (j = i; j < argc; j++)
97+
pattern[j - i] = xstrfmt("*/%s", argv[j]);
10298
}
10399
remote = remote_get(dest);
104100
if (!remote) {

builtin/name-rev.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,16 @@ static void name_rev(struct commit *commit,
5656
parents = parents->next, parent_number++) {
5757
if (parent_number > 1) {
5858
int len = strlen(tip_name);
59-
char *new_name = xmalloc(len +
60-
1 + decimal_length(generation) + /* ~<n> */
61-
1 + 2 + /* ^NN */
62-
1);
59+
char *new_name;
6360

6461
if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
6562
len -= 2;
6663
if (generation > 0)
67-
sprintf(new_name, "%.*s~%d^%d", len, tip_name,
68-
generation, parent_number);
64+
new_name = xstrfmt("%.*s~%d^%d", len, tip_name,
65+
generation, parent_number);
6966
else
70-
sprintf(new_name, "%.*s^%d", len, tip_name,
71-
parent_number);
67+
new_name = xstrfmt("%.*s^%d", len, tip_name,
68+
parent_number);
7269

7370
name_rev(parents->item, new_name, 0,
7471
distance + MERGE_TRAVERSAL_WEIGHT, 0);

environment.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,8 @@ static char *git_path_from_env(const char *envvar, const char *git_dir,
143143
const char *path, int *fromenv)
144144
{
145145
const char *value = getenv(envvar);
146-
if (!value) {
147-
char *buf = xmalloc(strlen(git_dir) + strlen(path) + 2);
148-
sprintf(buf, "%s/%s", git_dir, path);
149-
return buf;
150-
}
146+
if (!value)
147+
return xstrfmt("%s/%s", git_dir, path);
151148
if (fromenv)
152149
*fromenv = 1;
153150
return xstrdup(value);

imap-send.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,8 @@ static char *cram(const char *challenge_64, const char *user, const char *pass)
889889
}
890890

891891
/* response: "<user> <digest in hex>" */
892-
resp_len = strlen(user) + 1 + strlen(hex) + 1;
893-
response = xmalloc(resp_len);
894-
sprintf(response, "%s %s", user, hex);
892+
response = xstrfmt("%s %s", user, hex);
893+
resp_len = strlen(response) + 1;
895894

896895
response_64 = xmalloc(ENCODED_SIZE(resp_len) + 1);
897896
encoded_len = EVP_EncodeBlock((unsigned char *)response_64,

reflog-walk.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
5656
}
5757
}
5858
if (reflogs->nr == 0) {
59-
int len = strlen(ref);
60-
char *refname = xmalloc(len + 12);
61-
sprintf(refname, "refs/%s", ref);
59+
char *refname = xstrfmt("refs/%s", ref);
6260
for_each_reflog_ent(refname, read_one_reflog, reflogs);
6361
if (reflogs->nr == 0) {
64-
sprintf(refname, "refs/heads/%s", ref);
62+
free(refname);
63+
refname = xstrfmt("refs/heads/%s", ref);
6564
for_each_reflog_ent(refname, read_one_reflog, reflogs);
6665
}
6766
free(refname);

remote.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ static int valid_remote(const struct remote *remote)
6565
static const char *alias_url(const char *url, struct rewrites *r)
6666
{
6767
int i, j;
68-
char *ret;
6968
struct counted_string *longest;
7069
int longest_i;
7170

@@ -86,11 +85,7 @@ static const char *alias_url(const char *url, struct rewrites *r)
8685
if (!longest)
8786
return url;
8887

89-
ret = xmalloc(r->rewrite[longest_i]->baselen +
90-
(strlen(url) - longest->len) + 1);
91-
strcpy(ret, r->rewrite[longest_i]->base);
92-
strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
93-
return ret;
88+
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
9489
}
9590

9691
static void add_push_refspec(struct remote *remote, const char *ref)

setup.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ char *prefix_path_gently(const char *prefix, int len,
9999
return NULL;
100100
}
101101
} else {
102-
sanitized = xmalloc(len + strlen(path) + 1);
103-
if (len)
104-
memcpy(sanitized, prefix, len);
105-
strcpy(sanitized + len, path);
102+
sanitized = xstrfmt("%.*s%s", len, prefix, path);
106103
if (remaining_prefix)
107104
*remaining_prefix = len;
108105
if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
@@ -468,11 +465,8 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
468465

469466
if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
470467
size_t pathlen = slash+1 - path;
471-
size_t dirlen = pathlen + len - 8;
472-
dir = xmalloc(dirlen + 1);
473-
strncpy(dir, path, pathlen);
474-
strncpy(dir + pathlen, buf + 8, len - 8);
475-
dir[dirlen] = '\0';
468+
dir = xstrfmt("%.*s%.*s", (int)pathlen, path,
469+
(int)(len - 8), buf + 8);
476470
free(buf);
477471
buf = dir;
478472
}

unpack-trees.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,9 +1350,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
13501350
* Then we need to make sure that we do not lose a locally
13511351
* present file that is not ignored.
13521352
*/
1353-
pathbuf = xmalloc(namelen + 2);
1354-
memcpy(pathbuf, ce->name, namelen);
1355-
strcpy(pathbuf+namelen, "/");
1353+
pathbuf = xstrfmt("%.*s/", namelen, ce->name);
13561354

13571355
memset(&d, 0, sizeof(d));
13581356
if (o->dir)

0 commit comments

Comments
 (0)