Patch
Fix regex iterations that need trailing empty matches.
- Branch
- REL_14_STABLE
- Risk
- –
- Base commit
- 853b35f35b1c3e5b80d392568659662632e7eba8
To apply it to a checkout of REL_14_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d179-7808-749f-9511-58a79fb1de1a | jq -r .body | git am
From b19761b9ecf5b573ab3461e6e2ba4e275b2f2161 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Thu, 12 Nov 2026 12:24:57 -0800
Subject: [PATCH] Fix regex iterations that need trailing empty matches.
citerdissect() and creviterdissect() gave up once a quantified group
reached the end of its substring with fewer than the minimum number of
iterations. A group that contains a backreference can still match by
repeating an empty iteration, so patterns such as ()(\1){2} failed on
the empty string.
When the child DFA accepts the empty string at that point, pad the
division out to the minimum with zero-length iterations and let the
existing verifier reject a backreference that cannot actually match
empty.
---
src/backend/regex/regexec.c | 51 +++++++++++++++++++-
src/test/regress/expected/regex.out | 74 +++++++++++++++++++++++++++++
src/test/regress/sql/regex.sql | 15 ++++++
3 files changed, 138 insertions(+), 2 deletions(-)
diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index f42fcb43f6b..825e1220b72 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -1219,9 +1219,32 @@ citerdissect(struct vars *v,
* works so far as the child DFA can tell. If k is an allowed number
* of matches, start the slow part: recurse to verify each sub-match.
* We always have k <= max_matches, needn't check that.
+ *
+ * If the end is reached with fewer than min matches, the only
+ * remaining possibility is extra zero-length iterations there. The
+ * child DFA must accept the empty string at end; verification still
+ * rejects the division when a backreference cannot actually match
+ * empty.
*/
if (k < min_matches)
- goto backtrack;
+ {
+ chr *empty_end;
+
+ empty_end = longest(v, d, end, end, (int *) NULL);
+ if (ISERR())
+ {
+ FREE(endpts);
+ return v->err;
+ }
+ if (empty_end != end)
+ goto backtrack;
+
+ while (k < min_matches)
+ {
+ k++;
+ endpts[k] = end;
+ }
+ }
MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k));
@@ -1432,9 +1455,33 @@ creviterdissect(struct vars *v,
* works so far as the child DFA can tell. If k is an allowed number
* of matches, start the slow part: recurse to verify each sub-match.
* We always have k <= max_matches, needn't check that.
+ *
+ * If the end is reached with fewer than min matches, the only
+ * remaining possibility is extra zero-length iterations there. The
+ * child DFA must accept the empty string at end; verification still
+ * rejects the division when a backreference cannot actually match
+ * empty.
*/
if (k < min_matches)
- goto backtrack;
+ {
+ chr *empty_end;
+
+ empty_end = shortest(v, d, end, end, end,
+ (chr **) NULL, (int *) NULL);
+ if (ISERR())
+ {
+ FREE(endpts);
+ return v->err;
+ }
+ if (empty_end != end)
+ goto backtrack;
+
+ while (k < min_matches)
+ {
+ k++;
+ endpts[k] = end;
+ }
+ }
MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k));
diff --git a/src/test/regress/expected/regex.out b/src/test/regress/expected/regex.out
index ae0de7307db..acd75e2e2fe 100644
--- a/src/test/regress/expected/regex.out
+++ b/src/test/regress/expected/regex.out
@@ -636,6 +636,80 @@ select regexp_match('foo', '(?:.|){99}');
{foo}
(1 row)
+-- Quantified group containing a backreference, minimum >= 2, must be
+-- allowed to finish with empty iterations.
+select '' ~ '()(\1){2}' as t;
+ t
+---
+ t
+(1 row)
+
+select 'xy' ~ '^x()(\1){2}y$' as t;
+ t
+---
+ t
+(1 row)
+
+select 'b' ~ '^(a*)(\1){2}b$' as t;
+ t
+---
+ t
+(1 row)
+
+select 'aa' ~ '^((a?)\2){2}$' as t;
+ t
+---
+ t
+(1 row)
+
+select 'aa' ~ '^((a?)\2){3}$' as t;
+ t
+---
+ t
+(1 row)
+
+select '' ~ '()(\1){2}?' as t;
+ t
+---
+ t
+(1 row)
+
+select 'a' ~ '^(a)(\1){2}$' as f;
+ f
+---
+ f
+(1 row)
+
+select 'b' ~ '^(a+)(\1){2}b$' as f;
+ f
+---
+ f
+(1 row)
+
+select 'aaa' ~ '^(a)(\1){2}$' as t;
+ t
+---
+ t
+(1 row)
+
+select regexp_match('aa', '^((a?)\2){2}$');
+ regexp_match
+--------------
+ {"",""}
+(1 row)
+
+select regexp_match('aa', '^((a?)\2){2}?$');
+ regexp_match
+--------------
+ {"",""}
+(1 row)
+
+select regexp_match('', '()(\1){2}');
+ regexp_match
+--------------
+ {"",""}
+(1 row)
+
-- Error conditions
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
ERROR: invalid regular expression: invalid backreference number
diff --git a/src/test/regress/sql/regex.sql b/src/test/regress/sql/regex.sql
index 56217104ce6..ac83f4bc2e4 100644
--- a/src/test/regress/sql/regex.sql
+++ b/src/test/regress/sql/regex.sql
@@ -152,6 +152,21 @@ select regexp_match('fooba', '(?:..)*');
select regexp_match('xyz', repeat('.', 260));
select regexp_match('foo', '(?:.|){99}');
+-- Quantified group containing a backreference, minimum >= 2, must be
+-- allowed to finish with empty iterations.
+select '' ~ '()(\1){2}' as t;
+select 'xy' ~ '^x()(\1){2}y$' as t;
+select 'b' ~ '^(a*)(\1){2}b$' as t;
+select 'aa' ~ '^((a?)\2){2}$' as t;
+select 'aa' ~ '^((a?)\2){3}$' as t;
+select '' ~ '()(\1){2}?' as t;
+select 'a' ~ '^(a)(\1){2}$' as f;
+select 'b' ~ '^(a+)(\1){2}b$' as f;
+select 'aaa' ~ '^(a)(\1){2}$' as t;
+select regexp_match('aa', '^((a?)\2){2}$');
+select regexp_match('aa', '^((a?)\2){2}?$');
+select regexp_match('', '()(\1){2}');
+
-- Error conditions
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
select 'xyz' ~ 'x(\w)(?=(\1))';
base-commit: 853b35f35b1c3e5b80d392568659662632e7eba8
--
2.53.0