# GNU Grep core dump



## facedebouc (Feb 4, 2020)

Dear FreeBSD users,
I get core dump with stock GNU Grep with the following command line :
`cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Segmentation fault`

My system : FreeBSD 12.1-RELEASE-p2 GENERIC  amd64

testgrep.txt content :

```
Hello GNU Grep ;-)
Fuck GNU Grep !
```


----------



## SirDice (Feb 4, 2020)

```
dice@maelcum:~/test % cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Fuck GNU Grep !
dice@maelcum:~/test % uname -a
FreeBSD maelcum.dicelan.home 12.1-STABLE FreeBSD 12.1-STABLE r355710 MAELCUM  amd64
```


```
dice@wintermute:~/test % cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Fuck GNU Grep !
dice@wintermute:~/test % uname -a
FreeBSD wintermute.dicelan.home 12.1-RELEASE-p1 FreeBSD 12.1-RELEASE-p1 GENERIC  amd64
```

Noticed it was still p1, so I did a quick update:

```
dice@wintermute:~/test % cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Fuck GNU Grep !
dice@wintermute:~/test % uname -a
FreeBSD wintermute.dicelan.home 12.1-RELEASE-p2 FreeBSD 12.1-RELEASE-p2 GENERIC  amd64
```


----------



## facedebouc (Feb 4, 2020)

```
$ cat testgrep.txt | /usr/bin/bsdgrep -i -m1 -A20 Hello
Hello Gnu Grep ;-)
```
No core dump with BSD grep and I haven't got the same output than yours.


----------



## SirDice (Feb 4, 2020)

They seem to behave differently:

```
dice@maelcum:~/test % cat testgrep.txt | bsdgrep -i -m1 -A20 Hello
Hello GNU Grep ;-)
dice@maelcum:~/test % cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Fuck GNU Grep !
```


----------



## ljboiler (Feb 4, 2020)

I'm seeing the same grep segmentation fault here.

```
% uname -an
FreeBSD lt1.jimmy.localnet 12.1-STABLE FreeBSD 12.1-STABLE r357231 GENERIC  amd64
```


----------



## facedebouc (Feb 4, 2020)

Got the same problem with 2 machines with same release with ZFS.
When running a virtualbox VM with UFS, I haven't got the problem.
I will try to create a VM with ZFS for test.


----------



## facedebouc (Feb 4, 2020)

ljboiler said:


> I'm seeing the same grep segmentation fault here.
> 
> ```
> % uname -an
> ...


ZFS ?


----------



## ljboiler (Feb 4, 2020)

Nope - plain UFS.


----------



## facedebouc (Feb 4, 2020)

Strange


----------



## VladiBG (Feb 4, 2020)

`grep --v`


----------



## ljboiler (Feb 4, 2020)

```
grep (GNU grep) 2.5.1-FreeBSD
```
Interesting that the segfault doesn't happen when the requested max match count is increased to more than 1.


----------



## VladiBG (Feb 4, 2020)

i can't reproduce it on clean install under VM under csh

MD5 (/usr/bin/grep) = 989babfef5c0454b790bd2c9359fa686

grep (GNU grep) 2.5.1-FreeBSD

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


----------



## facedebouc (Feb 4, 2020)

Got the problem with login shell but not when using `su username` from root.
So got the problem with `su -l username`


----------



## facedebouc (Feb 4, 2020)

I get the problem with my locale setting in .login_conf :

```
me:\
    :charset=UTF-8:\
    :lang=fr_FR.UTF-8:
```


----------



## facedebouc (Feb 4, 2020)

What is the best way to get BSD grep by default ? 
Rename /usr/bin/grep and symlink to BSD grep ?
Does the system needs GNU grep ?
or just for me with an alias ?


----------



## facedebouc (Feb 4, 2020)

Whatever shell I am using, GNU grep is working with C locale :

```
% setenv LC_ALL C
% cat testgrep.txt | grep -i -m1 -A20 Hello
Hello GNU Grep ;-)
Fuck GNU Grep !
```


----------



## ljboiler (Feb 4, 2020)

Interesting...  I also have a UTF-8 locale by default.  If I remove that, no more segfault.


----------



## SirDice (Feb 5, 2020)

facedebouc said:


> What is the best way to get BSD grep by default ?
> Rename /usr/bin/grep and symlink to BSD grep ?


Build from source and set WITH_BSD_GREP in /etc/src.conf:

```
WITH_BSD_GREP
             Install BSD-licensed grep as '[ef]grep' instead of GNU grep.
```
_Caveat emptor_: Last time I tried this several ports failed to build with really weird errors. Same ports built fine with the GNU grep. This was quite some time ago, this issue may have been fixed in the mean time. But it's something to be aware off.


----------



## facedebouc (Feb 5, 2020)

SirDice said:


> Build from source and set WITH_BSD_GREP in /etc/src.conf:
> 
> ```
> WITH_BSD_GREP
> ...


I prefer to stick with GENERIC and binary updates. No time and resources to build world.


----------

