1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-25 05:26:20 +02:00

diagnose_invalid_index_path: use strbuf to avoid strcpy/strcat

We dynamically allocate a buffer and then strcpy and strcat
into it. This isn't buggy, but we'd prefer to avoid these
suspicious functions.

This would be a good candidate for converstion to xstrfmt,
but we need to record the length for dealing with index
entries. A strbuf handles that for us.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2015-09-24 17:07:52 -04:00 committed by Junio C Hamano
parent d4b3d11a03
commit 43bb66ae0b

View File

@ -1293,8 +1293,7 @@ static void diagnose_invalid_index_path(int stage,
const struct cache_entry *ce;
int pos;
unsigned namelen = strlen(filename);
unsigned fullnamelen;
char *fullname;
struct strbuf fullname = STRBUF_INIT;
if (!prefix)
prefix = "";
@ -1314,21 +1313,19 @@ static void diagnose_invalid_index_path(int stage,
}
/* Confusion between relative and absolute filenames? */
fullnamelen = namelen + strlen(prefix);
fullname = xmalloc(fullnamelen + 1);
strcpy(fullname, prefix);
strcat(fullname, filename);
pos = cache_name_pos(fullname, fullnamelen);
strbuf_addstr(&fullname, prefix);
strbuf_addstr(&fullname, filename);
pos = cache_name_pos(fullname.buf, fullname.len);
if (pos < 0)
pos = -pos - 1;
if (pos < active_nr) {
ce = active_cache[pos];
if (ce_namelen(ce) == fullnamelen &&
!memcmp(ce->name, fullname, fullnamelen))
if (ce_namelen(ce) == fullname.len &&
!memcmp(ce->name, fullname.buf, fullname.len))
die("Path '%s' is in the index, but not '%s'.\n"
"Did you mean ':%d:%s' aka ':%d:./%s'?",
fullname, filename,
ce_stage(ce), fullname,
fullname.buf, filename,
ce_stage(ce), fullname.buf,
ce_stage(ce), filename);
}
@ -1338,7 +1335,7 @@ static void diagnose_invalid_index_path(int stage,
die("Path '%s' does not exist (neither on disk nor in the index).",
filename);
free(fullname);
strbuf_release(&fullname);
}