{"thread":{"id":"47336","subject":"[PATCH] pretty: fix buffer over-read with %> and %<","startedAt":"2017-11-29T00:33:59Z","lastAt":"2017-11-29T00:33:59Z","messageCount":1,"participants":["mwnx"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"333714","messageId":"20171129003251.1981-1-mwnx@gmx.com","threadId":"47336","inReplyTo":null,"subject":"[PATCH] pretty: fix buffer over-read with %> and %<","fromName":"mwnx","fromEmail":"mwnx@gmx.com","sentAt":"2017-11-29T00:32:51Z","receivedAt":"2017-11-29T00:33:59Z","isPatch":true,"sender":{"key":"mwnx@gmx.com","avatar":null},"body":"A buffer over-read of the format string would occur with unterminated\nformats of the form '%>(#' and '%<(#', where '#' represents a number.\n\nThis error can be witnessed by running git log under valgrind like so:\n\n    valgrind git log -n1 --format='%<(42'\n\nThis was due to the fact that the \"not found\" case for strcspn() was\nbeing checked in the same way that one checks the \"not found\" case for\nstrchr(), i.e. by checking for a NULL pointer return value. Instead, one\nmust check for the end of the string since strcspn() points to the\ncharacter where it finished its search (which will be a '\\0' if\nunsuccessful).\n\nSigned-off-by: Maxwell Nixie <mwnx@gmx.com>\n---\nHope I got everything right.\n\n pretty.c                      | 2 +-\n t/t4205-log-pretty-formats.sh | 6 ++++++\n 2 files changed, 7 insertions(+), 1 deletion(-)\n\ndiff --git a/pretty.c b/pretty.c\nindex 2f6b0ae6c..4c70bad45 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1021,7 +1021,7 @@ static size_t parse_padding_placeholder(struct strbuf *sb,\n \t\tconst char *end = start + strcspn(start, \",)\");\n \t\tchar *next;\n \t\tint width;\n-\t\tif (!end || end == start)\n+\t\tif (!*end || end == start)\n \t\t\treturn 0;\n \t\twidth = strtol(start, &next, 10);\n \t\tif (next == start || width == 0)\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex 591f35daa..4d9555962 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -598,4 +598,10 @@ test_expect_success ':only and :unfold work together' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'unterminated alignment formatting' '\n+\tgit log -n1 --format=\"%<(42\" >actual &&\n+\techo \"%<(42\" >expected &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\n-- \n2.15.0.319.gb3398b820\n\n"}]}