Skip to content

Commit

Permalink
remove_leading_path: use a strbuf for internal storage
Browse files Browse the repository at this point in the history
This function strcpy's directly into a PATH_MAX-sized
buffer. There's only one caller, which feeds the git_dir into
it, so it's not easy to trigger in practice (even if you fed
a large $GIT_DIR through the environment or .git file, it
would have to actually exist and be accessible on the
filesystem to get to this point). We can fix it by moving to
a strbuf.

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 Oct 5, 2015
1 parent e9ba678 commit 4635768
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ const char *relative_path(const char *in, const char *prefix,
*/
const char *remove_leading_path(const char *in, const char *prefix)
{
static char buf[PATH_MAX + 1];
static struct strbuf buf = STRBUF_INIT;
int i = 0, j = 0;

if (!prefix || !prefix[0])
Expand Down Expand Up @@ -661,11 +661,13 @@ const char *remove_leading_path(const char *in, const char *prefix)
return in;
while (is_dir_sep(in[j]))
j++;

strbuf_reset(&buf);
if (!in[j])
strcpy(buf, ".");
strbuf_addstr(&buf, ".");
else
strcpy(buf, in + j);
return buf;
strbuf_addstr(&buf, in + j);
return buf.buf;
}

/*
Expand Down

0 comments on commit 4635768

Please sign in to comment.