diff --git a/NEWS b/NEWS index c72761a..96f7bac 100644 --- a/NEWS +++ b/NEWS @@ -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] diff --git a/sed/compile.c b/sed/compile.c index bc7e9dc..a55b1f6 100644 --- a/sed/compile.c +++ b/sed/compile.c @@ -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; } diff --git a/testsuite/local.mk b/testsuite/local.mk index bebd0fa..41ea1a0 100644 --- a/testsuite/local.mk +++ b/testsuite/local.mk @@ -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 \ diff --git a/testsuite/posix-bracket-esc.sh b/testsuite/posix-bracket-esc.sh new file mode 100644 index 0000000..c644c20 --- /dev/null +++ b/testsuite/posix-bracket-esc.sh @@ -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 . +. "${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