diff --git a/NEWS b/NEWS index b35e889..96818c3 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,13 @@ GNU sed NEWS -*- outline -*- ** Bug fixes + sed no longer rejects the documented GNU extension 'a**' (equivalent + to 'a*') in Basic Regular Expression (BRE) mode. Previously, this + worked only with -E (ERE mode), even though grep has always accepted + it in BRE mode. + [bug present since "the beginning"] + + sed no longer rejects "\c[" in regular expressions [bug present since the beginning] diff --git a/sed/regexp.c b/sed/regexp.c index 7c4af11..e52444c 100644 --- a/sed/regexp.c +++ b/sed/regexp.c @@ -52,6 +52,11 @@ compile_regex_1 (struct regex *new_regex, int needed_sub) syntax &= ~RE_DOT_NOT_NULL; syntax |= RE_NO_POSIX_BACKTRACKING; + /* As a GNU extension (for BRE), allow 'a**' to mean 'a*', + just as grep does via RE_SYNTAX_GREP. */ + if (!(extended_regexp_flags & REG_EXTENDED)) + syntax &= ~RE_CONTEXT_INVALID_DUP; + switch (posixicity) { case POSIXLY_EXTENDED: @@ -63,7 +68,7 @@ compile_regex_1 (struct regex *new_regex, int needed_sub) case POSIXLY_BASIC: syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS; if (!(extended_regexp_flags & REG_EXTENDED)) - syntax |= RE_LIMITED_OPS; + syntax |= RE_LIMITED_OPS | RE_CONTEXT_INVALID_DUP; break; } diff --git a/testsuite/bre-star-star.sh b/testsuite/bre-star-star.sh new file mode 100644 index 0000000..2e789b8 --- /dev/null +++ b/testsuite/bre-star-star.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# Ensure that a** works in BRE mode (GNU extension) + +# 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 + +unset POSIXLY_CORRECT +export LC_ALL=C + +# a** is a documented GNU extension meaning the same as a* +# and must work in BRE mode, not just in ERE mode. +printf '%s\n' 10 11 12 13 14 15 16 17 18 19 20 > in || framework_failure_ +printf '%s\n' 0 '' 2 3 4 5 6 7 8 9 20 > exp || framework_failure_ + +# BRE: was rejected before this fix +sed 's/1**//' in > out-bre || fail=1 +compare_ exp out-bre || fail=1 + +# ERE: always worked +sed -E 's/1**//' in > out-ere || fail=1 +compare_ exp out-ere || fail=1 + +# --posix BRE: must reject ** (not a POSIX feature) +cat <<\EOF > exp-err || framework_failure_ +sed: -e expression #1, char 7: Invalid preceding regular expression +EOF +returns_ 1 sed --posix 's/1**//' in 2>err || fail=1 +compare_ exp-err err || fail=1 + +Exit $fail diff --git a/testsuite/local.mk b/testsuite/local.mk index fda7c4b..62802a6 100644 --- a/testsuite/local.mk +++ b/testsuite/local.mk @@ -43,6 +43,7 @@ LOG_COMPILER = false T = \ testsuite/misc.pl \ + testsuite/bre-star-star.sh \ testsuite/bug80573.sh \ testsuite/bug32082.sh \ testsuite/bug32271-1.sh \