sed: --posix: fix escape handling after a named character class

With --posix, normalize_text tracks bracket-expression state to avoid
converting \X escapes inside bracket expressions.  It uses p[-1] and
p[-2] to detect the closing sequence of POSIX classes like [:alpha:].
However, normalize_text operates in-place with separate read (p)
and write (q) pointers, and when an earlier \n was collapsed from
two bytes to one, q falls behind p, so p[-1] and p[-2] read positions
already overwritten by q.

E.g., with '^A\n[[:alpha:]]\n*', the first \n shrinks, putting q one
byte behind p.  When processing the closing ']' in '[:alpha:]', p[-2]
reads ':' (written by q) rather than the original 'a', making the
"p[-2] != bracket_state" check fail.  Then, the second \n is never
converted to a newline.

* sed/compile.c (normalize_text): Use q[-1] and q[-2] for bracket
state tracking, not p[-1] and p[-2].
* NEWS: Mention this bug fix.
* testsuite/posix-bracket-esc.sh: New test.
* testsuite/local.mk (T): Add it.
Reported by Wolfgang Jeltsch in https://debbugs.gnu.org/77786
This commit is contained in:
Jim Meyering authored and Jim Meyering committed 2026-04-11 16:19:38 -07:00
1 parent 39c9ba576e
commit 53d12197e2
4 files changed
+64 -2

No files matched your search

+9
View File
@@ -4,6 +4,15 @@ GNU sed NEWS -*- outline -*-
** Bug fixes
In --posix mode, sed no longer mishandles backslash escapes (\n,
\t, \a, etc.) after a named character class like [[:alpha:]].
For example, 's/^A\n[[:alpha:]]\n*/XXX/' would fail to match the
trailing newline, treating \n as a literal backslash and an 'n'
rather than a newline. This happened when an earlier backslash
escape in the same regex had already been converted, shifting the
in-place normalization buffer.
[bug introduced in sed 4.9]
sed --debug no longer crashes when a label (":") command is compiled
before the --debug option is processed, e.g., sed -f<(...) --debug.
[bug introduced in sed 4.7 with --debug]
+2 -2
View File
@@ -1408,7 +1408,7 @@ convert:
case ':':
case '.':
case '=':
if (bracket_state == -1 && p[-1] == '[')
if (bracket_state == -1 && q[-1] == '[')
bracket_state = *p;
break;
@@ -1417,7 +1417,7 @@ convert:
;
else if (bracket_state == -1)
bracket_state = 0;
else if (p[-2] != bracket_state && p[-1] == bracket_state)
else if (q[-2] != bracket_state && q[-1] == bracket_state)
bracket_state = -1;
break;
}
+1
View File
@@ -76,6 +76,7 @@ T = \
testsuite/obinary.sh \
testsuite/panic-tests.sh \
testsuite/posix-backslash-warn.sh \
testsuite/posix-bracket-esc.sh \
testsuite/posix-char-class.sh \
testsuite/posix-mode-addr.sh \
testsuite/posix-mode-bad-ref.sh \
+52
View File
@@ -0,0 +1,52 @@
#!/bin/sh
# Ensure \n, \t, etc. after a named character class work with --posix
# 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=.}/testsuite/init.sh"; path_prepend_ ./sed
print_ver_ sed
export LC_ALL=C
# The bug: in --posix mode, normalize_text tracks bracket state using
# p[-1] and p[-2], but when an earlier \n was collapsed from 2 bytes
# to 1, those lookbacks read overwritten data. This left bracket_state
# stuck, preventing later \n from being converted to a newline.
printf '%s\n' A B C > in || framework_failure_
printf 'XXXC\n' > exp || framework_failure_
# The original bug report: [[:alpha:]] followed by \n*
sed --posix -E 'N; N; s/^A\n[[:alpha:]]\n*/XXX/' in > out1 2>/dev/null || fail=1
compare_ exp out1 || fail=1
# Other named classes must also work.
printf '%s\n' A 1 C > in2 || framework_failure_
sed --posix -E 'N; N; s/^A\n[[:digit:]]\n*/XXX/' in2 > out2 2>/dev/null || fail=1
compare_ exp out2 || fail=1
sed --posix -E 'N; N; s/^A\n[[:upper:]]\n*/XXX/' in > out3 2>/dev/null || fail=1
compare_ exp out3 || fail=1
# Other escapes (\t, not just \n) are also affected.
printf 'A\nB\tC\n' > in3 || framework_failure_
sed --posix -E 'N; s/^A\n[[:alpha:]]\t/XXX/' in3 > out4 2>/dev/null || fail=1
compare_ exp out4 || fail=1
# Without --posix, it always worked; verify no regression.
sed -E 'N; N; s/^A\n[[:alpha:]]\n*/XXX/' in > out5 || fail=1
compare_ exp out5 || fail=1
Exit $fail