1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-02-06 18:29:47 +00:00

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 <kevans91@ksu.edu>
Reviewed by:	cem
Differential Revision:	https://reviews.freebsd.org/D10820
This commit is contained in:
Ed Maste 2017-05-20 00:42:47 +00:00
parent 1ec22b4425
commit 9a1452026e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=318565

View File

@ -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);