[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    grep: Fix -f FILE when FILE is empty and -x provided
From:       Wolf <wolf () wolfsden ! cz>
Date:       2020-04-27 12:19:38
Message-ID: 20200427121939.545871-1-wolf () wolfsden ! cz
[Download RAW message or body]

Hello,

while implementing whitelist mechanism in my shell scripting, I've
noticed an inconsistency between busybox's grep and GNU grep. They
handle -xf EMPTY_FILE flags differently when EMPTY_FILE is well...
empty.

GNU grep in that case handles it as if /.*/ pattern was provided, while
busybox uses //.

Attached patch fixes this issue. I've both added a test case into grep's
testsuite and I've also used following shell script to verify that the
behavior does match now:



#!/bin/sh
set -eu

# Sanity checks if this is run outside of my setup
[ ! -x ./busybox ] && ( echo >&2 './busybox missing'; exit 1 )
if ! grep --version | head -n1 | grep -Eq '^grep \(GNU grep\) '; then
	echo >&2 'grep is not GNU grep'
	exit 1
fi

[ -f empty ] && rm empty
[ -f blank ] && rm blank

touch empty
echo >blank

# Yeah yeah GNU grep is not coreutils, but core and busy both have 4 letters so
# it all nicely aligns.
tmpdir=/tmp/grep_test
bpref="$tmpdir/busy."
cpref="$tmpdir/core."

[ -d "$tmpdir" ] && rm -r "$tmpdir"
mkdir -p "$tmpdir"

busy_grep() {
	./busybox grep "$@"
}

core_grep() {
	grep "$@"
}

data() {
	echo a
	echo b
	echo c
}

cmp() (
	printf 'Test: %s ... ' "$*"

	data | busy_grep "$@" >"$bpref$$" || :
	data | core_grep "$@" >"$cpref$$" || :

	if diff -u "$bpref$$" "$cpref$$" >/dev/null; then
		echo 'OK'
	else
		echo 'FAIL'
		diff -u "$bpref$$" "$cpref$$" || :
	fi
)

cmp -Evxf ./empty
cmp -Evxf ./blank

cmp -Evf ./empty
cmp -Evf ./blank

cmp -Exf ./empty
cmp -Exf ./blank

cmp -vxf ./empty
cmp -vxf ./blank

cmp -Ef ./empty
cmp -Ef ./blank

cmp -vf ./empty
cmp -vf ./blank

cmp -xf ./empty
cmp -xf ./blank

cmp -f ./empty
cmp -f ./blank



With output being:

+   $ ./test.sh
Test: -Evxf ./empty ... OK
Test: -Evxf ./blank ... OK
Test: -Evf ./empty ... OK
Test: -Evf ./blank ... OK
Test: -Exf ./empty ... OK
Test: -Exf ./blank ... OK
Test: -vxf ./empty ... OK
Test: -vxf ./blank ... OK
Test: -Ef ./empty ... OK
Test: -Ef ./blank ... OK
Test: -vf ./empty ... OK
Test: -vf ./blank ... OK
Test: -xf ./empty ... OK
Test: -xf ./blank ... OK
Test: -f ./empty ... OK
Test: -f ./blank ... OK


_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic