From fde8a28b3825c8ce9b33eb59d8f083862e50a4d7 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@fb.com>
Date: Sat, 4 Oct 2014 20:25:02 -0700
Subject: [PATCH] grep: avoid stack buffer read-underrun and overrun

Testing binaries built with -fsanitize=address caused aborts due
to stack underrun and overrun.
* src/grep.c (main): Allocate a larger buffer for eolbytes:
one byte before the beginning and one more after the end.
---
 src/grep.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 7475ea1..9dcf298 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2513,9 +2513,10 @@ main (int argc, char **argv)

   compile (keys, keycc);
   free (keys);
-  char eolbytes[2] = { eolbyte };
+  /* We need one byte prior and at least two after.  */
+  char eolbytes[4] = { 0, eolbyte, 0, 0 };
   size_t match_size;
-  skip_empty_lines = ((execute (eolbytes, 1, &match_size, NULL) == 0)
+  skip_empty_lines = ((execute (eolbytes + 1, 1, &match_size, NULL) == 0)
                       == out_invert);

   if ((argc - optind > 1 && !no_filenames) || with_filenames)
-- 
2.0.0.421.g786a89d

