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

perf tools: Make alias handler to check return value of strbuf

Make alias handler and sq_quote_argv to check the return value of strbuf
APIs.

In sq_quote_argv() calls die(), but this fix handles strbuf failure as a
special case and returns to caller, since the caller - handle_alias()
also has to check the return value of other strbuf APIs and those checks
can be merged to one if() statement.

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/20160510054725.6158.84597.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Masami Hiramatsu and committed by
Arnaldo Carvalho de Melo
70a6898f b72ca403

+26 -20
+5 -3
tools/perf/perf.c
··· 309 309 if (*argcp > 1) { 310 310 struct strbuf buf; 311 311 312 - strbuf_init(&buf, PATH_MAX); 313 - strbuf_addstr(&buf, alias_string); 314 - sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); 312 + if (strbuf_init(&buf, PATH_MAX) < 0 || 313 + strbuf_addstr(&buf, alias_string) < 0 || 314 + sq_quote_argv(&buf, (*argv) + 1, 315 + PATH_MAX) < 0) 316 + die("Failed to allocate memory."); 315 317 free(alias_string); 316 318 alias_string = buf.buf; 317 319 }
+20 -16
tools/perf/util/quote.c
··· 17 17 return (c == '\'' || c == '!'); 18 18 } 19 19 20 - static void sq_quote_buf(struct strbuf *dst, const char *src) 20 + static int sq_quote_buf(struct strbuf *dst, const char *src) 21 21 { 22 22 char *to_free = NULL; 23 + int ret; 23 24 24 25 if (dst->buf == src) 25 26 to_free = strbuf_detach(dst, NULL); 26 27 27 - strbuf_addch(dst, '\''); 28 - while (*src) { 28 + ret = strbuf_addch(dst, '\''); 29 + while (!ret && *src) { 29 30 size_t len = strcspn(src, "'!"); 30 - strbuf_add(dst, src, len); 31 + ret = strbuf_add(dst, src, len); 31 32 src += len; 32 - while (need_bs_quote(*src)) { 33 - strbuf_addstr(dst, "'\\"); 34 - strbuf_addch(dst, *src++); 35 - strbuf_addch(dst, '\''); 36 - } 33 + while (!ret && need_bs_quote(*src)) 34 + ret = strbuf_addf(dst, "'\\%c\'", *src++); 37 35 } 38 - strbuf_addch(dst, '\''); 36 + if (!ret) 37 + ret = strbuf_addch(dst, '\''); 39 38 free(to_free); 39 + 40 + return ret; 40 41 } 41 42 42 - void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) 43 + int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) 43 44 { 44 - int i; 45 + int i, ret; 45 46 46 47 /* Copy into destination buffer. */ 47 - strbuf_grow(dst, 255); 48 - for (i = 0; argv[i]; ++i) { 49 - strbuf_addch(dst, ' '); 50 - sq_quote_buf(dst, argv[i]); 48 + ret = strbuf_grow(dst, 255); 49 + for (i = 0; !ret && argv[i]; ++i) { 50 + ret = strbuf_addch(dst, ' '); 51 + if (ret) 52 + break; 53 + ret = sq_quote_buf(dst, argv[i]); 51 54 if (maxlen && dst->len > maxlen) 52 55 die("Too many or long arguments"); 53 56 } 57 + return ret; 54 58 }
+1 -1
tools/perf/util/quote.h
··· 24 24 * sq_quote() in a real application. 25 25 */ 26 26 27 - void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen); 27 + int sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen); 28 28 29 29 #endif /* __PERF_QUOTE_H */