Skip to content

Commit fa3f60b

Browse files
peffgitster
authored andcommitted
use xstrfmt in favor of manual size calculations
In many parts of the code, we do an ugly and error-prone malloc like: const char *fmt = "something %s"; buf = xmalloc(strlen(foo) + 10 + 1); sprintf(buf, fmt, foo); This makes the code brittle, and if we ever get the allocation wrong, is a potential heap overflow. Let's instead favor xstrfmt, which handles the allocation automatically, and makes the code shorter and more readable. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 30a0ddb commit fa3f60b

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

remote.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ static struct branch *make_branch(const char *name, int len)
170170
{
171171
struct branch *ret;
172172
int i;
173-
char *refname;
174173

175174
for (i = 0; i < branches_nr; i++) {
176175
if (len ? (!strncmp(name, branches[i]->name, len) &&
@@ -186,10 +185,7 @@ static struct branch *make_branch(const char *name, int len)
186185
ret->name = xstrndup(name, len);
187186
else
188187
ret->name = xstrdup(name);
189-
refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
190-
strcpy(refname, "refs/heads/");
191-
strcpy(refname + strlen("refs/heads/"), ret->name);
192-
ret->refname = refname;
188+
ret->refname = xstrfmt("refs/heads/%s", ret->name);
193189

194190
return ret;
195191
}

unpack-trees.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
5656
int i;
5757
const char **msgs = opts->msgs;
5858
const char *msg;
59-
char *tmp;
6059
const char *cmd2 = strcmp(cmd, "checkout") ? cmd : "switch branches";
60+
6161
if (advice_commit_before_merge)
6262
msg = "Your local changes to the following files would be overwritten by %s:\n%%s"
6363
"Please, commit your changes or stash them before you can %s.";
6464
else
6565
msg = "Your local changes to the following files would be overwritten by %s:\n%%s";
66-
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 2);
67-
sprintf(tmp, msg, cmd, cmd2);
68-
msgs[ERROR_WOULD_OVERWRITE] = tmp;
69-
msgs[ERROR_NOT_UPTODATE_FILE] = tmp;
66+
msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
67+
xstrfmt(msg, cmd, cmd2);
7068

7169
msgs[ERROR_NOT_UPTODATE_DIR] =
7270
"Updating the following directories would lose untracked files in it:\n%s";
@@ -76,12 +74,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
7674
"Please move or remove them before you can %s.";
7775
else
7876
msg = "The following untracked working tree files would be %s by %s:\n%%s";
79-
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("removed") + strlen(cmd2) - 4);
80-
sprintf(tmp, msg, "removed", cmd, cmd2);
81-
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = tmp;
82-
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("overwritten") + strlen(cmd2) - 4);
83-
sprintf(tmp, msg, "overwritten", cmd, cmd2);
84-
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = tmp;
77+
78+
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = xstrfmt(msg, "removed", cmd, cmd2);
79+
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = xstrfmt(msg, "overwritten", cmd, cmd2);
8580

8681
/*
8782
* Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we

0 commit comments

Comments
 (0)