Patch
Fix regex iterations that need trailing empty matches.
- Branch
- master
- Risk
- –
- Base commit
- 8e27c9261c9dd6127e16ced998d37ed7a8cb2b7d
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d179-7807-7d32-aa69-95d4fc4a9603 | jq -r .body | git am
From 56e3a7f51fffdf30a173c342506fd1c39f7250e4 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 665aa31bd03..316a358d52c 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -1240,9 +1240,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));
@@ -1453,9 +1476,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 9fd503ffe78..4c45959563d 100644
--- a/src/test/regress/expected/regex.out
+++ b/src/test/regress/expected/regex.out
@@ -634,6 +634,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 a4f99c1f25f..a7a1e4742c1 100644
--- a/src/test/regress/sql/regex.sql
+++ b/src/test/regress/sql/regex.sql
@@ -149,6 +149,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: 8e27c9261c9dd6127e16ced998d37ed7a8cb2b7d
--
2.53.0