Skip to content

Commit b9a6859

Browse files
committed
quote: handle null 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 if they pass in a NULL. Reported by: Junio Hamano <[email protected]> Signed-off-by: Garima Singh <[email protected]>
1 parent 5fa0f52 commit b9a6859

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

quote.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ 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("BUG can't append a NULL token to the buffer");
54+
55+
/* In case of empty tokens, add a '' to ensure they
56+
* don't get inadvertently dropped.
57+
*/
58+
if (!*src) {
59+
strbuf_addstr(dst, "''");
60+
return;
61+
}
62+
5163
for (p = src; *p; p++) {
5264
if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
5365
sq_quote_buf(dst, src);

t/t0014-alias.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ 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+
cat >expect <<-EOF &&
42+
fatal: cannot change to '\''alias.foo=frotz foo '\'''\'' bar'\'': No such file or directory
43+
EOF
44+
test_expect_code 128 git -C "alias.foo=frotz foo '\'''\'' bar" foo 2>actual &&
45+
test_cmp expect actual
46+
'
47+
4048
test_done

0 commit comments

Comments
 (0)