git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] grep: do not segfault when -f is used
@ 2009-10-16  8:53 Matt Kraai
  2009-10-16 10:34 ` Johannes Sixt
  0 siblings, 1 reply; 6+ messages in thread
From: Matt Kraai @ 2009-10-16  8:53 UTC (permalink / raw)
  To: git, gitster; +Cc: Matt Kraai

"git grep" would segfault if its -f option was used because it would
try to use an uninitialized strbuf, so initialize the strbuf.

Signed-off-by: Matt Kraai <kraai@ftbfs.org>
---
 builtin-grep.c  |    2 +-
 t/t7002-grep.sh |    4 ++++
 2 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/builtin-grep.c b/builtin-grep.c
index 761799d..1df25b0 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -631,7 +631,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
 	struct grep_opt *grep_opt = opt->value;
 	FILE *patterns;
 	int lno = 0;
-	struct strbuf sb;
+	struct strbuf sb = STRBUF_INIT;
 
 	patterns = fopen(arg, "r");
 	if (!patterns)
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index ae56a36..762f815 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -44,6 +44,10 @@ test_expect_success 'grep should not segfault with a bad input' '
 	test_must_fail git grep "("
 '
 
+test_expect_success 'grep should not segfault with -f' '
+        test_must_fail git grep -f /dev/null
+'
+
 for H in HEAD ''
 do
 	case "$H" in
-- 
1.6.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep: do not segfault when -f is used
  2009-10-16  8:53 [PATCH] grep: do not segfault when -f is used Matt Kraai
@ 2009-10-16 10:34 ` Johannes Sixt
  2009-10-16 13:39   ` Matt Kraai
  0 siblings, 1 reply; 6+ messages in thread
From: Johannes Sixt @ 2009-10-16 10:34 UTC (permalink / raw)
  To: Matt Kraai; +Cc: git, gitster

Matt Kraai schrieb:
> "git grep" would segfault if its -f option was used because it would
> try to use an uninitialized strbuf, so initialize the strbuf.

Thanks for noticing and for the patch.

But...

> +test_expect_success 'grep should not segfault with -f' '
> +        test_must_fail git grep -f /dev/null
> +'

there must be a better way to test whether grep -f behaves correctly.

-- Hannes

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep: do not segfault when -f is used
  2009-10-16 10:34 ` Johannes Sixt
@ 2009-10-16 13:39   ` Matt Kraai
  2009-10-16 13:46     ` Johannes Sixt
  0 siblings, 1 reply; 6+ messages in thread
From: Matt Kraai @ 2009-10-16 13:39 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: git

On Fri, Oct 16, 2009 at 12:34:23PM +0200, Johannes Sixt wrote:
> Matt Kraai schrieb:
> > +test_expect_success 'grep should not segfault with -f' '
> > +        test_must_fail git grep -f /dev/null
> > +'
> 
> there must be a better way to test whether grep -f behaves correctly.

How about the following test cases instead?

test_expect_success 'grep -f, non-existent file' '
	test_must_fail git grep -f patterns
'

cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
EOF

cat >pattern <<EOF
mmap
EOF

test_expect_success 'grep -f, one pattern' '
	git grep -f pattern >actual &&
	test_cmp expected actual
'

cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
t/a/v:vvv
t/v:vvv
v:vvv
EOF

cat >patterns <<EOF
mmap
vvv
EOF

test_expect_success 'grep -f, multiple patterns' '
	git grep -f patterns >actual &&
	test_cmp expected actual
'

cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
t/a/v:vvv
t/v:vvv
v:vvv
EOF

cat >patterns <<EOF

mmap

vvv

EOF

test_expect_success 'grep -f, ignore empty lines' '
	git grep -f patterns >actual &&
	test_cmp expected actual
'

-- 
Matt Kraai                                           http://ftbfs.org/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep: do not segfault when -f is used
  2009-10-16 13:39   ` Matt Kraai
@ 2009-10-16 13:46     ` Johannes Sixt
  2009-10-16 14:13       ` Matt Kraai
  0 siblings, 1 reply; 6+ messages in thread
From: Johannes Sixt @ 2009-10-16 13:46 UTC (permalink / raw)
  To: Matt Kraai; +Cc: git

Matt Kraai schrieb:
> On Fri, Oct 16, 2009 at 12:34:23PM +0200, Johannes Sixt wrote:
>> there must be a better way to test whether grep -f behaves correctly.
> 
> How about the following test cases instead?

*MUCH* better! Now, if you could wrap them up in a patch...

-- Hannes

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] grep: do not segfault when -f is used
  2009-10-16 13:46     ` Johannes Sixt
@ 2009-10-16 14:13       ` Matt Kraai
  2009-10-17  7:44         ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Matt Kraai @ 2009-10-16 14:13 UTC (permalink / raw)
  To: git, gitster, Johannes Sixt; +Cc: Matt Kraai

"git grep" would segfault if its -f option was used because it would
try to use an uninitialized strbuf, so initialize the strbuf.

Thanks to Johannes Sixt <j.sixt@viscovery.net> for the help with the
test cases.

Signed-off-by: Matt Kraai <kraai@ftbfs.org>
---
 builtin-grep.c  |    2 +-
 t/t7002-grep.sh |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletions(-)

diff --git a/builtin-grep.c b/builtin-grep.c
index 761799d..1df25b0 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -631,7 +631,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
 	struct grep_opt *grep_opt = opt->value;
 	FILE *patterns;
 	int lno = 0;
-	struct strbuf sb;
+	struct strbuf sb = STRBUF_INIT;
 
 	patterns = fopen(arg, "r");
 	if (!patterns)
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index ae56a36..ae5290a 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -213,6 +213,72 @@ test_expect_success 'grep -e A --and --not -e B' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep -f, non-existent file' '
+	test_must_fail git grep -f patterns
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+EOF
+
+cat >pattern <<EOF
+mmap
+EOF
+
+test_expect_success 'grep -f, one pattern' '
+	git grep -f pattern >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+t/a/v:vvv
+t/v:vvv
+v:vvv
+EOF
+
+cat >patterns <<EOF
+mmap
+vvv
+EOF
+
+test_expect_success 'grep -f, multiple patterns' '
+	git grep -f patterns >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+t/a/v:vvv
+t/v:vvv
+v:vvv
+EOF
+
+cat >patterns <<EOF
+
+mmap
+
+vvv
+
+EOF
+
+test_expect_success 'grep -f, ignore empty lines' '
+	git grep -f patterns >actual &&
+	test_cmp expected actual
+'
+
 cat >expected <<EOF
 y:y yy
 --
-- 
1.6.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] grep: do not segfault when -f is used
  2009-10-16 14:13       ` Matt Kraai
@ 2009-10-17  7:44         ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2009-10-17  7:44 UTC (permalink / raw)
  To: Matt Kraai; +Cc: git, gitster, Johannes Sixt

Matt Kraai <kraai@ftbfs.org> writes:

> "git grep" would segfault if its -f option was used because it would
> try to use an uninitialized strbuf, so initialize the strbuf.
>
> Thanks to Johannes Sixt <j.sixt@viscovery.net> for the help with the
> test cases.
>
> Signed-off-by: Matt Kraai <kraai@ftbfs.org>

Thanks, both of you.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-10-17  7:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-16  8:53 [PATCH] grep: do not segfault when -f is used Matt Kraai
2009-10-16 10:34 ` Johannes Sixt
2009-10-16 13:39   ` Matt Kraai
2009-10-16 13:46     ` Johannes Sixt
2009-10-16 14:13       ` Matt Kraai
2009-10-17  7:44         ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).