Skip to contentSkip to conversation

off-by-one bugs found by valgrind

PATCH In review Pavel Roskin 4 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 off-by-one bugs found by valgrind

Insufficient memory is allocated in index-pack.c to hold the *.idx name. One more byte should be allocated to hold the terminating 0.

quote_c_style_counted() in quote.c uses a dangerous construct, when a variable is incremented once and used twice in the same expression. Convert this construct to a more traditional form of the for loop.

index-pack.c +1 -1
index 785fe71..d4ce3af 100644
@@ -440,7 +440,7 @@ int main(int argc, char **argv)
 		if (len < 5 || strcmp(pack_name + len - 5, ".pack"))
 			die("packfile name '%s' does not end with '.pack'",
 			    pack_name);
-		index_name_buf = xmalloc(len - 1);
+		index_name_buf = xmalloc(len);
 		memcpy(index_name_buf, pack_name, len - 5);
 		strcpy(index_name_buf + len - 5, ".idx");
 		index_name = index_name_buf;
quote.c +4 -2
index 76eb144..00297b5 100644
@@ -126,8 +126,10 @@ static int quote_c_style_counted(const c
 
 	if (!no_dq)
 		EMIT('"');
-	for (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {
-
+	for (sp = name; sp < name + namelen; sp++) {
+		ch = *sp;
+		if (!ch)
+			break;
 		if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
 		    (ch == 0177)) {
 			needquote = 1;
Signature · 2 lines
Regards,
Pavel Roskin
#1 lore raw
Quoted 2 lines · Pavel Roskin

Insufficient memory is allocated in index-pack.c to hold the *.idx name. One more byte should be allocated to hold the terminating 0.

Thanks.

Quoted 2 lines

quote_c_style_counted() in quote.c uses a dangerous construct, when a variable is incremented once and used twice in the same expression.

Sorry, I do not follow you. Isn't && a sequence point?

Quoted 3 lines
quote.c +1 -2
-	for (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {
-
+	for (sp = name; sp < name + namelen; sp++) {
#2 lore raw
Quoted 1 line · Junio C Hamano

Pavel Roskin <[email protected]> writes:

[...]

Quoted 8 lines
Quoted 2 lines

quote_c_style_counted() in quote.c uses a dangerous construct, when a variable is incremented once and used twice in the same expression.

Sorry, I do not follow you. Isn't && a sequence point?

quote.c static int quote_c_style_counted(const c · line 129

Quoted 3 lines
quote.c +1 -2
-	for (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {
-
+	for (sp = name; sp < name + namelen; sp++) {

Yes, it is, so it doesn't fix any bugs; but Pavel's version is definitely more readable.

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#3 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Pavel Roskin

Insufficient memory is allocated in index-pack.c to hold the *.idx name. One more byte should be allocated to hold the terminating 0.

Quoted 8 lines

Thanks.

Quoted 2 lines

quote_c_style_counted() in quote.c uses a dangerous construct, when a variable is incremented once and used twice in the same expression.

Sorry, I do not follow you. Isn't && a sequence point?

&& is a sequence point. The code is techically fine, but it's harder than necessary to read.

-hpa

#4 lore raw
Quoted 4 lines · H. Peter Anvin
Quoted 4 lines
Quoted 2 lines

quote_c_style_counted() in quote.c uses a dangerous construct, when a variable is incremented once and used twice in the same expression.

Sorry, I do not follow you. Isn't && a sequence point?

The patch is right, but my comment was wrong, sorry.

The actual problem detected by valgrind is that sp is dereferenced before it's checked for the upper boundary. So, if e.g. namelen is 6, the code reads name[6] into ch and then leaves the loop.

Quoted 2 lines

&& is a sequence point. The code is techically fine, but it's harder than necessary to read.

That alone should be a good reason to apply this patch.

Signature · 2 lines
Regards,
Pavel Roskin