Page MenuHomeFreeBSD

bsdgrep(1): fix -w flag matching with an empty pattern
ClosedPublic

Authored by kevans on Apr 20 2017, 2:48 AM.
Tags
None
Referenced Files
F106638344: D10433.diff
Fri, Jan 3, 4:13 AM
F106624964: D10433.id27906.diff
Thu, Jan 2, 10:37 PM
Unknown Object (File)
Tue, Dec 31, 3:13 PM
Unknown Object (File)
Sat, Dec 14, 2:12 AM
Unknown Object (File)
Fri, Dec 13, 3:28 PM
Unknown Object (File)
Dec 1 2024, 9:11 AM
Unknown Object (File)
Dec 1 2024, 5:06 AM
Unknown Object (File)
Dec 1 2024, 1:50 AM
Subscribers
None

Details

Summary

-w flag matching with an empty pattern was generally 'broken', allowing
matches to occur on any line whether or not it actually matches -w criteria.

This one required a good amount of refactoring to address. Basically, procline()
gets altered to *only* process the line and return whether it was a match or not,
necessary to be able to short-circuit the whole function in case of this matchall
flag. We move out the -m flag handling at the same time because it suffers from
the same fate as context handling if we bypass any actual pattern matching.

The matching context (matches, mostly) didn't previously exist outside of procline(),
so we go ahead and create context object for file processing bits to pass around.
grep_printline() was created due to this, for the scenarios where the matches don't
actually matter and we just want to print a line or two, a la flushing the context
queue and no -o or --color specified.

Damage from this broken behavior would have been mitigated by the fact that the
odds are really low that one would depend on empty patterns with the -w flag.

This was spotted while checking PR 105221 for problems that this may cause in
bsdgrep(1), but 105221 is *not* a report of this behavior.

Test Plan

Ensure no regressions in Kyua tests

Diff Detail

Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 8803
Build 9172: arc lint + arc unit

Event Timeline

Apologies, my working directory changes did not make it into a commit for
the initial submission of this. procline()'s return value was supposed to be changed
to instead use the normal pattern (return of 0 is good/match, 1 is no good/match)

  • Fix merge conflicts as a result of rS317254

Rewrite/fix the context handling; this was missed because I forgot to change the
atf_expect_fail with rS317051, so I completely broke context handling but missed
it because the tests failed as expected. This version has some added benefits:

  • The queue handling for -B was previously done whether we actually cared about context or not; this is no longer the case
  • The flow for printing output is a lot more linear and readable, IMO. It's pretty clear now where the matched line is printed and where the context is printed.

This should be the last tweak to this one:

  • Simplify even further; ctxover isn't necessary, we can just inspect linesqueued
  • Sorry, I lied; simplify context separator bits a little bit further
  • More simplification...we can always get better, right?
  • Fix one last context nit that we weren't testing for and I stumbled upon; adjacent non-overlapping contexts were still broken

Apologies for the phab spam; I think that's it. The last one was only found after throwing stuff at the wall for the better part of two hours, so I think it's safe to say this has settled down.

  • Further amend tests for two other cases; properly measure gap between context/match by whether we're rotating lines out of the queue or not doing -B matches
  • Correct comparison in test for grep type; remnant from a past iteration where this was thought to have failed on multiple types
  • Fix indentation while changing line/match references on this line
  • Resolve merge conflicts introduced by rS317665
  • Also remove the binary check from procline -- this was solely due to context

printing, and it's covered in the setting of the 'doctx' variable introduced in
procfile since it handles all context now.

usr.bin/grep/util.c
248–250

some lines are > 80 cols

424–433

some of these are > 80 cols

  • Refactor a little bit more to avoid excessive line lengths and wrapping
usr.bin/grep/grep.c
726–727

I will rewrap this to 80 cols before commit

This revision was automatically updated to reflect the committed changes.