]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
grep: fix -A handling in conjunction with -m match limitation
authorKyle Evans <kevans@FreeBSD.org>
Thu, 11 Feb 2021 15:11:24 +0000 (09:11 -0600)
committerKyle Evans <kevans@FreeBSD.org>
Thu, 11 Feb 2021 15:12:17 +0000 (09:12 -0600)
commitdc57f212526d25e8ef036e768b816a7fdae5707b
tree4635b8d87a5f2e75ae69f542793fe544e4015c21
parentf28ccd8d95954134cec93db482fb0bd4ce93f36e
grep: fix -A handling in conjunction with -m match limitation

The basic issue here is that grep, when given -m 1, would stop all
line processing once it hit the match count and exit immediately.  The
problem with exiting immediately is that -A processing only happens when
subsequent lines are processed and do not match.

The fix here is relatively easy; when bsdgrep matches a line, it resets
the 'tail' of the matching context to the value supplied to -A and
dumps anything that's been queued up for -B. After the current line has
been printed and tail is reset, we check our mcount and do what's
needed. Therefore, at the time that we decide we're doing nothing, we
know that 'tail' of the context is correct and we can simply continue
on if there's still more to pick up.

With this change, we still bail out immediately if there's been no -A
flag. If -A was supplied, we signal that we should continue on. However,
subsequent lines will not even bothere to try and process the line.  We
have reached the match count, so even if the next line would match then
we must process it if it hadn't. Thus, the loop in procfile() can
short-circuit and just process the line as a non-match until
procmatches() indicates that it's safe to stop.

A test has been added to reflect both that we should be picking up the
next line and that the next line should be considered a non-match even
if it should have been.

PR: 253350
Approved by: re (gjb)

(cherry picked from commit 3e2d96ac974db823255a6f40b90eeffa6e38d022)
(cherry picked from commit 08f25b50dbd332e5c5c9380fd90c516e9af1ab36)
contrib/netbsd-tests/usr.bin/grep/t_grep.sh
usr.bin/grep/util.c