Skip to content

Commit 9d2685b

Browse files
committed
quote: handle null and empty strings in sq_quote_buf_pretty()
In [1], Junio described a potential bug in sq_quote_buf_pretty() when the arg is a zero length string. It should emit quote-quote rather than nothing. This commit teaches sq_quote_buf_pretty to emit '' for null and empty strings. [1] https://public-inbox.org/git/[email protected]/T/#m9e33936067ec2066f675aa63133a2486efd415fd Signed-off-by: Garima Singh <[email protected]>
1 parent 5fa0f52 commit 9d2685b

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
728728
TEST_BUILTINS_OBJS += test-path-utils.o
729729
TEST_BUILTINS_OBJS += test-pkt-line.o
730730
TEST_BUILTINS_OBJS += test-prio-queue.o
731+
TEST_BUILTINS_OBJS += test-quote.o
731732
TEST_BUILTINS_OBJS += test-reach.o
732733
TEST_BUILTINS_OBJS += test-read-cache.o
733734
TEST_BUILTINS_OBJS += test-read-midx.o

quote.c

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

51+
/*
52+
* In case of null or empty tokens, add a '' to ensure we
53+
* don't inadvertently drop those tokens
54+
*/
55+
if (!src || !*src) {
56+
strbuf_addstr(dst, "''");
57+
return;
58+
}
59+
5160
for (p = src; *p; p++) {
5261
if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
5362
sq_quote_buf(dst, src);

t/helper/test-quote.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "test-tool.h"
2+
#include "quote.h"
3+
#include "strbuf.h"
4+
#include "string.h"
5+
6+
int cmd__quote_buf_pretty(int argc, const char **argv)
7+
{
8+
struct strbuf buf_payload = STRBUF_INIT;
9+
10+
if (!argv[1]) {
11+
strbuf_release(&buf_payload);
12+
die("missing input string");
13+
}
14+
15+
if (!strcmp(argv[1], "nullString"))
16+
sq_quote_buf_pretty(&buf_payload, NULL);
17+
18+
else if (!*argv[1])
19+
sq_quote_buf_pretty(&buf_payload, "");
20+
21+
else
22+
sq_quote_buf_pretty(&buf_payload, argv[1]);
23+
24+
/* Wrap the results in [] to make the test script more readable */
25+
printf("[%s]\n", buf_payload.buf);
26+
strbuf_release(&buf_payload);
27+
return 0;
28+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static struct test_cmd cmds[] = {
5656
{ "sha1-array", cmd__sha1_array },
5757
{ "sha256", cmd__sha256 },
5858
{ "sigchain", cmd__sigchain },
59+
{ "quote-buf-pretty", cmd__quote_buf_pretty },
5960
{ "strcmp-offset", cmd__strcmp_offset },
6061
{ "string-list", cmd__string_list },
6162
{ "submodule-config", cmd__submodule_config },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int cmd__sha1(int argc, const char **argv);
4646
int cmd__sha1_array(int argc, const char **argv);
4747
int cmd__sha256(int argc, const char **argv);
4848
int cmd__sigchain(int argc, const char **argv);
49+
int cmd__quote_buf_pretty(int argc, const char **argv);
4950
int cmd__strcmp_offset(int argc, const char **argv);
5051
int cmd__string_list(int argc, const char **argv);
5152
int cmd__submodule_config(int argc, const char **argv);

t/t0091-quote.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
3+
test_description='Testing the sq_quote_buf_pretty method in quote.c'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'test method without input string' '
7+
test_must_fail test-tool quote-buf-pretty
8+
'
9+
10+
test_expect_success 'test null string' '
11+
cat >expect <<-EOF &&
12+
'[\'\']'
13+
EOF
14+
test-tool quote-buf-pretty nullString >actual &&
15+
test_cmp expect actual
16+
'
17+
18+
test_expect_success 'test empty string' '
19+
cat >expect <<-EOF &&
20+
'[\'\']'
21+
EOF
22+
test-tool quote-buf-pretty "" >actual &&
23+
test_cmp expect actual
24+
'
25+
26+
test_expect_success 'string without any punctuation' '
27+
cat >expect <<-EOF &&
28+
[testString]
29+
EOF
30+
test-tool quote-buf-pretty testString >actual &&
31+
test_cmp expect actual
32+
'
33+
34+
test_expect_success 'string with punctuation that do not require special quotes' '
35+
cat >expect <<-EOF &&
36+
[test+String]
37+
EOF
38+
test-tool quote-buf-pretty test+String >actual &&
39+
test_cmp expect actual
40+
'
41+
42+
test_expect_success 'string with punctuation that requires special quotes' '
43+
cat >expect <<-EOF &&
44+
'[\'test~String\']'
45+
EOF
46+
test-tool quote-buf-pretty test~String >actual &&
47+
test_cmp expect actual
48+
'
49+
50+
test_expect_success 'string with punctuation that requires special quotes' '
51+
cat >expect <<-EOF &&
52+
'[\'test\'\\!\'String\']'
53+
EOF
54+
test-tool quote-buf-pretty test!String >actual &&
55+
test_cmp expect actual
56+
'
57+
58+
test_done

0 commit comments

Comments
 (0)