grep: very long lines no longer evoke unwarranted "memory exhausted"

When calling xpalloc (NULL, &n, incr_min, alloc_max, 1) with
nontrivial ALLOC_MAX, this must hold: N + INCR_MIN <= ALLOC_MAX.
With a very long line, it did not, and grep would mistakenly fail
with a report of "memory exhausted".
* src/grep.c (fillbuf): When using nontrivial ALLOC_MAX, ensure it
is at least N+INCR_MIN.
* tests/fillbuf-long-line: New file, to test for this.
* tests/Makefile.am (TESTS): Add its name.
This commit is contained in:
Jim Meyering 2022-03-20 09:43:30 -07:00
parent efe1e1543c
commit 6e95551ad6
3 changed files with 13 additions and 1 deletions

View File

@ -984,7 +984,7 @@ fillbuf (idx_t save, struct stat const *st)
ptrdiff_t a;
if (0 <= to_be_read
&& INT_ADD_OK (to_be_read, save + min_after_buflim, &a))
alloc_max = a;
alloc_max = MAX (a, bufalloc + incr_min);
}
newbuf = xpalloc (NULL, &bufalloc, incr_min, alloc_max, 1);

View File

@ -104,6 +104,7 @@ TESTS = \
fgrep-longest \
file \
filename-lineno.pl \
fillbuf-long-line \
fmbtest \
foad1 \
grep-dev-null \

11
tests/fillbuf-long-line Executable file
View File

@ -0,0 +1,11 @@
#!/bin/sh
# This would fail for v3.7-15-ge3694e9 .. grep-v3.7-48-g5c3c427
. "${srcdir=.}/init.sh"; path_prepend_ ../src
printf %0104681d 0 > in || framework_failure_
fail=0
returns_ 1 grep xx in || fail=1
Exit $fail