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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 24, 2015 at 17:05 UTC
0bb443fdd2518600f88434e2aad8f515546ee707
1 file changed
+11
-12
trace.c
+11
-12
@@ -277,25 +277,24 @@ void trace_performance_fl(const char *file, int line, uint64_t nanos,
277
278
static const char *quote_crnl(const char *path)
279
{
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;
281
282
if (!path)
283
return NULL;
284
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;
292
default:
293
- *p1++ = *p2;
293
+ strbuf_addch(&new_path, *path);
294
}
295
- p2++;
295
+ path++;
296
}
297
- *p1 = '\0';
298
- return new_path;
297
+ return new_path.buf;
298
}
299
300
/* FIXME: move prefix to startup_info struct and get rid of this arg */