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>

Jeff King committed Sep 24, 2015 at 17:07 UTC 43bb66ae0b2f1e379b73e3f0f697ca0d93e2a57b
1 file changed +9 -12
sha1_name.c
+9 -12
@@ -1293,8 +1293,7 @@ static void diagnose_invalid_index_path(int stage,
1293 const struct cache_entry *ce;
1294 int pos;
1295 unsigned namelen = strlen(filename);
1296 - unsigned fullnamelen;
1297 - char *fullname;
1296 + struct strbuf fullname = STRBUF_INIT;
1297
1298 if (!prefix)
1299 prefix = "";
@@ -1314,21 +1313,19 @@ static void diagnose_invalid_index_path(int stage,
1313 }
1314
1315 /* Confusion between relative and absolute filenames? */
1317 - fullnamelen = namelen + strlen(prefix);
1318 - fullname = xmalloc(fullnamelen + 1);
1319 - strcpy(fullname, prefix);
1320 - strcat(fullname, filename);
1321 - pos = cache_name_pos(fullname, fullnamelen);
1316 + strbuf_addstr(&fullname, prefix);
1317 + strbuf_addstr(&fullname, filename);
1318 + pos = cache_name_pos(fullname.buf, fullname.len);
1319 if (pos < 0)
1320 pos = -pos - 1;
1321 if (pos < active_nr) {
1322 ce = active_cache[pos];
1326 - if (ce_namelen(ce) == fullnamelen &&
1327 - !memcmp(ce->name, fullname, fullnamelen))
1323 + if (ce_namelen(ce) == fullname.len &&
1324 + !memcmp(ce->name, fullname.buf, fullname.len))
1325 die("Path '%s' is in the index, but not '%s'.\n"
1326 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1330 - fullname, filename,
1331 - ce_stage(ce), fullname,
1327 + fullname.buf, filename,
1328 + ce_stage(ce), fullname.buf,
1329 ce_stage(ce), filename);
1330 }
1331
@@ -1338,7 +1335,7 @@ static void diagnose_invalid_index_path(int stage,
1335 die("Path '%s' does not exist (neither on disk nor in the index).",
1336 filename);
1337
1341 - free(fullname);
1338 + strbuf_release(&fullname);
1339 }
1340
1341