diff options
author | Jim Meyering <meyering@fb.com> | 2020-10-04 09:39:39 -0700 |
---|---|---|
committer | Jim Meyering <meyering@fb.com> | 2020-10-11 20:30:30 -0700 |
commit | df33ff807de40b131ba835ee4b0d27a0577a17ab (patch) | |
tree | 877177afbfe2d6c5b2805074d7a488fdac22a26a | |
parent | f31abf786f61f4bdd7134559a5f155fc9c8c2513 (diff) | |
download | grep-df33ff807de40b131ba835ee4b0d27a0577a17ab.tar.gz |
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.
-rw-r--r-- | NEWS | 7 | ||||
-rw-r--r-- | src/grep.c | 2 | ||||
-rw-r--r-- | src/pcresearch.c | 14 | ||||
-rw-r--r-- | src/search.h | 2 | ||||
-rwxr-xr-x | tests/filename-lineno.pl | 15 |
5 files changed, 32 insertions, 8 deletions
@@ -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] @@ -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 67989675..df91ee9d 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 af81dcb6..5d987162 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 d44dd7b7..42d1b046 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 }, ], |