Patch
Fix captures of non-greedy regex iterator on empty match
- Branch
- REL_19_STABLE
- Risk
- 30
- Base commit
- 5c839a2a08b9b87a349be97371fac6c5a2873b10
To apply it to a checkout of REL_19_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1e70f-f0db-72a1-a6f1-d8974ce5a428 | jq -r .body | git am
From 96250cb5933e9dbfc527de65df6fef32e7f2fbe3 Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Mon, 16 Nov 2026 17:05:51 -0800
Subject: [PATCH] Fix captures of non-greedy regex iterator on empty match
creviterdissect(), which dissects a match for an iteration node whose
child prefers the shortest match, returned REG_OKAY at once when zero
iterations were permitted and the substring to be matched was empty.
That skipped dissecting the child, so any capturing parentheses inside
the iteration stayed unset (rm_so = -1). A later back-reference to
such a group then failed in cbrdissect(), and the regex reported no
match although one exists, for example
SELECT 'x' ~ '^(a*?)??x\1$'; -- returned false, should be true
The greedy counterpart citerdissect() had the same shortcut until
commit f2c4ffc3307cab6619a28e77da9211416c8b1d83 made it prefer one
empty iteration, so that the captures get set, and fall back to zero
iterations only when the child cannot match at all. Do the same here,
so that greedy and non-greedy quantifiers differ only in which match is
preferred, not in whether a match is found.
Note that cdissect() dispatches on the SHORTER flag of the iteration
node's child, so a greedy quantifier applied to an atom that contains a
non-greedy quantifier, such as '(a*?)*', takes this path as well.
As with the earlier fix, a side effect is that a group under such a
quantifier that is able to match an empty string now reports an empty
match instead of not participating, which is what the greedy form has
done since that commit.
Oversight in commit 173e29aa5deefd9e71c183583ba37805c8102a72, which
introduced the iteration node, so this affects all supported branches.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit b9ac1a1b5f2b9b5485298e0472d6f07a113590ad)
---
src/backend/regex/regexec.c | 32 ++++++++++------
src/test/regress/expected/regex.out | 59 +++++++++++++++++++++++++++++
src/test/regress/sql/regex.sql | 15 ++++++++
3 files changed, 95 insertions(+), 11 deletions(-)
diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index 665aa31bd03..32ba1213f59 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -1343,20 +1343,19 @@ creviterdissect(struct vars *v,
MDEBUG(("%d: creviterdissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
/*
- * If zero matches are allowed, and target string is empty, just declare
- * victory. OTOH, if target string isn't empty, zero matches can't work
- * so we pretend the min is 1.
+ * For the moment, assume the minimum number of matches is 1. If zero
+ * matches are allowed, and the target string is empty, we are allowed to
+ * match regardless of the contents of the iter node --- but we would
+ * prefer to match once, so that capturing parens get set. (An example of
+ * the concern here is a pattern like "(a*?)??x\1", which needs the
+ * capture to be set to an empty string for the backref to succeed.) This
+ * is the same reasoning as in citerdissect. Therefore, we deal with the
+ * zero-matches case at the bottom, after failing to find any other way to
+ * match.
*/
min_matches = t->min;
if (min_matches <= 0)
- {
- if (begin == end)
- {
- MDEBUG(("%d: allowing zero matches\n", t->id));
- return REG_OKAY;
- }
min_matches = 1;
- }
/*
* We need workspace to track the endpoints of each sub-match. Normally
@@ -1506,8 +1505,19 @@ backtrack:
}
/* all possibilities exhausted */
- MDEBUG(("%d: failed\n", t->id));
FREE(endpts);
+
+ /*
+ * Now consider the possibility that we can match to a zero-length string
+ * by using zero repetitions.
+ */
+ if (t->min == 0 && begin == end)
+ {
+ MDEBUG(("%d: allowing zero matches\n", t->id));
+ return REG_OKAY;
+ }
+
+ MDEBUG(("%d: failed\n", t->id));
return REG_NOMATCH;
}
diff --git a/src/test/regress/expected/regex.out b/src/test/regress/expected/regex.out
index 698870f93ae..4a0cfaed904 100644
--- a/src/test/regress/expected/regex.out
+++ b/src/test/regress/expected/regex.out
@@ -565,6 +565,65 @@ select 'a' ~ '()+\1';
t
(1 row)
+-- Test that a non-greedy iteration that may match zero times still sets
+-- its capturing parens when matching an empty string, so that a later
+-- backref can succeed (compare '()*\1' above, which is the greedy case)
+select 'x' ~ '^(a*?)??x\1$' as t;
+ t
+---
+ t
+(1 row)
+
+select 'c' ~ 'c(a*?)??\1' as t;
+ t
+---
+ t
+(1 row)
+
+select 'y' ~ '(x*?){0,2}?y\1' as t;
+ t
+---
+ t
+(1 row)
+
+select 'x' ~ '^(a*?)*x\1$' as t;
+ t
+---
+ t
+(1 row)
+
+select 'aa' ~ '^(a*?)??a\1$' as f;
+ f
+---
+ f
+(1 row)
+
+select regexp_match('ab', '(a*?)??b\1');
+ regexp_match
+--------------
+ {""}
+(1 row)
+
+-- the group participates as an empty match, just as with a greedy quantifier
+select regexp_match('x', '^(a*?)??x$');
+ regexp_match
+--------------
+ {""}
+(1 row)
+
+select regexp_match('x', '^(a*)??x$');
+ regexp_match
+--------------
+ {""}
+(1 row)
+
+-- but not if the group cannot match an empty string
+select regexp_match('x', '^(a)??x$');
+ regexp_match
+--------------
+ {NULL}
+(1 row)
+
-- Test incorrect removal of capture groups within {0}
select 'xxx' ~ '(.){0}(\1)' as f;
f
diff --git a/src/test/regress/sql/regex.sql b/src/test/regress/sql/regex.sql
index 9a04fd1ac0e..b372de2984c 100644
--- a/src/test/regress/sql/regex.sql
+++ b/src/test/regress/sql/regex.sql
@@ -132,6 +132,21 @@ select 'a' ~ '.. ()|\1';
select 'a' ~ '()*\1';
select 'a' ~ '()+\1';
+-- Test that a non-greedy iteration that may match zero times still sets
+-- its capturing parens when matching an empty string, so that a later
+-- backref can succeed (compare '()*\1' above, which is the greedy case)
+select 'x' ~ '^(a*?)??x\1$' as t;
+select 'c' ~ 'c(a*?)??\1' as t;
+select 'y' ~ '(x*?){0,2}?y\1' as t;
+select 'x' ~ '^(a*?)*x\1$' as t;
+select 'aa' ~ '^(a*?)??a\1$' as f;
+select regexp_match('ab', '(a*?)??b\1');
+-- the group participates as an empty match, just as with a greedy quantifier
+select regexp_match('x', '^(a*?)??x$');
+select regexp_match('x', '^(a*)??x$');
+-- but not if the group cannot match an empty string
+select regexp_match('x', '^(a)??x$');
+
-- Test incorrect removal of capture groups within {0}
select 'xxx' ~ '(.){0}(\1)' as f;
select 'xxx' ~ '((.)){0}(\2)' as f;
base-commit: 5c839a2a08b9b87a349be97371fac6c5a2873b10
--
2.53.0