[Bug 258616] fgrep fails to find string that is present

From: <bugzilla-noreply_at_freebsd.org>
Date: Mon, 20 Sep 2021 01:13:55 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=258616

            Bug ID: 258616
           Summary: fgrep fails to find string that is present
           Product: Base System
           Version: 12.2-STABLE
          Hardware: amd64
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: dtucker@dtucker.net

Created attachment 228028
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=228028&action=edit
known_hosts file demonstrating fgrep problem

I think I've discovered a case where fgrep fails to find a string that is in
fact present in the input file.

I have been seeing intermittent test failures in OpenSSH's regress tests on at
least FreeBSD 12.2 and NetBSD 9 (which seems to be based on FreeBSD's code). 
The test fails maybe 1 in 100 times when fgrep fails to match a string (an SSH
host key in this case) that is in the input file.

It seems to be (a) data-dependent and (b) consistent.  I captured one of the
failure cases (attached), and if I select the key from the file with awk then
fgrep for it, fgrep does not find it:

$ uname -a
FreeBSD fbsd12 12.2-RELEASE-p3 FreeBSD 12.2-RELEASE-p3 GENERIC  amd64
$ fgrep $(awk '$2=="ssh-rsa"{print $3}' known_hosts) known_hosts; echo $?
1
$ grep -F $(awk '$2=="ssh-rsa"{print $3}' known_hosts) known_hosts; echo $?
1

but the equivalent awk will find it:

$ a=$(awk '$2=="ssh-rsa"{print $3}' known_hosts)           
$ awk '$3=="'$a'"' known_hosts
localhost-with-alias ssh-rsa [key elided]

and if I reduce the size of the file fgrep then also works:

$ grep ssh-rsa known_hosts >known_hosts2
$ fgrep $(awk '$2=="ssh-rsa"{print $3}' known_hosts2) known_hosts2; echo $?
localhost-with-alias ssh-rsa [key elided]
0

Deleting the first few lines from the file will also make it work.  Further
weirdness: I built grep from the 12.2 branch it worked as expected:

$ ./grep -F $(awk '$2=="ssh-rsa"{print $3}' known_hosts) known_hosts; echo $?
localhost-with-alias ssh-rsa [key elided]
0

Same file works as expected on at least Linux and OpenBSD.  One of the other
developers reproduced this and reduced the size of the test case which I will
also attach.

-- 
You are receiving this mail because:
You are the assignee for the bug.