aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
authorMasami Hiramatsu <mhiramat@kernel.org>2016-05-10 01:47:26 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-05-10 10:56:52 -0400
commit70a6898fdc11272249622f77b034f47f1e9adb35 (patch)
treecafe8a11fbe568c8816f42041e3bacaafa0650e2 /tools/perf
parentb72ca4039099e953f1ea2dbd58c201b14feb6605 (diff)
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>
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/perf.c8
-rw-r--r--tools/perf/util/quote.c36
-rw-r--r--tools/perf/util/quote.h2
3 files changed, 26 insertions, 20 deletions
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 83ffe7cd7330..797000842d40 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -309,9 +309,11 @@ static int handle_alias(int *argcp, const char ***argv)
309 if (*argcp > 1) { 309 if (*argcp > 1) {
310 struct strbuf buf; 310 struct strbuf buf;
311 311
312 strbuf_init(&buf, PATH_MAX); 312 if (strbuf_init(&buf, PATH_MAX) < 0 ||
313 strbuf_addstr(&buf, alias_string); 313 strbuf_addstr(&buf, alias_string) < 0 ||
314 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); 314 sq_quote_argv(&buf, (*argv) + 1,
315 PATH_MAX) < 0)
316 die("Failed to allocate memory.");
315 free(alias_string); 317 free(alias_string);
316 alias_string = buf.buf; 318 alias_string = buf.buf;
317 } 319 }
diff --git a/tools/perf/util/quote.c b/tools/perf/util/quote.c
index 01f03242b86a..c6d4ee2de752 100644
--- a/tools/perf/util/quote.c
+++ b/tools/perf/util/quote.c
@@ -17,38 +17,42 @@ static inline int need_bs_quote(char c)
17 return (c == '\'' || c == '!'); 17 return (c == '\'' || c == '!');
18} 18}
19 19
20static void sq_quote_buf(struct strbuf *dst, const char *src) 20static int sq_quote_buf(struct strbuf *dst, const char *src)
21{ 21{
22 char *to_free = NULL; 22 char *to_free = NULL;
23 int ret;
23 24
24 if (dst->buf == src) 25 if (dst->buf == src)
25 to_free = strbuf_detach(dst, NULL); 26 to_free = strbuf_detach(dst, NULL);
26 27
27 strbuf_addch(dst, '\''); 28 ret = strbuf_addch(dst, '\'');
28 while (*src) { 29 while (!ret && *src) {
29 size_t len = strcspn(src, "'!"); 30 size_t len = strcspn(src, "'!");
30 strbuf_add(dst, src, len); 31 ret = strbuf_add(dst, src, len);
31 src += len; 32 src += len;
32 while (need_bs_quote(*src)) { 33 while (!ret && need_bs_quote(*src))
33 strbuf_addstr(dst, "'\\"); 34 ret = strbuf_addf(dst, "'\\%c\'", *src++);
34 strbuf_addch(dst, *src++);
35 strbuf_addch(dst, '\'');
36 }
37 } 35 }
38 strbuf_addch(dst, '\''); 36 if (!ret)
37 ret = strbuf_addch(dst, '\'');
39 free(to_free); 38 free(to_free);
39
40 return ret;
40} 41}
41 42
42void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) 43int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
43{ 44{
44 int i; 45 int i, ret;
45 46
46 /* Copy into destination buffer. */ 47 /* Copy into destination buffer. */
47 strbuf_grow(dst, 255); 48 ret = strbuf_grow(dst, 255);
48 for (i = 0; argv[i]; ++i) { 49 for (i = 0; !ret && argv[i]; ++i) {
49 strbuf_addch(dst, ' '); 50 ret = strbuf_addch(dst, ' ');
50 sq_quote_buf(dst, argv[i]); 51 if (ret)
52 break;
53 ret = sq_quote_buf(dst, argv[i]);
51 if (maxlen && dst->len > maxlen) 54 if (maxlen && dst->len > maxlen)
52 die("Too many or long arguments"); 55 die("Too many or long arguments");
53 } 56 }
57 return ret;
54} 58}
diff --git a/tools/perf/util/quote.h b/tools/perf/util/quote.h
index 3340c9c4a6ca..e1ec19146fb0 100644
--- a/tools/perf/util/quote.h
+++ b/tools/perf/util/quote.h
@@ -24,6 +24,6 @@
24 * sq_quote() in a real application. 24 * sq_quote() in a real application.
25 */ 25 */
26 26
27void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen); 27int sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
28 28
29#endif /* __PERF_QUOTE_H */ 29#endif /* __PERF_QUOTE_H */