grep-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] grep: -P: report input filename upon PCRE execution failure


From: Jim Meyering
Subject: [PATCH] grep: -P: report input filename upon PCRE execution failure
Date: Sun, 11 Oct 2020 20:36:18 -0700

I've just pushed the following:

grep: -P: report input filename upon PCRE execution failure

Without this, it could be tedious to determine which input
file evokes a PCRE-execution-time failure.
* src/pcresearch.c (Pexecute): When failing, include the
error-provoking file name in the diagnostic.
* src/grep.c (input_filename): Make extern, since used above.
* src/search.h (input_filename): Declare.
* tests/filename-lineno.pl: Test for this.
($no_pcre): Factor out.
* NEWS (Bug fixes): Mention this.

diff --git a/NEWS b/NEWS
index cc2d8f7..ac78589 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,13 @@ GNU grep NEWS                                    -*- outline 
-*-

 * Noteworthy changes in release ?.? (????-??-??) [?]

+** Bug fixes
+
+  grep -P now reports the troublesome input filename upon PCRE execution
+  failure.  Before, searching many files for something rare might fail with
+  just "exceeded PCRE's backtracking limit".  Now, it also reports which file
+  triggered the failure.
+

 * Noteworthy changes in release 3.5 (2020-09-27) [stable]

diff --git a/src/grep.c b/src/grep.c
index de7616a..bd6ca53 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -641,7 +641,7 @@ typedef size_t (*execute_fp_t) (void *, char const *, 
size_t, size_t *,
 static execute_fp_t execute;
 static void *compiled_pattern;

-static char const *
+char const *
 input_filename (void)
 {
   if (!filename)
diff --git a/src/pcresearch.c b/src/pcresearch.c
index 6798967..df91ee9 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -303,25 +303,29 @@ Pexecute (void *vcp, char const *buf, size_t size, size_t 
*match_size,
           break;

         case PCRE_ERROR_NOMEMORY:
-          die (EXIT_TROUBLE, 0, _("memory exhausted"));
+          die (EXIT_TROUBLE, 0, _("%s: memory exhausted"), input_filename ());

 #if PCRE_STUDY_JIT_COMPILE
         case PCRE_ERROR_JIT_STACKLIMIT:
-          die (EXIT_TROUBLE, 0, _("exhausted PCRE JIT stack"));
+          die (EXIT_TROUBLE, 0, _("%s: exhausted PCRE JIT stack"),
+               input_filename ());
 #endif

         case PCRE_ERROR_MATCHLIMIT:
-          die (EXIT_TROUBLE, 0, _("exceeded PCRE's backtracking limit"));
+          die (EXIT_TROUBLE, 0, _("%s: exceeded PCRE's backtracking limit"),
+               input_filename ());

         case PCRE_ERROR_RECURSIONLIMIT:
-          die (EXIT_TROUBLE, 0, _("exceeded PCRE's recursion limit"));
+          die (EXIT_TROUBLE, 0, _("%s: exceeded PCRE's recursion limit"),
+               input_filename ());

         default:
           /* For now, we lump all remaining PCRE failures into this basket.
              If anyone cares to provide sample grep usage that can trigger
              particular PCRE errors, we can add to the list (above) of more
              detailed diagnostics.  */
-          die (EXIT_TROUBLE, 0, _("internal PCRE error: %d"), e);
+          die (EXIT_TROUBLE, 0, _("%s: internal PCRE error: %d"),
+               input_filename (), e);
         }

       return -1;
diff --git a/src/search.h b/src/search.h
index af81dcb..5d98716 100644
--- a/src/search.h
+++ b/src/search.h
@@ -82,6 +82,8 @@ mb_clen (char const *s, size_t n, mbstate_t *mbs)
   return len == (size_t) -2 ? mbrlen (s, n, mbs) : len;
 }

+extern char const *input_filename (void);
+
 _GL_INLINE_HEADER_END

 #endif /* GREP_SEARCH_H */
diff --git a/tests/filename-lineno.pl b/tests/filename-lineno.pl
index d44dd7b..42d1b04 100755
--- a/tests/filename-lineno.pl
+++ b/tests/filename-lineno.pl
@@ -37,6 +37,8 @@ $prog = $full_prog_name if $full_prog_name;
 # Transform each to this: "Unmatched [..."
 my $err_subst = {ERR_SUBST => 's/(: Unmatched \[).*/$1.../'};

+my $no_pcre = "$prog: Perl matching not supported in a --disable-perl-regexp 
build\n";
+
 my @Tests =
   (
    # Show that grep now includes filename:lineno in the diagnostic:
@@ -100,13 +102,22 @@ my @Tests =
    ['invalid-re-P-paren', '-P ")"', {EXIT=>2},
     {ERR => $ENV{PCRE_WORKS} == 1
        ? "$prog: unmatched parentheses\n"
-       : "$prog: Perl matching not supported in a --disable-perl-regexp 
build\n"
+       : $no_pcre
     },
    ],
    ['invalid-re-P-star-paren', '-P "a.*)"', {EXIT=>2},
     {ERR => $ENV{PCRE_WORKS} == 1
        ? "$prog: unmatched parentheses\n"
-       : "$prog: Perl matching not supported in a --disable-perl-regexp 
build\n"
+       : $no_pcre
+    },
+   ],
+
+   # Prior to grep-3.6, the name of the offending file was not printed.
+   ['backtracking-with-file', '-P "((a+)*)+$"', {EXIT=>2},
+    {IN=>{f=>"a"x20 ."b"}},
+    {ERR => $ENV{PCRE_WORKS} == 1
+       ? "$prog: f: exceeded PCRE's backtracking limit\n"
+       : $no_pcre
     },
    ],



reply via email to

[Prev in Thread] Current Thread [Next in Thread]