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>
This commit is contained in:
Masami Hiramatsu 2016-05-10 14:47:26 +09:00 коммит произвёл Arnaldo Carvalho de Melo
Родитель b72ca40390
Коммит 70a6898fdc
3 изменённых файлов: 26 добавлений и 20 удалений

Просмотреть файл

@ -309,9 +309,11 @@ static int handle_alias(int *argcp, const char ***argv)
if (*argcp > 1) { if (*argcp > 1) {
struct strbuf buf; struct strbuf buf;
strbuf_init(&buf, PATH_MAX); if (strbuf_init(&buf, PATH_MAX) < 0 ||
strbuf_addstr(&buf, alias_string); strbuf_addstr(&buf, alias_string) < 0 ||
sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); sq_quote_argv(&buf, (*argv) + 1,
PATH_MAX) < 0)
die("Failed to allocate memory.");
free(alias_string); free(alias_string);
alias_string = buf.buf; alias_string = buf.buf;
} }

Просмотреть файл

@ -17,38 +17,42 @@ static inline int need_bs_quote(char c)
return (c == '\'' || c == '!'); return (c == '\'' || c == '!');
} }
static void sq_quote_buf(struct strbuf *dst, const char *src) static int sq_quote_buf(struct strbuf *dst, const char *src)
{ {
char *to_free = NULL; char *to_free = NULL;
int ret;
if (dst->buf == src) if (dst->buf == src)
to_free = strbuf_detach(dst, NULL); to_free = strbuf_detach(dst, NULL);
strbuf_addch(dst, '\''); ret = strbuf_addch(dst, '\'');
while (*src) { while (!ret && *src) {
size_t len = strcspn(src, "'!"); size_t len = strcspn(src, "'!");
strbuf_add(dst, src, len); ret = strbuf_add(dst, src, len);
src += len; src += len;
while (need_bs_quote(*src)) { while (!ret && need_bs_quote(*src))
strbuf_addstr(dst, "'\\"); ret = strbuf_addf(dst, "'\\%c\'", *src++);
strbuf_addch(dst, *src++);
strbuf_addch(dst, '\'');
}
} }
strbuf_addch(dst, '\''); if (!ret)
ret = strbuf_addch(dst, '\'');
free(to_free); free(to_free);
return ret;
} }
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
{ {
int i; int i, ret;
/* Copy into destination buffer. */ /* Copy into destination buffer. */
strbuf_grow(dst, 255); ret = strbuf_grow(dst, 255);
for (i = 0; argv[i]; ++i) { for (i = 0; !ret && argv[i]; ++i) {
strbuf_addch(dst, ' '); ret = strbuf_addch(dst, ' ');
sq_quote_buf(dst, argv[i]); if (ret)
break;
ret = sq_quote_buf(dst, argv[i]);
if (maxlen && dst->len > maxlen) if (maxlen && dst->len > maxlen)
die("Too many or long arguments"); die("Too many or long arguments");
} }
return ret;
} }

Просмотреть файл

@ -24,6 +24,6 @@
* sq_quote() in a real application. * sq_quote() in a real application.
*/ */
void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen); int sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
#endif /* __PERF_QUOTE_H */ #endif /* __PERF_QUOTE_H */