Skip to content

Commit

Permalink
perf tools: Fix strbuf_addf() when the buffer needs to grow
Browse files Browse the repository at this point in the history
This was found during chasing down the header output regression.  The
strbuf_addf() was checking buffer length with a result of vscnprintf()
which cannot be greater than that of strbuf_avail().

Since numa topology and pmu mapping info in header were converted to use
strbuf, it sometimes caused uninteresting behaviors with the broken
strbuf.

Fix it by using vsnprintf() which returns desired output string length
regardless of the available buffer size and grow the buffer if needed.

Reported-by: Andrew Jones <[email protected]>
Tested-by: Andrew Jones <[email protected]>
Signed-off-by: Namhyung Kim <[email protected]>
Cc: Andrew Jones <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
namhyung authored and acmel committed Oct 30, 2012
1 parent 1234471 commit f787d95
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tools/perf/util/strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
if (!strbuf_avail(sb))
strbuf_grow(sb, 64);
va_start(ap, fmt);
len = vscnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
va_end(ap);
if (len < 0)
die("your vscnprintf is broken");
die("your vsnprintf is broken");
if (len > strbuf_avail(sb)) {
strbuf_grow(sb, len);
va_start(ap, fmt);
len = vscnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
va_end(ap);
if (len > strbuf_avail(sb)) {
die("this should not happen, your snprintf is broken");
die("this should not happen, your vsnprintf is broken");
}
}
strbuf_setlen(sb, sb->len + len);
Expand Down

0 comments on commit f787d95

Please sign in to comment.