forked from Minki/linux
70a6898fdc
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>
59 lines
1.3 KiB
C
59 lines
1.3 KiB
C
#include "cache.h"
|
|
#include "quote.h"
|
|
|
|
/* Help to copy the thing properly quoted for the shell safety.
|
|
* any single quote is replaced with '\'', any exclamation point
|
|
* is replaced with '\!', and the whole thing is enclosed in a
|
|
*
|
|
* E.g.
|
|
* original sq_quote result
|
|
* name ==> name ==> 'name'
|
|
* a b ==> a b ==> 'a b'
|
|
* a'b ==> a'\''b ==> 'a'\''b'
|
|
* a!b ==> a'\!'b ==> 'a'\!'b'
|
|
*/
|
|
static inline int need_bs_quote(char c)
|
|
{
|
|
return (c == '\'' || c == '!');
|
|
}
|
|
|
|
static int sq_quote_buf(struct strbuf *dst, const char *src)
|
|
{
|
|
char *to_free = NULL;
|
|
int ret;
|
|
|
|
if (dst->buf == src)
|
|
to_free = strbuf_detach(dst, NULL);
|
|
|
|
ret = strbuf_addch(dst, '\'');
|
|
while (!ret && *src) {
|
|
size_t len = strcspn(src, "'!");
|
|
ret = strbuf_add(dst, src, len);
|
|
src += len;
|
|
while (!ret && need_bs_quote(*src))
|
|
ret = strbuf_addf(dst, "'\\%c\'", *src++);
|
|
}
|
|
if (!ret)
|
|
ret = strbuf_addch(dst, '\'');
|
|
free(to_free);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
|
|
{
|
|
int i, ret;
|
|
|
|
/* Copy into destination buffer. */
|
|
ret = strbuf_grow(dst, 255);
|
|
for (i = 0; !ret && argv[i]; ++i) {
|
|
ret = strbuf_addch(dst, ' ');
|
|
if (ret)
|
|
break;
|
|
ret = sq_quote_buf(dst, argv[i]);
|
|
if (maxlen && dst->len > maxlen)
|
|
die("Too many or long arguments");
|
|
}
|
|
return ret;
|
|
}
|