sed: don't warn about escaped special characters with --posix

* NEWS: Mention the bug fix.
* sed/compile.c: Don't warn about special characters.
* testsuite/posix-backslash-warn.sh: Add a few test cases.
* THANKS.in: Update.
Reported by Karl Ove Hufthammer in https://bugs.gnu.org/81037
This commit is contained in:
Collin Funk authored and Jim Meyering committed 2026-05-15 13:29:25 -07:00
1 parent f8c5af67f3
commit 367d7e98f1
4 files changed
+46 -1

No files matched your search

+6
View File
@@ -2,6 +2,12 @@ GNU sed NEWS -*- outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
** Bug fixes
sed --posix no longer warns about escaped special characters in the
's' command.
[bug introduced in sed 4.10]
* Noteworthy changes in release 4.10 (2026-04-21) [stable]
+1
View File
@@ -38,6 +38,7 @@ Jakub Jelinek jakub@redhat.com
Jason Molenda crash@cygnus.com
Karl Berry karl@freefriends.org
Karl Heuer kwzh@gnu.org
Karl Ove Hufthammer karl@huftis.org
Kaveh R. Ghazi ghazi@caip.rutgers.edu
Kevin Buettner kev@cujo.geg.mot.com
Laurent Vogel lvl@club-internet.fr
+8 -1
View File
@@ -486,8 +486,15 @@ match_slash (int slash, bool regex, bool s_command)
/* Skip end-of-loop add1_buffer, we already did it. */
continue;
}
/* Warn for some non-portable backslash escapes if --posix is
in use. Note that we ignore any special characters, although
they may be non-portable in some contexts. */
char const *special_chars
= ((extended_regexp_flags & REG_EXTENDED)
? ".[\\()*+?{}|^$" : ".[\\*^$)({}");
if (s_command && posixicity != POSIXLY_EXTENDED && ch != '&'
&& ch != '\\' && !ISDIGIT (ch) && ch != '\n' && ch != slash)
&& ch != '\\' && !ISDIGIT (ch) && ch != '\n' && ch != slash
&& !strchr (special_chars, ch))
fprintf (stderr, _("%s: warning: using \"\\%c\" in the 's' "
"command is not portable\n"),
program_name, ch);
+31
View File
@@ -90,4 +90,35 @@ sed --posix 's/abc/\|/g' input6 > out6 2> out-err6 || fail=1
compare_ out6 exp-out6 || fail=1
compare_ out-err6 exp-err6 || fail=1
# Test that BRE interval expressions do not warn.
echo 'abc' > input7 || framework_failure_
cat << \EOF > exp-out7 || framework_failure_
def
EOF
sed --posix 's/abc\{0,1\}/def/g' input7 > out7 2> out-err7 || fail=1
compare_ out7 exp-out7 || fail=1
compare_ out-err7 /dev/null || fail=1
# Test that BRE subexpressions do not warn.
echo 'abc' > input8 || framework_failure_
cat << \EOF > exp-out8 || framework_failure_
def
EOF
sed --posix 's/ab\(c\)/def/g' input8 > out8 2> out-err8 || fail=1
compare_ out8 exp-out8 || fail=1
compare_ out-err8 /dev/null || fail=1
# Test that ERE special characters do not warn.
echo '.[\()*+?{}|^$' > input9
cat << \EOF > exp-out9 || framework_failure_
abc
EOF
sed -E --posix 's/^\.\[\\\(\)\*\+\?\{\}\|\^\$$/abc/g' \
input9 > out9 2> out-err9 || fail=1
compare_ out9 exp-out9 || fail=1
compare_ out-err9 /dev/null || fail=1
Exit $fail