Skip to contentSkip to conversation

Fix off-by-one error: don't read the byte before a malloc'd buffer.

PATCH In review Jim Meyering 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix off-by-one error: don't read the byte before a malloc'd buffer.

* config.c (store_write_pair): Don't read value[-1].

---
 config.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
config.c +1 -1
index ed96213..6031b38 100644
@@ -652,7 +652,7 @@ static int store_write_pair(int fd, const char* key, const char* value)
 	for (i = 0; value[i]; i++)
 		if (value[i] == ';' || value[i] == '#')
 			quote = 1;
-	if (value[i-1] == ' ')
+	if (i && value[i-1] == ' ')
 		quote = 1;
 
 	if (write_in_full(fd, "\t", 1) != 1 ||

--
1.5.3.7.1116.gae2a9

#1 lore raw
Quoted 12 lines · Jim Meyering

* config.c (store_write_pair): Don't read value[-1].

---
 config.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
config.c +0 -0
index ed96213..6031b38 100644

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

Quoted 8 lines
config.c +1 -1
@@ -652,7 +652,7 @@ static int store_write_pair(int fd, const char* key, const char* value)
 	for (i = 0; value[i]; i++)
 		if (value[i] == ';' || value[i] == '#')
 			quote = 1;
-	if (value[i-1] == ' ')
+	if (i && value[i-1] == ' ')
 		quote = 1;
 
 	if (write_in_full(fd, "\t", 1) != 1 ||

Thanks.

The fix may be correct but the comment above the part the patch fixes is very misleading and I wasted a few minutes checking to see what it is doing is correct.

The "quote" variable does not really control quoting; the quoting with backslash is always done for the value, and this check is about adding a dq pair around it, so that the parser does not lose leading or trailing SP or string after start-of-comment marker characters.

#2 lore raw
Quoted 3 lines · Junio C Hamano

The fix may be correct but the comment above the part the patch fixes is very misleading and I wasted a few minutes checking to see what it is doing is correct.

The fix is correct. I did not anticipate empty strings when I wrote the loop in question. Although I wonder if that should be expanded to use isspace instead of checking for just ' '. I'm less familiar with the config parsing than I was.

Quoted 4 lines

The "quote" variable does not really control quoting; the quoting with backslash is always done for the value, and this check is about adding a dq pair around it, so that the parser does not lose leading or trailing SP or string after start-of-comment marker characters.

It's for adding quotes, which made the comment make sense to me. But your expanded description is correct and matches the commit message I wrote. Thanks for clarifying it in the code.

~~ Brian