Hello,

Following commit
https://git.busybox.net/busybox/commit/?id=c3295d233b6a7d924814eec9a5c5999a876daf9e
that landed in the 1.32.0 in 2020 fixed a grep behavior issue when -x was
specified together with an empty pattern file -f.

The patch adds the regex .* to the patterns when -x is set and -f pattern file
is empty. This becomes an issue only when -F (no regex) is used.
This doesn't look like a regression, because this issue was already there
before the above patch was applied.

I wrote a small script to illustrate the problem, which only happens when -F,
-x, together with an empty -f pattern file are specified.
Execute this script on busybox 1.31.1, 1.32.0 and above, GNU grep and BSD grep.
It shouldn't output anything, but it does.

Script output when using busybox 1.31.1 (before the above commit) :

> grep -f
> grep -xf
> hello
> grep -Ff
> grep -Fxf
> hello

Script output when using busybox 1.36.1 (after the above commit introduced in
1.32.0) :

> grep -f
> grep -xf
> grep -Ff
> grep -Fxf
> hello

Following the above fix, one of the hellos has (correctly) disappeared, but
there is still the last one that shouldn't be there.

Script output when using GNU grep 3.4 or GNU grep 3.11

> grep -f
> grep -xf
> grep -Ff
> grep -Fxf

Script output when using grep (BSD grep, GNU compatible) 2.6.0-FreeBSD

> grep -f
> grep -xf
> grep -Ff
> grep -Fxf



You will find the script below.

Best Regards,
Martin


#!/bin/sh
# Grep bug
# Strange interaction between -F, -x and -f flags when the -f file is empty
# Bug noticed on busybox 1.36.1 grep
# No bug found on GNU grep 3.4

:> patterns
echo "hello" > file

# According to the manual: -f file will match NOTHING if the given file is
# empty
# No pattern means no match.

# Should output nothing
echo "grep -f"
grep -f patterns file
# -> OK!

# Should output nothing
# Because adding -x shouldn't change anything:
# We'll work by line now, but I still didn't provide any pattern anyway
echo "grep -xf"
grep -xf patterns file
# -> OK!

# Should output nothing
# Adding -F shouldn't change anything: I promise I don't use regex, and it's
# the case...
# An empty string is not a regex, right?
echo "grep -Ff"
grep -Ff patterns file
# -> OK!

# Should output nothing
# Adding -F and -x shouldn't change anything...
echo "grep -Fxf"
grep -Fxf patterns file
# -> KO! It outputs something.
# I'm promising not to use regex with -F, but, because I use -x with -f,
# busybox adds a pattern ".*". But this is now a regex, internally! But due to
# the -F, this regex won't be understood...
# The user shouldn't be impacted by this now "internal regex".
# User is not aware that a regex was provided internally.
# https://elixir.bootlin.com/busybox/1.36.1/source/findutils/grep.c#L772
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to