From: Ismail Ramzi <[email protected]>

re_search() returns the offset of the match, not its length, so a
return value of zero only means the end of sentence or end of line
separator matched at the start of the remaining text.  Treating that
as a zero-length match made ptx exit for ordinary input, e.g.,
"printf 'a\n\nb\n' | ptx -G".  Only a match whose end offset is zero
leaves the cursor unchanged, and that is the case which has to be
rejected to avoid looping forever.

* src/ptx.c (find_occurs_in_text): Diagnose a zero-length match by
checking the end of the match rather than re_search's return value.
* tests/ptx/context-regex.sh: New test.
* tests/local.mk (all_tests): Add it.
* NEWS: Mention the bug fix.

Link: https://github.com/coreutils/coreutils/pull/473
---
 NEWS                       |  5 +++++
 src/ptx.c                  | 14 +++++++------
 tests/local.mk             |  1 +
 tests/ptx/context-regex.sh | 40 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100755 tests/ptx/context-regex.sh

diff --git a/NEWS b/NEWS
index af956c2ad..ae06204b6 100644
--- a/NEWS
+++ b/NEWS
@@ -268,6 +268,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   the end of a heap buffer.
   [This bug was present in "the beginning".]
 
+  'ptx' no longer fails with a "regular expression has a match of length zero"
+  diagnostic when an end of sentence or end of line separator starts a
+  context, as happens with 'ptx -G' on input containing a blank line.
+  [bug introduced in coreutils-8.29]
+
   'realpath' without -s, or 'readlink' with -e,-f,-m no longer
   loop infinitely when resolving a symbolic link which
   resolves to a path beginning with the symbolic link itself.
diff --git a/src/ptx.c b/src/ptx.c
index 7a3e43561..50ac65111 100644
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -792,13 +792,15 @@ find_occurs_in_text (int file_index)
           case -1:
             break;
 
-          case 0:
-            error (EXIT_FAILURE, 0,
-                   _("error: regular expression has a match of length zero:"
-                     " %s"),
-                   quote (context_regex.string));
-
           default:
+            /* Only a match ending at offset zero leaves the cursor
+               unchanged, which would loop forever.  */
+            if (context_regs.end[0] == 0)
+              error (EXIT_FAILURE, 0,
+                     _("error: regular expression has a match of length zero:"
+                       " %s"),
+                     quote (context_regex.string));
+
             next_context_start = cursor + context_regs.end[0];
             break;
           }
diff --git a/tests/local.mk b/tests/local.mk
index 25a4127c1..78e2995f1 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -297,6 +297,7 @@ all_tests =                                 \
   tests/date/date-next-dow.pl                  \
   tests/ptx/ptx-overrun.sh                     \
   tests/ptx/word-regex-loop.sh                 \
+  tests/ptx/context-regex.sh                   \
   tests/misc/xstrtol.pl                                \
   tests/tail/overlay-headers.sh                        \
   tests/tail/pid.sh                            \
diff --git a/tests/ptx/context-regex.sh b/tests/ptx/context-regex.sh
new file mode 100755
index 000000000..7c55b73c7
--- /dev/null
+++ b/tests/ptx/context-regex.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Ensure a context separator matching at the start of a context is accepted
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ ptx
+
+# coreutils-8.29 through 9.12 treated a separator matching at the start of
+# the remaining text as a match of length zero, and exited with a bogus
+# "regular expression has a match of length zero" diagnostic.
+
+printf 'a\nb\n' > plain.in || framework_failure_
+printf 'a\n\nb\n' > blank.in || framework_failure_
+ptx -G plain.in > exp 2> err || fail=1
+compare /dev/null err || fail=1
+ptx -G blank.in > out 2> err || fail=1
+compare /dev/null err || fail=1
+compare exp out || fail=1
+
+# Likewise for the default end of sentence separator, with a line
+# holding just a period.
+printf 'x.\n.\ny\n' > dot.in || framework_failure_
+ptx dot.in > out 2> err || fail=1
+compare /dev/null err || fail=1
+
+Exit $fail
-- 
2.56.0


Reply via email to