{"thread":{"id":"2899","subject":"[PATCH] off-by-one bugs found by valgrind","startedAt":"2005-12-21T20:35:48Z","lastAt":"2005-12-21T21:47:21Z","messageCount":4,"participants":["Pavel Roskin","Junio C Hamano","H. Peter Anvin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"13884","messageId":"1135197348.3046.7.camel@dv","threadId":"2899","inReplyTo":null,"subject":"[PATCH] off-by-one bugs found by valgrind","fromName":"Pavel Roskin","fromEmail":"proski@gnu.org","sentAt":"2005-12-21T20:35:48Z","receivedAt":"2005-12-21T20:35:48Z","isPatch":true,"sender":{"key":"proski@gnu.org","avatar":null},"body":"Insufficient memory is allocated in index-pack.c to hold the *.idx name.\nOne more byte should be allocated to hold the terminating 0.\n\nquote_c_style_counted() in quote.c uses a dangerous construct, when a\nvariable is incremented once and used twice in the same expression.\nConvert this construct to a more traditional form of the for loop.\n\nSigned-off-by: Pavel Roskin <proski@gnu.org>\n\ndiff --git a/index-pack.c b/index-pack.c\nindex 785fe71..d4ce3af 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -440,7 +440,7 @@ int main(int argc, char **argv)\n \t\tif (len < 5 || strcmp(pack_name + len - 5, \".pack\"))\n \t\t\tdie(\"packfile name '%s' does not end with '.pack'\",\n \t\t\t    pack_name);\n-\t\tindex_name_buf = xmalloc(len - 1);\n+\t\tindex_name_buf = xmalloc(len);\n \t\tmemcpy(index_name_buf, pack_name, len - 5);\n \t\tstrcpy(index_name_buf + len - 5, \".idx\");\n \t\tindex_name = index_name_buf;\ndiff --git a/quote.c b/quote.c\nindex 76eb144..00297b5 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -126,8 +126,10 @@ static int quote_c_style_counted(const c\n \n \tif (!no_dq)\n \t\tEMIT('\"');\n-\tfor (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {\n-\n+\tfor (sp = name; sp < name + namelen; sp++) {\n+\t\tch = *sp;\n+\t\tif (!ch)\n+\t\t\tbreak;\n \t\tif ((ch < ' ') || (ch == '\"') || (ch == '\\\\') ||\n \t\t    (ch == 0177)) {\n \t\t\tneedquote = 1;\n\n\n-- \nRegards,\nPavel Roskin\n"},{"id":"13887","messageId":"7vr7865fq5.fsf@assigned-by-dhcp.cox.net","threadId":"2899","inReplyTo":"1135197348.3046.7.camel@dv","subject":"Re: [PATCH] off-by-one bugs found by valgrind","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-21T20:59:14Z","receivedAt":"2005-12-21T20:59:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Pavel Roskin <proski@gnu.org> writes:\n\n> Insufficient memory is allocated in index-pack.c to hold the *.idx name.\n> One more byte should be allocated to hold the terminating 0.\n\nThanks.\n\n> quote_c_style_counted() in quote.c uses a dangerous construct, when a\n> variable is incremented once and used twice in the same expression.\n\nSorry, I do not follow you.  Isn't && a sequence point?\n\n> -\tfor (sp = name; (ch = *sp++) && (sp - name) <= namelen; ) {\n> -\n> +\tfor (sp = name; sp < name + namelen; sp++) {\n"},{"id":"13890","messageId":"43A9C654.2010009@zytor.com","threadId":"2899","inReplyTo":"7vr7865fq5.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] off-by-one bugs found by valgrind","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2005-12-21T21:17:08Z","receivedAt":"2005-12-21T21:17:08Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"Junio C Hamano wrote:\n> Pavel Roskin <proski@gnu.org> writes:\n> \n> \n>>Insufficient memory is allocated in index-pack.c to hold the *.idx name.\n>>One more byte should be allocated to hold the terminating 0.\n> \n> Thanks.\n> \n> \n>>quote_c_style_counted() in quote.c uses a dangerous construct, when a\n>>variable is incremented once and used twice in the same expression.\n> \n> Sorry, I do not follow you.  Isn't && a sequence point?\n> \n\n&& is a sequence point.  The code is techically fine, but it's harder \nthan necessary to read.\n\n\t-hpa\n"},{"id":"13892","messageId":"1135201641.15567.7.camel@dv","threadId":"2899","inReplyTo":"43A9C654.2010009@zytor.com","subject":"Re: [PATCH] off-by-one bugs found by valgrind","fromName":"Pavel Roskin","fromEmail":"proski@gnu.org","sentAt":"2005-12-21T21:47:21Z","receivedAt":"2005-12-21T21:47:21Z","isPatch":true,"sender":{"key":"proski@gnu.org","avatar":null},"body":"On Wed, 2005-12-21 at 13:17 -0800, H. Peter Anvin wrote:\n> >>quote_c_style_counted() in quote.c uses a dangerous construct, when a\n> >>variable is incremented once and used twice in the same expression.\n> > \n> > Sorry, I do not follow you.  Isn't && a sequence point?\n\nThe patch is right, but my comment was wrong, sorry.\n\nThe actual problem detected by valgrind is that sp is dereferenced\nbefore it's checked for the upper boundary.  So, if e.g. namelen is 6,\nthe code reads name[6] into ch and then leaves the loop.\n\n> && is a sequence point.  The code is techically fine, but it's harder \n> than necessary to read.\n\nThat alone should be a good reason to apply this patch.\n\n-- \nRegards,\nPavel Roskin\n"}]}