Skip to content

Commit 4635768

Browse files
peffgitster
authored andcommitted
remove_leading_path: use a strbuf for internal storage
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9ba678 commit 4635768

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

path.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ const char *relative_path(const char *in, const char *prefix,
632632
*/
633633
const char *remove_leading_path(const char *in, const char *prefix)
634634
{
635-
static char buf[PATH_MAX + 1];
635+
static struct strbuf buf = STRBUF_INIT;
636636
int i = 0, j = 0;
637637

638638
if (!prefix || !prefix[0])
@@ -661,11 +661,13 @@ const char *remove_leading_path(const char *in, const char *prefix)
661661
return in;
662662
while (is_dir_sep(in[j]))
663663
j++;
664+
665+
strbuf_reset(&buf);
664666
if (!in[j])
665-
strcpy(buf, ".");
667+
strbuf_addstr(&buf, ".");
666668
else
667-
strcpy(buf, in + j);
668-
return buf;
669+
strbuf_addstr(&buf, in + j);
670+
return buf.buf;
669671
}
670672

671673
/*

0 commit comments

Comments
 (0)