Author: emaste
Date: Sat May 20 00:42:47 2017
New Revision: 318565
URL: https://svnweb.freebsd.org/changeset/base/318565

Log:
  bsdgrep: fix segfault with --mmap
  
  r313948 partially fixed --mmap behavior but was incomplete.  This commit
  generally reverts it and does it the more correct way- by just consuming
  the rest of the buffer and moving on.
  
  PR:           219402
  Submitted by: Kyle Evans <kevan...@ksu.edu>
  Reviewed by:  cem
  Differential Revision:        https://reviews.freebsd.org/D10820

Modified:
  head/usr.bin/grep/file.c

Modified: head/usr.bin/grep/file.c
==============================================================================
--- head/usr.bin/grep/file.c    Sat May 20 00:41:12 2017        (r318564)
+++ head/usr.bin/grep/file.c    Sat May 20 00:42:47 2017        (r318565)
@@ -213,24 +213,24 @@ grep_fgetln(struct file *f, size_t *lenp
                if (grep_lnbufgrow(len + LNBUFBUMP))
                        goto error;
                memcpy(lnbuf + off, bufpos, len - off);
+               /* With FILE_MMAP, this is EOF; there's no more to refill */
+               if (filebehave == FILE_MMAP) {
+                       bufrem -= len;
+                       break;
+               }
                off = len;
+               /* Fetch more to try and find EOL/EOF */
                if (grep_refill(f) != 0)
                        goto error;
                if (bufrem == 0)
                        /* EOF: return partial line */
                        break;
-               if ((p = memchr(bufpos, fileeol, bufrem)) == NULL &&
-                   filebehave != FILE_MMAP)
+               if ((p = memchr(bufpos, fileeol, bufrem)) == NULL)
                        continue;
-               if (p == NULL) {
-                       /* mmap EOF: return partial line, consume buffer */
-                       diff = len;
-               } else {
-                       /* got it: finish up the line (like code above) */
-                       ++p;
-                       diff = p - bufpos;
-                       len += diff;
-               }
+               /* got it: finish up the line (like code above) */
+               ++p;
+               diff = p - bufpos;
+               len += diff;
                if (grep_lnbufgrow(len))
                    goto error;
                memcpy(lnbuf + off, bufpos, diff);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to