|
|
04161d |
From 9618fb718b75920f37e5be2049ad1d0bb5c4a28c Mon Sep 17 00:00:00 2001
|
|
|
04161d |
From: Paul Eggert <eggert@cs.ucla.edu>
|
|
|
04161d |
Date: Tue, 26 Jan 2021 09:23:54 -0800
|
|
|
04161d |
Subject: [PATCH] expr: fix bug with unmatched \(...\)
|
|
|
04161d |
|
|
|
04161d |
Problem reported by Qiuhao Li.
|
|
|
04161d |
* doc/coreutils.texi (String expressions):
|
|
|
04161d |
Document the correct behavior, which POSIX requires.
|
|
|
04161d |
* src/expr.c (docolon): Treat unmatched \(...\) as empty.
|
|
|
04161d |
* tests/misc/expr.pl: New test.
|
|
|
04161d |
|
|
|
04161d |
Upstream-commit: 735083ba24878075235007b4417982ad5700436d
|
|
|
04161d |
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
|
|
|
04161d |
---
|
|
|
04161d |
doc/coreutils.texi | 14 ++++++++------
|
|
|
04161d |
src/expr.c | 9 +++++++--
|
|
|
04161d |
tests/misc/expr.pl | 3 +++
|
|
|
04161d |
3 files changed, 18 insertions(+), 8 deletions(-)
|
|
|
04161d |
|
|
|
04161d |
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
|
|
|
04161d |
index 2382a16..5b2bb2c 100644
|
|
|
04161d |
--- a/doc/coreutils.texi
|
|
|
04161d |
+++ b/doc/coreutils.texi
|
|
|
04161d |
@@ -13529,12 +13529,14 @@ second is considered to be a (basic, a la GNU @code{grep}) regular
|
|
|
04161d |
expression, with a @code{^} implicitly prepended. The first argument is
|
|
|
04161d |
then matched against this regular expression.
|
|
|
04161d |
|
|
|
04161d |
-If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
|
|
|
04161d |
-@code{:} expression returns the part of @var{string} that matched the
|
|
|
04161d |
-subexpression; otherwise, it returns the number of characters matched.
|
|
|
04161d |
-
|
|
|
04161d |
-If the match fails, the @code{:} operator returns the null string if
|
|
|
04161d |
-@samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
|
|
|
04161d |
+If @var{regex} does not use @samp{\(} and @samp{\)}, the @code{:}
|
|
|
04161d |
+expression returns the number of characters matched, or 0 if the match
|
|
|
04161d |
+fails.
|
|
|
04161d |
+
|
|
|
04161d |
+If @var{regex} uses @samp{\(} and @samp{\)}, the @code{:} expression
|
|
|
04161d |
+returns the part of @var{string} that matched the subexpression, or
|
|
|
04161d |
+the null string if the match failed or the subexpression did not
|
|
|
04161d |
+contribute to the match.
|
|
|
04161d |
|
|
|
04161d |
@kindex \( @r{regexp operator}
|
|
|
04161d |
Only the first @samp{\( @dots{} \)} pair is relevant to the return
|
|
|
04161d |
diff --git a/src/expr.c b/src/expr.c
|
|
|
04161d |
index e134872..0616a42 100644
|
|
|
04161d |
--- a/src/expr.c
|
|
|
04161d |
+++ b/src/expr.c
|
|
|
04161d |
@@ -721,8 +721,13 @@ docolon (VALUE *sv, VALUE *pv)
|
|
|
04161d |
/* Were \(...\) used? */
|
|
|
04161d |
if (re_buffer.re_nsub > 0)
|
|
|
04161d |
{
|
|
|
04161d |
- sv->u.s[re_regs.end[1]] = '\0';
|
|
|
04161d |
- v = str_value (sv->u.s + re_regs.start[1]);
|
|
|
04161d |
+ if (re_regs.end[1] < 0)
|
|
|
04161d |
+ v = str_value ("");
|
|
|
04161d |
+ else
|
|
|
04161d |
+ {
|
|
|
04161d |
+ sv->u.s[re_regs.end[1]] = '\0';
|
|
|
04161d |
+ v = str_value (sv->u.s + re_regs.start[1]);
|
|
|
04161d |
+ }
|
|
|
04161d |
}
|
|
|
04161d |
else
|
|
|
04161d |
{
|
|
|
04161d |
diff --git a/tests/misc/expr.pl b/tests/misc/expr.pl
|
|
|
04161d |
index e45f8e7..e57f79d 100755
|
|
|
04161d |
--- a/tests/misc/expr.pl
|
|
|
04161d |
+++ b/tests/misc/expr.pl
|
|
|
04161d |
@@ -84,6 +84,9 @@ my @Tests =
|
|
|
04161d |
# In 5.94 and earlier, anchors incorrectly matched newlines.
|
|
|
04161d |
['anchor', "'a\nb' : 'a\$'", {OUT => '0'}, {EXIT => 1}],
|
|
|
04161d |
|
|
|
04161d |
+ # In 8.32, \( ... \) that did not match caused memory errors.
|
|
|
04161d |
+ ['emptysub', '"a" : "\\(b\\)*"', {OUT => ''}, {EXIT => 1}],
|
|
|
04161d |
+
|
|
|
04161d |
# These tests are taken from grep/tests/bre.tests.
|
|
|
04161d |
['bre1', '"abc" : "a\\(b\\)c"', {OUT => 'b'}],
|
|
|
04161d |
['bre2', '"a(" : "a("', {OUT => '2'}],
|
|
|
04161d |
--
|
|
|
04161d |
2.26.2
|
|
|
04161d |
|