{"thread":{"id":"21258","subject":"[PATCH] grep: do not segfault when -f is used","startedAt":"2009-10-16T08:53:24Z","lastAt":"2009-10-17T07:44:19Z","messageCount":6,"participants":["Matt Kraai","Johannes Sixt","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"125152","messageId":"1255683204-28988-1-git-send-email-kraai@ftbfs.org","threadId":"21258","inReplyTo":null,"subject":"[PATCH] grep: do not segfault when -f is used","fromName":"Matt Kraai","fromEmail":"kraai@ftbfs.org","sentAt":"2009-10-16T08:53:24Z","receivedAt":"2009-10-16T08:53:24Z","isPatch":true,"sender":{"key":"kraai@ftbfs.org","avatar":null},"body":"\"git grep\" would segfault if its -f option was used because it would\ntry to use an uninitialized strbuf, so initialize the strbuf.\n\nSigned-off-by: Matt Kraai <kraai@ftbfs.org>\n---\n builtin-grep.c  |    2 +-\n t/t7002-grep.sh |    4 ++++\n 2 files changed, 5 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 761799d..1df25b0 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -631,7 +631,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)\n \tstruct grep_opt *grep_opt = opt->value;\n \tFILE *patterns;\n \tint lno = 0;\n-\tstruct strbuf sb;\n+\tstruct strbuf sb = STRBUF_INIT;\n \n \tpatterns = fopen(arg, \"r\");\n \tif (!patterns)\ndiff --git a/t/t7002-grep.sh b/t/t7002-grep.sh\nindex ae56a36..762f815 100755\n--- a/t/t7002-grep.sh\n+++ b/t/t7002-grep.sh\n@@ -44,6 +44,10 @@ test_expect_success 'grep should not segfault with a bad input' '\n \ttest_must_fail git grep \"(\"\n '\n \n+test_expect_success 'grep should not segfault with -f' '\n+        test_must_fail git grep -f /dev/null\n+'\n+\n for H in HEAD ''\n do\n \tcase \"$H\" in\n-- \n1.6.5\n"},{"id":"125155","messageId":"4AD84C2F.5000809@viscovery.net","threadId":"21258","inReplyTo":"1255683204-28988-1-git-send-email-kraai@ftbfs.org","subject":"Re: [PATCH] grep: do not segfault when -f is used","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2009-10-16T10:34:23Z","receivedAt":"2009-10-16T10:34:23Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Matt Kraai schrieb:\n> \"git grep\" would segfault if its -f option was used because it would\n> try to use an uninitialized strbuf, so initialize the strbuf.\n\nThanks for noticing and for the patch.\n\nBut...\n\n> +test_expect_success 'grep should not segfault with -f' '\n> +        test_must_fail git grep -f /dev/null\n> +'\n\nthere must be a better way to test whether grep -f behaves correctly.\n\n-- Hannes\n"},{"id":"125167","messageId":"20091016133908.GA3172@ftbfs.org","threadId":"21258","inReplyTo":"4AD84C2F.5000809@viscovery.net","subject":"Re: [PATCH] grep: do not segfault when -f is used","fromName":"Matt Kraai","fromEmail":"kraai@ftbfs.org","sentAt":"2009-10-16T13:39:08Z","receivedAt":"2009-10-16T13:39:08Z","isPatch":true,"sender":{"key":"kraai@ftbfs.org","avatar":null},"body":"On Fri, Oct 16, 2009 at 12:34:23PM +0200, Johannes Sixt wrote:\n> Matt Kraai schrieb:\n> > +test_expect_success 'grep should not segfault with -f' '\n> > +        test_must_fail git grep -f /dev/null\n> > +'\n> \n> there must be a better way to test whether grep -f behaves correctly.\n\nHow about the following test cases instead?\n\ntest_expect_success 'grep -f, non-existent file' '\n\ttest_must_fail git grep -f patterns\n'\n\ncat >expected <<EOF\nfile:foo mmap bar\nfile:foo_mmap bar\nfile:foo_mmap bar mmap\nfile:foo mmap bar_mmap\nfile:foo_mmap bar mmap baz\nEOF\n\ncat >pattern <<EOF\nmmap\nEOF\n\ntest_expect_success 'grep -f, one pattern' '\n\tgit grep -f pattern >actual &&\n\ttest_cmp expected actual\n'\n\ncat >expected <<EOF\nfile:foo mmap bar\nfile:foo_mmap bar\nfile:foo_mmap bar mmap\nfile:foo mmap bar_mmap\nfile:foo_mmap bar mmap baz\nt/a/v:vvv\nt/v:vvv\nv:vvv\nEOF\n\ncat >patterns <<EOF\nmmap\nvvv\nEOF\n\ntest_expect_success 'grep -f, multiple patterns' '\n\tgit grep -f patterns >actual &&\n\ttest_cmp expected actual\n'\n\ncat >expected <<EOF\nfile:foo mmap bar\nfile:foo_mmap bar\nfile:foo_mmap bar mmap\nfile:foo mmap bar_mmap\nfile:foo_mmap bar mmap baz\nt/a/v:vvv\nt/v:vvv\nv:vvv\nEOF\n\ncat >patterns <<EOF\n\nmmap\n\nvvv\n\nEOF\n\ntest_expect_success 'grep -f, ignore empty lines' '\n\tgit grep -f patterns >actual &&\n\ttest_cmp expected actual\n'\n\n-- \nMatt Kraai                                           http://ftbfs.org/\n"},{"id":"125168","messageId":"4AD8791A.8060500@viscovery.net","threadId":"21258","inReplyTo":"20091016133908.GA3172@ftbfs.org","subject":"Re: [PATCH] grep: do not segfault when -f is used","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2009-10-16T13:46:02Z","receivedAt":"2009-10-16T13:46:02Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Matt Kraai schrieb:\n> On Fri, Oct 16, 2009 at 12:34:23PM +0200, Johannes Sixt wrote:\n>> there must be a better way to test whether grep -f behaves correctly.\n> \n> How about the following test cases instead?\n\n*MUCH* better! Now, if you could wrap them up in a patch...\n\n-- Hannes\n"},{"id":"125170","messageId":"1255702405-7050-1-git-send-email-kraai@ftbfs.org","threadId":"21258","inReplyTo":"4AD8791A.8060500@viscovery.net","subject":"[PATCH] grep: do not segfault when -f is used","fromName":"Matt Kraai","fromEmail":"kraai@ftbfs.org","sentAt":"2009-10-16T14:13:25Z","receivedAt":"2009-10-16T14:13:25Z","isPatch":true,"sender":{"key":"kraai@ftbfs.org","avatar":null},"body":"\"git grep\" would segfault if its -f option was used because it would\ntry to use an uninitialized strbuf, so initialize the strbuf.\n\nThanks to Johannes Sixt <j.sixt@viscovery.net> for the help with the\ntest cases.\n\nSigned-off-by: Matt Kraai <kraai@ftbfs.org>\n---\n builtin-grep.c  |    2 +-\n t/t7002-grep.sh |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 67 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 761799d..1df25b0 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -631,7 +631,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)\n \tstruct grep_opt *grep_opt = opt->value;\n \tFILE *patterns;\n \tint lno = 0;\n-\tstruct strbuf sb;\n+\tstruct strbuf sb = STRBUF_INIT;\n \n \tpatterns = fopen(arg, \"r\");\n \tif (!patterns)\ndiff --git a/t/t7002-grep.sh b/t/t7002-grep.sh\nindex ae56a36..ae5290a 100755\n--- a/t/t7002-grep.sh\n+++ b/t/t7002-grep.sh\n@@ -213,6 +213,72 @@ test_expect_success 'grep -e A --and --not -e B' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'grep -f, non-existent file' '\n+\ttest_must_fail git grep -f patterns\n+'\n+\n+cat >expected <<EOF\n+file:foo mmap bar\n+file:foo_mmap bar\n+file:foo_mmap bar mmap\n+file:foo mmap bar_mmap\n+file:foo_mmap bar mmap baz\n+EOF\n+\n+cat >pattern <<EOF\n+mmap\n+EOF\n+\n+test_expect_success 'grep -f, one pattern' '\n+\tgit grep -f pattern >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+cat >expected <<EOF\n+file:foo mmap bar\n+file:foo_mmap bar\n+file:foo_mmap bar mmap\n+file:foo mmap bar_mmap\n+file:foo_mmap bar mmap baz\n+t/a/v:vvv\n+t/v:vvv\n+v:vvv\n+EOF\n+\n+cat >patterns <<EOF\n+mmap\n+vvv\n+EOF\n+\n+test_expect_success 'grep -f, multiple patterns' '\n+\tgit grep -f patterns >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+cat >expected <<EOF\n+file:foo mmap bar\n+file:foo_mmap bar\n+file:foo_mmap bar mmap\n+file:foo mmap bar_mmap\n+file:foo_mmap bar mmap baz\n+t/a/v:vvv\n+t/v:vvv\n+v:vvv\n+EOF\n+\n+cat >patterns <<EOF\n+\n+mmap\n+\n+vvv\n+\n+EOF\n+\n+test_expect_success 'grep -f, ignore empty lines' '\n+\tgit grep -f patterns >actual &&\n+\ttest_cmp expected actual\n+'\n+\n cat >expected <<EOF\n y:y yy\n --\n-- \n1.6.5\n"},{"id":"125230","messageId":"7vzl7qforw.fsf@alter.siamese.dyndns.org","threadId":"21258","inReplyTo":"1255702405-7050-1-git-send-email-kraai@ftbfs.org","subject":"Re: [PATCH] grep: do not segfault when -f is used","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-10-17T07:44:19Z","receivedAt":"2009-10-17T07:44:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matt Kraai <kraai@ftbfs.org> writes:\n\n> \"git grep\" would segfault if its -f option was used because it would\n> try to use an uninitialized strbuf, so initialize the strbuf.\n>\n> Thanks to Johannes Sixt <j.sixt@viscovery.net> for the help with the\n> test cases.\n>\n> Signed-off-by: Matt Kraai <kraai@ftbfs.org>\n\nThanks, both of you.\n"}]}