Page MenuHomeFreeBSD

bsdgrep(1): Handle special case of pattern being terminated early with a NULL byte
ClosedPublic

Authored by kevans on Mar 22 2017, 8:43 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Jun 22, 6:43 PM
Unknown Object (File)
Sat, Jun 22, 10:11 AM
Unknown Object (File)
Sat, Jun 22, 9:17 AM
Unknown Object (File)
Sat, Jun 22, 9:16 AM
Unknown Object (File)
May 13 2024, 3:52 PM
Unknown Object (File)
Mar 29 2024, 2:33 PM
Unknown Object (File)
Mar 26 2024, 9:57 AM
Unknown Object (File)
Feb 21 2024, 10:49 AM
Subscribers

Details

Summary

Teach bsdgrep(1) how to handle the special case of a pattern being terminated early with a NULL byte. We ignore these patterns, in line with how gnugrep(1) handles the same case.

PR: 202022

Test Plan

Test kcwu@csie.org's original example, ensure no segfault and no matches.
Run kyua tests to check that we've not caused any further regressions.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Practically, when does this happen? Malicious/test inputs only?

Also, what happens with a nul mid-line? Just the first part of the line is used, the second part discarded?

usr.bin/grep/grep.c
318 โ†—(On Diff #26565)

style(9): space between "if" and "(".

In D10102#212232, @cem wrote:

Practically, when does this happen? Malicious/test inputs only?

Yes, or it could be under the class of rm -rf /$empty problems -- the following produces a perfectly valid pattern file that does exactly what you expect, because getline(3) does the right thing:

printf "BSD\0\nLicensing\0\n" > /tmp/pat
grep -f /tmp/pat /COPYRIGHT

It's a quick step from there to, in some case, printf "$empty\0\n" >> /tmp/pat in some loop to generate a series of expressions to test by. The only problem here is we hadn't taken into consideration what getline(3) does when it comes across a "\0\n" line, which is produce the tested-for above.

Also, what happens with a nul mid-line? Just the first part of the line is used, the second part discarded?

Correct.

ngie added inline comments.
usr.bin/grep/grep.c
318 โ†—(On Diff #27030)

Could you please check for '\0' instead of 0 for reader/static analysis tool clarity?

  • Check for '\0' instead of 0 for clarity
This revision is now accepted and ready to land.Apr 4 2017, 7:45 PM

Ping @emaste -- any further comments/action required on this one? =)

This revision was automatically updated to reflect the committed changes.