Page MenuHomeFreeBSD

grep: avoid duplicated lines when we're coloring output
ClosedPublic

Authored by kevans on Mar 12 2025, 4:52 AM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Oct 8, 2:54 AM
Unknown Object (File)
Sun, Oct 5, 11:00 PM
Unknown Object (File)
Tue, Sep 30, 7:08 PM
Unknown Object (File)
Sep 13 2025, 11:20 AM
Unknown Object (File)
Sep 13 2025, 9:25 AM
Unknown Object (File)
Sep 12 2025, 12:19 PM
Unknown Object (File)
Sep 8 2025, 6:19 PM
Unknown Object (File)
Jul 29 2025, 7:31 AM

Details

Summary

For the default uncolored output, we'll just output a line once and then
move on. For colored output, we'll output multiple matches per
line with context from the line interspersed and may end up writing out
some match context multiple times as we don't persist which part of the
lines have already been printed.

Fix it by tracking the length of line printed thus far in printline()
and retaining it across successive calls to printline() in the same
line. printline() should indicate whether it terminated the line or not
to avoid tracking the logic for that in multiple places: -o lines are
always terminated, so it's generally only some --color contexts where we
wouldn't have terminated.

Add a test to make sure that we're only printing one line going forward.

Reported by: Jamie Landeg-Jones <jamie catflap org>

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Looks reasonable

usr.bin/grep/tests/grep_freebsd_test.sh
116

Doesn't matter of course, but . doesn't need quoting

This revision is now accepted and ready to land.Mar 12 2025, 12:03 PM

Works perfectly for me on a recent 14-stable.

Thanks!

This revision was automatically updated to reflect the committed changes.
kevans marked an inline comment as done.