Bug 193835 - bug in GNU grep with -m and -A
Summary: bug in GNU grep with -m and -A
Status: Closed Overcome By Events
Alias: None
Product: Base System
Classification: Unclassified
Component: gnu (show other bugs)
Version: 10.0-STABLE
Hardware: Any Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-22 13:44 UTC by aborche
Modified: 2023-05-01 13:51 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description aborche 2014-09-22 13:44:52 UTC
grep segfault

create a file test.txt
-------------
text
text
<blank line>
text
text
-------------

run command: grep -m 1 -A 2 -e '^$' test.txt
> Segmentation Fault (core dumped)

in file test.txt add to end blank line

run grep again.
Result: none

Change -m1 to -m2
Result: all is ok
Comment 1 aborche 2014-09-22 13:54:36 UTC
http://dpaste.com/3VPJ869
Comment 2 Veniamin Gvozdikov freebsd_committer freebsd_triage 2014-09-22 14:35:47 UTC
Copied gdb output from link, because it will expired through 1 day.

main:[~] %> gdb grep
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "amd64-marcel-freebsd"...(no debugging symbols found)...
(gdb) r -m 1 -A 2 -e '^$' text.test
Starting program: /usr/bin/grep -m 1 -A 2 -e '^$' text.test
(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...

Program received signal SIGSEGV, Segmentation fault.
0x0000000800fb1a60 in memchr () from /lib/libc.so.7
(gdb) thread apply all bt
(gdb) bt
#0  0x0000000800fb1a60 in memchr () from /lib/libc.so.7
#1  0x000000000040ff92 in ?? ()
#2  0x000000000040d090 in ?? ()
#3  0x000000000040c5c9 in ?? ()
#4  0x000000000040bd52 in ?? ()
#5  0x000000000040260f in ?? ()
#6  0x0000000800633000 in ?? ()
#7  0x0000000000000000 in ?? ()
(gdb) f 0
#0  0x0000000800fb1a60 in memchr () from /lib/libc.so.7
(gdb)
Comment 3 Kyle Evans freebsd_committer freebsd_triage 2017-04-20 17:19:19 UTC
This is indeed an issue with gnugrep, and not with libgnuregex. This one goes away when bsdgrep is installed as /usr/bin/grep, and can safely go away at that time.
Comment 4 commit-hook freebsd_committer freebsd_triage 2020-12-08 14:05:58 UTC
A commit references this bug:

Author: kevans
Date: Tue Dec  8 14:05:26 UTC 2020
New revision: 368439
URL: https://svnweb.freebsd.org/changeset/base/368439

Log:
  src.opts.mk: switch to bsdgrep as /usr/bin/grep

  This has been years in the making, and we all knew it was bound to happen
  some day. Switch to the BSDL grep implementation now that it's been a
  little more thoroughly tested and theoretically supports all of the
  extensions that gnugrep in base had with our libregex(3).

  Folks shouldn't really notice much from this update; bsdgrep is slower than
  gnugrep, but this is currently the price to pay for fewer bugs. Those
  dissatisfied with the speed of grep and in need of a faster implementation
  should check out what textproc/ripgrep and textproc/the_silver_searcher
  can do for them.

  I have some WIP to make bsdgrep faster, but do not consider it a blocker
  when compared to the pros of switching now (aforementioned bugs, licensing).

  PR:		228798 (exp-run)
  PR:		128645, 156704, 166842, 166862, 180937, 193835, 201650
  PR:		232565, 242308, 246000, 251081, 191086, 194397
  Relnotes:	yes, please

Changes:
  head/share/mk/src.opts.mk
Comment 5 Alexey Dokuchaev freebsd_committer freebsd_triage 2023-03-03 04:52:27 UTC
Still happens on 12.4-STABLE with GNU grep 2.5.1 (stable/12 branch EoLs on 31 December 2023).
Comment 6 Ed Maste freebsd_committer freebsd_triage 2023-05-01 13:51:20 UTC
Fixed by the switch to BSD grep in FreeBSD 13+. This is not fixed in 12.x but as there are no more releases to come it will unfortunately remain unfixed there.