svn commit: r322520 - stable/11/usr.bin/grep

Kyle Evans kevans at FreeBSD.org
Mon Aug 14 21:48:51 UTC 2017


Author: kevans
Date: Mon Aug 14 21:48:50 2017
New Revision: 322520
URL: https://svnweb.freebsd.org/changeset/base/322520

Log:
  MFC r313948: bsdgrep: fix EOF handling with --mmap
  
  Rework part of the loop in grep_fgetln to return the rest of the line
  and ensure that we still advance the buffer by the length of the rest
  of the line.
  
  PR:		165471
  Approved by:	emaste (mentor)

Modified:
  stable/11/usr.bin/grep/file.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.bin/grep/file.c
==============================================================================
--- stable/11/usr.bin/grep/file.c	Mon Aug 14 20:53:01 2017	(r322519)
+++ stable/11/usr.bin/grep/file.c	Mon Aug 14 21:48:50 2017	(r322520)
@@ -219,12 +219,18 @@ grep_fgetln(struct file *f, size_t *lenp)
 		if (bufrem == 0)
 			/* EOF: return partial line */
 			break;
-		if ((p = memchr(bufpos, '\n', bufrem)) == NULL)
+		if ((p = memchr(bufpos, '\n', bufrem)) == NULL &&
+		    filebehave != FILE_MMAP)
 			continue;
-		/* got it: finish up the line (like code above) */
-		++p;
-		diff = p - bufpos;
-		len += diff;
+		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;
+		}
 		if (grep_lnbufgrow(len))
 		    goto error;
 		memcpy(lnbuf + off, bufpos, diff);


More information about the svn-src-all mailing list