grep: Fix -f FILE when FILE is empty and -x provided
authorGray Wolf <wolf@wolfsden.cz>
Wed, 29 Apr 2020 13:49:17 +0000 (15:49 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 29 Apr 2020 14:01:42 +0000 (16:01 +0200)
Grep currently special-cased empty pattern file to be the same as
pattern file with one empty line (empty pattern). That does mirror how
GNU grep behaves, except when -x is provided. In that case .* pattern
needs to be used instead.

Signed-off-by: Gray Wolf <wolf@wolfsden.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
findutils/grep.c
testsuite/grep.tests

index 5b8644c36d9a7dce7d829479fe2256944f186515..84a6f7b1cdd8bc19a95eb2af2785e8ef42e8db6c 100644 (file)
@@ -762,8 +762,9 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
        if (option_mask32 & OPT_f) {
                load_regexes_from_file(fopt);
                if (!pattern_head) { /* -f EMPTY_FILE? */
-                       /* GNU grep treats it as "nothing matches" */
-                       llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
+                       /* GNU grep treats it as "nothing matches" except when -x */
+                       const char *data = (option_mask32 & OPT_x) ? ".*" : "";
+                       llist_add_to(&pattern_head, new_grep_list_data((char*)data, 0));
                        invert_search ^= 1;
                }
        }
index 26f8e69cfd44f08a68128a20035306cffa218bea..9c1f354994f1a6e38a3e85f9ed1ff5c3eadcfd2c 100755 (executable)
@@ -129,6 +129,12 @@ testing "grep -v -f EMPTY_FILE" \
        "" \
        "test\n"
 
+testing "grep -vxf EMPTY_FILE" \
+       "grep -vxf input" \
+       "test\n" \
+       "" \
+       "test\n"
+
 testing "grep -Fw matches only words" \
        "grep -Fw foo input" \
        "" \