NetBSD Problem Report #58849
From www@netbsd.org Sat Nov 23 02:17:52 2024
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (2048 bits)
client-signature RSA-PSS (2048 bits))
(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id 55DDB1A9238
for <gnats-bugs@gnats.NetBSD.org>; Sat, 23 Nov 2024 02:17:52 +0000 (UTC)
Message-Id: <20241123021742.3FEEC1A923E@mollari.NetBSD.org>
Date: Sat, 23 Nov 2024 02:17:42 +0000 (UTC)
From: campbell+netbsd@mumble.net
Reply-To: campbell+netbsd@mumble.net
To: gnats-bugs@NetBSD.org
Subject: usr.bin/grep is broken (TOOL_GREP, MKBSDGREP=yes)
X-Send-Pr-Version: www-1.0
>Number: 58849
>Category: bin
>Synopsis: usr.bin/grep is broken (TOOL_GREP, MKBSDGREP=yes)
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: bin-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sat Nov 23 02:20:00 +0000 2024
>Last-Modified: Sat Nov 23 14:55:01 +0000 2024
>Originator: Taylor R Campbell
>Release:
>Organization:
The NetBSD Foundgreption
>Environment:
>Description:
usr.bin/grep matching fails sometimes.
>How-To-Repeat:
$ { echo __bss_start__; echo __bss_end__; echo hello; } | nbgrep -Fvx -e _end -e _fini -e _init -e __bss_end__ -e __bss_start__ -e __end__ -e _bss_end__
__bss_end__
hello
>Fix:
Yes, please!
>Audit-Trail:
From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: bin/58849: usr.bin/grep is broken (TOOL_GREP, MKBSDGREP=yes)
Date: Sat, 23 Nov 2024 07:58:31 -0000 (UTC)
campbell+netbsd@mumble.net writes:
>$ { echo __bss_start__; echo __bss_end__; echo hello; } | nbgrep -Fvx -e _end -e _fini -e _init -e __bss_end__ -e __bss_start__ -e __end__ -e _bss_end__
>__bss_end__
>hello
# echo bla | nbgrep -x -e a -e bla
# echo bla | nbgrep -x -e bla -e a
bla
The bug is in util.c:procline(). The code iterates over all patterns
and continues the search where the previous search operation left
off (the pattern match advances pmatch.rm_so).
Checking for 'whole line' (-x) or 'word' (-w) is just a secondary test.
So when one pattern matches but doesn't qualify -x or -w, the following
patterns do not revisit the same place and may not match.
This bug already existed when the code was taken from FreeBSD in
rev 1.7, but the current FreeBSD code has fixed it and recalculates
where to continue the search.
From: "Roland Illig" <rillig@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/58849 CVS commit: src/tests/usr.bin/grep
Date: Sat, 23 Nov 2024 09:38:02 +0000
Module Name: src
Committed By: rillig
Date: Sat Nov 23 09:38:02 UTC 2024
Modified Files:
src/tests/usr.bin/grep: t_grep.sh
Log Message:
tests/grep: add expected output for grep -Fvx with overlapping patterns
By default, /usr/bin/grep is GNU grep, which does not exhibit this bug.
The tools build uses the grep implementation from usr.bin/grep, though.
See PR bin/58849.
To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/grep/t_grep.sh
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: Taylor R Campbell <riastradh@NetBSD.org>
To: Roland Illig <rillig@NetBSD.org>
Cc: gnats-bugs@NetBSD.org, netbsd-bugs@NetBSD.org
Subject: Re: PR/58849 CVS commit: src/tests/usr.bin/grep
Date: Sat, 23 Nov 2024 14:54:18 +0000
> Date: Sat, 23 Nov 2024 09:38:02 +0000
> From: "Roland Illig" <rillig@netbsd.org>
>
> Module Name: src
> Committed By: rillig
> Date: Sat Nov 23 09:38:02 UTC 2024
>
> Modified Files:
> src/tests/usr.bin/grep: t_grep.sh
>
> Log Message:
> tests/grep: add expected output for grep -Fvx with overlapping patterns
>
> By default, /usr/bin/grep is GNU grep, which does not exhibit this bug.
> The tools build uses the grep implementation from usr.bin/grep, though.
>
> See PR bin/58849.
Thanks! Might be worthwhile to grab whatever other automatic tests
FreeBSD and GNU grep have too, if you (or anyone else) feel inclined.
(Contact us)
$NetBSD: query-full-pr,v 1.49 2026/05/14 01:52:41 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.