Skip to content

Commit 0711461

Browse files
committed
Fix regexp misbehavior with capturing parens inside "{0}".
Regexps like "(.){0}...\1" drew an "invalid backreference number". That's not unreasonable on its face, since the capture group will never be matched if it's iterated zero times. However, other engines such as Perl's don't complain about this, nor do we throw an error for related cases such as "(.)|\1", even though that backref can never succeed either. Also, if the zero-iterations case happens at runtime rather than compile time --- say, "(x)*...\1" when there's no "x" to be found --- that's not an error, we just deem the backref to not match. Making this even less defensible, no error was thrown for nested cases such as "((.)){0}...\2"; and to add insult to injury, those cases could result in assertion failures instead. (It seems that nothing especially bad happened in non-assert builds, though.) Let's just fix it so that no error is thrown and instead the backref is deemed to never match, so that compile-time detection of no iterations behaves the same as run-time detection. Per report from Mark Dilger. This appears to be an aboriginal error in Spencer's library, so back-patch to all supported versions. Pre-v14, it turns out to also be necessary to back-patch one aspect of commits cb76fbd/00116dee5, namely to create capture-node subREs with the begin/end states of their subexpressions, not the current lp/rp of the outer parseqatom invocation. Otherwise delsub complains that we're trying to disconnect a state from itself. This is a bit scary but code examination shows that it's safe: in the pre-v14 code, if we want to wrap iteration around the subexpression, the first thing we do is overwrite the atom's begin/end fields with new states. So the bogus values didn't survive long enough to be used for anything, except if no iteration is required, in which case it doesn't matter. Discussion: https://postgr.es/m/A099E4A8-4377-4C64-A98C-3DEDDC075502@enterprisedb.com
1 parent 9a32717 commit 0711461

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/backend/regex/regcomp.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ parseqatom(struct vars *v,
958958
if (cap)
959959
{
960960
v->subs[subno] = atom;
961-
t = subre(v, '(', atom->flags | CAP, lp, rp);
961+
t = subre(v, '(', atom->flags | CAP, s, s2);
962962
NOERR();
963963
t->subno = subno;
964964
t->left = atom;
@@ -1041,11 +1041,23 @@ parseqatom(struct vars *v,
10411041
/* annoying special case: {0} or {0,0} cancels everything */
10421042
if (m == 0 && n == 0)
10431043
{
1044-
if (atom != NULL)
1045-
freesubre(v, atom);
1046-
if (atomtype == '(')
1047-
v->subs[subno] = NULL;
1048-
delsub(v->nfa, lp, rp);
1044+
/*
1045+
* If we had capturing subexpression(s) within the atom, we don't want
1046+
* to destroy them, because it's legal (if useless) to back-ref them
1047+
* later. Hence, just unlink the atom from lp/rp and then ignore it.
1048+
*/
1049+
if (atom != NULL && (atom->flags & CAP))
1050+
{
1051+
delsub(v->nfa, lp, atom->begin);
1052+
delsub(v->nfa, atom->end, rp);
1053+
}
1054+
else
1055+
{
1056+
/* Otherwise, we can clean up any subre infrastructure we made */
1057+
if (atom != NULL)
1058+
freesubre(v, atom);
1059+
delsub(v->nfa, lp, rp);
1060+
}
10491061
EMPTYARC(lp, rp);
10501062
return;
10511063
}

src/test/regress/expected/regex.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,25 @@ select 'a' ~ '()+\1';
567567
t
568568
(1 row)
569569

570+
-- Test incorrect removal of capture groups within {0}
571+
select 'xxx' ~ '(.){0}(\1)' as f;
572+
f
573+
---
574+
f
575+
(1 row)
576+
577+
select 'xxx' ~ '((.)){0}(\2)' as f;
578+
f
579+
---
580+
f
581+
(1 row)
582+
583+
select 'xyz' ~ '((.)){0}(\2){0}' as t;
584+
t
585+
---
586+
t
587+
(1 row)
588+
570589
-- Test ancient oversight in when to apply zaptreesubs
571590
select 'abcdef' ~ '^(.)\1|\1.' as f;
572591
f

src/test/regress/sql/regex.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ select 'a' ~ '.. ()|\1';
135135
select 'a' ~ '()*\1';
136136
select 'a' ~ '()+\1';
137137

138+
-- Test incorrect removal of capture groups within {0}
139+
select 'xxx' ~ '(.){0}(\1)' as f;
140+
select 'xxx' ~ '((.)){0}(\2)' as f;
141+
select 'xyz' ~ '((.)){0}(\2){0}' as t;
142+
138143
-- Test ancient oversight in when to apply zaptreesubs
139144
select 'abcdef' ~ '^(.)\1|\1.' as f;
140145
select 'abadef' ~ '^((.)\2|..)\2' as f;

0 commit comments

Comments
 (0)