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