]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ptx: avoid infloop due to zero-length matches with -S regex
authorBernhard Voelker <mail@bernhard-voelker.de>
Wed, 13 Sep 2017 21:37:20 +0000 (23:37 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Thu, 14 Sep 2017 05:50:27 +0000 (07:50 +0200)
* src/ptx.c (find_occurs_in_text): Die with an appropriate error
diagnostic when the given regular expression returns a match of
length 0.
* tests/misc/ptx.pl (S-infloop): Add a test.
* NEWS (Bug fixes): Mention the fix.

Fixes https://bugs.gnu.org/28417 which was detected using
Symbolic Execution techniques developed in the course of the
SYMBIOSYS research project at COMSYS, RWTH Aachen University.

NEWS
src/ptx.c
tests/misc/ptx.pl

diff --git a/NEWS b/NEWS
index 8be22f169c0aeb8b751b909aac8a2dba0a9feb7f..ca36063eb5503ae50c64ddfdf43f6dc100d993c4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Bug fixes
+
+  ptx -S no longer infloops for a pattern which returns zero-length matches.
+  [the bug dates back to the initial implementation]
+
 
 * Noteworthy changes in release 8.28 (2017-09-01) [stable]
 
index 2aababff6db8c3b05c99913dc506af1270c92413..b7aa1079b2d2c272a1f6a3d43c4237bbf4885536 100644 (file)
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -818,6 +818,11 @@ find_occurs_in_text (int file_index)
           case -1:
             break;
 
+          case 0:
+            die (EXIT_FAILURE, 0,
+                 _("error: regular expression has a match of length zero: %s"),
+                 quote (context_regex.string));
+
           default:
             next_context_start = cursor + context_regs.end[0];
             break;
index d71d06500bdd608ebc41f39e3dc74a0f06feb6c9..4d4e1c7292c55397515ff89d0284428ee9cb2204 100755 (executable)
@@ -40,6 +40,12 @@ my @Tests =
                               {OUT=>".xx \"\" \"\" \"foo\" \"\"\n"}],
 ["format-t", '--format=tex',  {IN=>"foo\n"},
                               {OUT=>"\\xx {}{}{foo}{}{}\n"}],
+
+# with coreutils-8.28 and earlier, the -S option would infloop with
+# matches of zero-length.
+["S-infloop", '-S ^', {IN=>"a\n"}, {EXIT=>1},
+                      {ERR_SUBST=>'s/^.*reg.*ex.*length zero.*$/regexlzero/'},
+                      {ERR=>"regexlzero\n"}],
 );
 
 @Tests = triple_test \@Tests;