Skip to content

Commit

Permalink
use xstrfmt in favor of manual size calculations
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Jun 19, 2014
1 parent 30a0ddb commit fa3f60b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
6 changes: 1 addition & 5 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ static struct branch *make_branch(const char *name, int len)
{
struct branch *ret;
int i;
char *refname;

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

return ret;
}
Expand Down
17 changes: 6 additions & 11 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
int i;
const char **msgs = opts->msgs;
const char *msg;
char *tmp;
const char *cmd2 = strcmp(cmd, "checkout") ? cmd : "switch branches";

if (advice_commit_before_merge)
msg = "Your local changes to the following files would be overwritten by %s:\n%%s"
"Please, commit your changes or stash them before you can %s.";
else
msg = "Your local changes to the following files would be overwritten by %s:\n%%s";
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 2);
sprintf(tmp, msg, cmd, cmd2);
msgs[ERROR_WOULD_OVERWRITE] = tmp;
msgs[ERROR_NOT_UPTODATE_FILE] = tmp;
msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
xstrfmt(msg, cmd, cmd2);

msgs[ERROR_NOT_UPTODATE_DIR] =
"Updating the following directories would lose untracked files in it:\n%s";
Expand All @@ -76,12 +74,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
"Please move or remove them before you can %s.";
else
msg = "The following untracked working tree files would be %s by %s:\n%%s";
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("removed") + strlen(cmd2) - 4);
sprintf(tmp, msg, "removed", cmd, cmd2);
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = tmp;
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("overwritten") + strlen(cmd2) - 4);
sprintf(tmp, msg, "overwritten", cmd, cmd2);
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = tmp;

msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = xstrfmt(msg, "removed", cmd, cmd2);
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = xstrfmt(msg, "overwritten", cmd, cmd2);

/*
* Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
Expand Down

0 comments on commit fa3f60b

Please sign in to comment.