Skip to contentSkip to conversation

Auto-quote config values in config.c:store_write_pair()

PATCH In review Brian Gernhardt 3 participants 4 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Auto-quote config values in config.c:store_write_pair()

Suggested by Jakub Narebski <[email protected]> on the list.

When we send a value to store_write_pair(), make sure that the value that gets read out matches the one passed in. This means that for any value that contains leading or trailing whitespace or any comment character (# and ;), we need to surround it in quotes.

---
 config.c               |   14 ++++++++++++++
 t/t1300-repo-config.sh |   17 +++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)
config.c +14 -0
index 5cbd130..e99681a 100644
@@ -496,11 +496,23 @@ static void store_write_section(int fd, const char* key)
 static void store_write_pair(int fd, const char* key, const char* value)
 {
 	int i;
+	int quote = 0;
+
+	/* Check to see if the value needs to be quoted. */
+	if (value[0] == ' ')
+		quote = 1;
+	for (i = 0; value[i]; i++)
+		if (value[i] == ';' || value[i] == '#')
+			quote = 1;
+	if (value[i-1] == ' ')
+		quote = 1;
 
 	write(fd, "\t", 1);
 	write(fd, key+store.baselen+1,
 		strlen(key+store.baselen+1));
 	write(fd, " = ", 3);
+	if (quote)
+		write(fd, "\"", 1);
 	for (i = 0; value[i]; i++)
 		switch (value[i]) {
 		case '\n': write(fd, "\\n", 2); break;
@@ -508,6 +520,8 @@ static void store_write_pair(int fd, const char* key, const char* value)
 		case '"': case '\\': write(fd, "\\", 1);
 		default: write(fd, value+i, 1);
 	}
+	if (quote)
+		write(fd, "\"", 1);
 	write(fd, "\n", 1);
 }
 
t/t1300-repo-config.sh +17 -0
index a29caa0..60acdd3 100755
@@ -401,5 +401,22 @@ test_expect_success numbers '
 	test z1048576 = "z$m"
 '
 
+rm .git/config
+
+git-repo-config quote.leading " test"
+git-repo-config quote.ending "test "
+git-repo-config quote.semicolon "test;test"
+git-repo-config quote.hash "test#test"
+
+cat > expect << EOF
+[quote]
+	leading = " test"
+	ending = "test "
+	semicolon = "test;test"
+	hash = "test#test"
+EOF
+
+test_expect_success 'quoting' 'cmp .git/config expect'
+
 test_done
 
Signature · 1 line
1.4.4.4.g4083
#2 lore raw
In reply to Brian Gernhardt

Hi,

config.c static void store_write_pair(int fd, const char* key, const char* value) · line 524

Quoted 11 lines · Brian Gernhardt
config.c +4 -0
+	if (quote)
+		write(fd, "\"", 1);
 	for (i = 0; value[i]; i++)
 		switch (value[i]) {
 		case '\n': write(fd, "\\n", 2); break;
@@ -508,6 +520,8 @@ static void store_write_pair(int fd, const char* key, const char* value)
 		case '"': case '\\': write(fd, "\\", 1);
 		default: write(fd, value+i, 1);
 	}
+	if (quote)
+		write(fd, "\"", 1);

AFAICS this misses the case where you have to quote a single '"'.

Ciao,
Dscho

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi,

[Cc'ing list]

Quoted 18 lines · Junio C Hamano
Quoted 15 lines · Johannes Schindelin

config.c static void store_write_pair(int fd, const char* key, const char* value) · line 524

Quoted 11 lines · Brian Gernhardt
config.c +4 -0
+	if (quote)
+		write(fd, "\"", 1);
 	for (i = 0; value[i]; i++)
 		switch (value[i]) {
 		case '\n': write(fd, "\\n", 2); break;
@@ -508,6 +520,8 @@ static void store_write_pair(int fd, const char* key, const char* value)
 		case '"': case '\\': write(fd, "\\", 1);
 		default: write(fd, value+i, 1);
 	}
+	if (quote)
+		write(fd, "\"", 1);

AFAICS this misses the case where you have to quote a single '"'.

Quoted 6 lines
	$ git-repo-config qu.ot 'ab"cd'
        $ git-repo-config --get qu.ot
        ab"cd
        $ tail -n 2 .git/config
        [qu]
        	ot = ab\"cd

Oooopps! Even more embarassing: you can see it in the diff (it is line 508 of the original code). And most embarassing: AFAICT I wrote the code myself!!!

So I shut up.

Ciao,
Dscho