Skip to content

Commit 0bb443f

Browse files
peffgitster
authored andcommitted
trace: use strbuf for quote_crnl output
When we output GIT_TRACE_SETUP paths, we quote any meta-characters. But our buffer to hold the result is only PATH_MAX bytes, and we could double the size of the input path (if every character needs quoting). We could use a 2*PATH_MAX buffer, if we assume the input will never be more than PATH_MAX. But it's easier still to just switch to a strbuf and not worry about whether the input can exceed PATH_MAX or not. The original copied the "p2" pointer to "p1", advancing both. Since this gets rid of "p1", let's also drop "p2", whose name is now confusing. We can just advance the original "path" pointer. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1d895f1 commit 0bb443f

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

trace.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,25 +277,24 @@ void trace_performance_fl(const char *file, int line, uint64_t nanos,
277277

278278
static const char *quote_crnl(const char *path)
279279
{
280-
static char new_path[PATH_MAX];
281-
const char *p2 = path;
282-
char *p1 = new_path;
280+
static struct strbuf new_path = STRBUF_INIT;
283281

284282
if (!path)
285283
return NULL;
286284

287-
while (*p2) {
288-
switch (*p2) {
289-
case '\\': *p1++ = '\\'; *p1++ = '\\'; break;
290-
case '\n': *p1++ = '\\'; *p1++ = 'n'; break;
291-
case '\r': *p1++ = '\\'; *p1++ = 'r'; break;
285+
strbuf_reset(&new_path);
286+
287+
while (*path) {
288+
switch (*path) {
289+
case '\\': strbuf_addstr(&new_path, "\\\\"); break;
290+
case '\n': strbuf_addstr(&new_path, "\\n"); break;
291+
case '\r': strbuf_addstr(&new_path, "\\r"); break;
292292
default:
293-
*p1++ = *p2;
293+
strbuf_addch(&new_path, *path);
294294
}
295-
p2++;
295+
path++;
296296
}
297-
*p1 = '\0';
298-
return new_path;
297+
return new_path.buf;
299298
}
300299

301300
/* FIXME: move prefix to startup_info struct and get rid of this arg */

0 commit comments

Comments
 (0)