From cf58accc5220d0179d25cbcb9490577779e780cf Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Mon, 30 Nov 2026 13:19:45 -0800
Subject: [PATCH v1] Fix regex captures of empty strings for the non-greedy
 case.

Previously, regexp_match('', '(a*?)*') returned {NULL} but should
return {""}. That also affected patterns containing back-references to
such groups, such as in '' ~ '^(a*?)*\1$', which would fail.

The identical problem was already fixed for greedy quantifiers back in
f2c4ffc330 (so the same pattern works without the '?'), but that
commit missed the non-greedy case. The fix is the same, except for
creviterdissect() instead of citerdissect().

Backpatch-through: 15
---
 src/backend/regex/regexec.c         | 25 ++++++++++++++-----------
 src/test/regress/expected/regex.out | 25 +++++++++++++++++++++++++
 src/test/regress/sql/regex.sql      |  6 ++++++
 3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index d8bb5c0e617..9333fe37103 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -1343,20 +1343,12 @@ 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.  See
+	 * citerdissect().
 	 */
 	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 +1498,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..d63ee3378d3 100644
--- a/src/test/regress/expected/regex.out
+++ b/src/test/regress/expected/regex.out
@@ -565,6 +565,31 @@ select 'a' ~ '()+\1';
  t
 (1 row)
 
+-- Test that groups capture zero-length strings
+select '' ~ '(a*)*\1';
+ ?column? 
+----------
+ t
+(1 row)
+
+select '' ~ '(a*?)*\1';
+ ?column? 
+----------
+ t
+(1 row)
+
+select regexp_match('', '(a*)*');
+ regexp_match 
+--------------
+ {""}
+(1 row)
+
+select regexp_match('', '(a*?)*');
+ regexp_match 
+--------------
+ {""}
+(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..c2a1b22e99c 100644
--- a/src/test/regress/sql/regex.sql
+++ b/src/test/regress/sql/regex.sql
@@ -132,6 +132,12 @@ select 'a' ~ '.. ()|\1';
 select 'a' ~ '()*\1';
 select 'a' ~ '()+\1';
 
+-- Test that groups capture zero-length strings
+select '' ~ '(a*)*\1';
+select '' ~ '(a*?)*\1';
+select regexp_match('', '(a*)*');
+select regexp_match('', '(a*?)*');
+
 -- Test incorrect removal of capture groups within {0}
 select 'xxx' ~ '(.){0}(\1)' as f;
 select 'xxx' ~ '((.)){0}(\2)' as f;
-- 
2.53.0

