Skip to content

Commit 43bb66a

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4b3d11 commit 43bb66a

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

sha1_name.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,8 +1293,7 @@ static void diagnose_invalid_index_path(int stage,
12931293
const struct cache_entry *ce;
12941294
int pos;
12951295
unsigned namelen = strlen(filename);
1296-
unsigned fullnamelen;
1297-
char *fullname;
1296+
struct strbuf fullname = STRBUF_INIT;
12981297

12991298
if (!prefix)
13001299
prefix = "";
@@ -1314,21 +1313,19 @@ static void diagnose_invalid_index_path(int stage,
13141313
}
13151314

13161315
/* 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);
13221319
if (pos < 0)
13231320
pos = -pos - 1;
13241321
if (pos < active_nr) {
13251322
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))
13281325
die("Path '%s' is in the index, but not '%s'.\n"
13291326
"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,
13321329
ce_stage(ce), filename);
13331330
}
13341331

@@ -1338,7 +1335,7 @@ static void diagnose_invalid_index_path(int stage,
13381335
die("Path '%s' does not exist (neither on disk nor in the index).",
13391336
filename);
13401337

1341-
free(fullname);
1338+
strbuf_release(&fullname);
13421339
}
13431340

13441341

0 commit comments

Comments
 (0)