Skip to content

Commit 399fe02

Browse files
committed
quote: handle numm and empty strings in sq_quote_buf_pretty
The sq_quote_buf_pretty() function does not emit anything when the incoming string is empty, but the function is to accumulate command line arguments, properly quoted as necessary, and the right way to add an argument that is an empty string is to show it quoted, i.e. ''. We warn the caller with the BUG macro is they pass in a NULL. Reported by: Junio Hamano <[email protected]> Signed-off-by: Garima Singh <[email protected]>
1 parent 5fa0f52 commit 399fe02

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

quote.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
4848
static const char ok_punct[] = "+,-./:=@_^";
4949
const char *p;
5050

51+
/* In case of null tokens, warn the user of the BUG in their call. */
52+
if (!src)
53+
BUG("Cannot append a NULL token to the buffer");
54+
55+
/* Avoid dropping a zero-length token by adding '' */
56+
if (!*src) {
57+
strbuf_addstr(dst, "''");
58+
return;
59+
}
60+
5161
for (p = src; *p; p++) {
5262
if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
5363
sq_quote_buf(dst, src);

t/t0014-alias.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ test_expect_success 'looping aliases - internal execution' '
3737
# test_i18ngrep "^fatal: alias loop detected: expansion of" output
3838
#'
3939

40+
test_expect_success 'run-command parses empty args properly, using sq_quote_buf_pretty' '
41+
GIT_TRACE=1 git frotz a "" b " " c 2>&1 |
42+
sed -ne "/run_command:/s/.*trace: run_command: //p" >actual &&
43+
echo "git-frotz a '\'''\'' b '\'' '\'' c" >expect &&
44+
test_cmp expect actual
45+
'
46+
4047
test_done

0 commit comments

Comments
 (0)