Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf pmu: Make pmu_formats_string to check return value of strbuf

Make pmu_formats_string() to check return value of strbuf APIs so that
it can detect errors in it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160510054744.6158.37810.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Masami Hiramatsu and committed by
Arnaldo Carvalho de Melo
11db4e29 642aadaa

+5 -5
+5 -5
tools/perf/util/pmu.c
··· 643 643 static char *pmu_formats_string(struct list_head *formats) 644 644 { 645 645 struct perf_pmu_format *format; 646 - char *str; 647 - struct strbuf buf; 646 + char *str = NULL; 647 + struct strbuf buf = STRBUF_INIT; 648 648 unsigned i = 0; 649 649 650 650 if (!formats) 651 651 return NULL; 652 652 653 - strbuf_init(&buf, 0); 654 653 /* sysfs exported terms */ 655 654 list_for_each_entry(format, formats, list) 656 - strbuf_addf(&buf, i++ ? ",%s" : "%s", 657 - format->name); 655 + if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 656 + goto error; 658 657 659 658 str = strbuf_detach(&buf, NULL); 659 + error: 660 660 strbuf_release(&buf); 661 661 662 662 return str;