提交 43bb66ae 编写于 作者: J Jeff King 提交者: Junio C Hamano

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: NJeff King <peff@peff.net>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 d4b3d11a
......@@ -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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册