Report
Backreference failure never retries an alternative match of an earlier sibling
- Severity
- 70
- Blamed commit
- 7bcc6d98fb5c3bda2787ae085ef3ff3dbb65ae42
On 24 branches
REL7_4_STABLE, REL8_0_STABLE, REL8_1_STABLE, REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Regex dissection keeps the first successful split or alternation branch. When a later backreference rejects that choice, the engine does not ask the earlier subexpression for another dissection of the same span. A pattern that does match therefore reports no match, and regexp_match() returns NULL.
'aa' ~ '^(?:(a)|(a))\2$' is false. The first branch captures group 1 and leaves group 2 unset, so \2 fails, and the second branch is never tried. 'aaxa' ~ '^(?:(a*)a*)x\1$' is false for the same reason: the concatenation inside the group is dissected once, as a long capture and an empty remainder, and that capture is not the text before 'x'. Direct siblings such as '^(a*)(a*)\1$' already work, because the midpoint search sits in the same concatenation as the backreference.
The same failure shows up under regexp_match() and the POSIX operators. A CHECK constraint or a filter written this way accepts or rejects the wrong rows. Rewriting the pattern so the ambiguous split is a direct sibling of the backreference avoids it. These patterns are uncommon, which is why the failure has stayed latent.
The behavior comes from the Spencer engine imported in 7bcc6d98fb5c (Replace regular expression package with Henry Spencer's latest version, 2003-02-05). ccondissect(), crevcondissect(), and caltdissect() still return on the first locally successful dissection. Later edits of those functions did not add a way to resume an earlier child. The same loops are on master and on REL_14_STABLE through REL_19_STABLE. Patches are attached for master and for REL_16_STABLE through REL_19_STABLE. REL_14_STABLE and REL_15_STABLE have those same first-success loops.
The documentation says a backreference matches the same string matched by the referenced subexpression. It does not say that only the first alternative, or only the first split, is considered.
On current master each of these is false. Each should be true.
SELECT 'aa' ~ '^(?:(a)|(a))\2$';
SELECT 'aa' ~ '^(a|(a))\2$';
SELECT 'aba' ~ '^(?:(ab)|(a)b)\2$';
SELECT 'aaxa' ~ '^(?:(a*)a*)x\1$';
SELECT 'aaxa' ~ '^(?:(a*?)a*?)x\1$';
SELECT 'aaa' ~ '^(?:(a)|(a)){2}\2$';
'aa' ~ '^(?:(a)|(a))\1$' stays true, and regexp_match() of that pattern returns {a,NULL}. With \2 the same call should return {NULL,a}. 'aa' ~ '^(?:(a)|(b))\2$' and 'ab' ~ '^(?:(a)|(a))\2$' stay false.
cdissect() walks the subre tree after the DFA has accepted a span. Each node returns one dissection. A parent whose right side then fails tries another midpoint, or the next alternation branch, but only a fresh dissection of the child. It never asks for a later dissection of a span that already succeeded, so nested ambiguity is invisible. Iteration has the same gap for the last repetition, the only one whose captures survive.
The fix gives cdissect() an nwant argument. Zero is still the first dissection in the existing preference order (longest or shortest match, then the first alternation branch). A larger value asks for a later dissection of the same span. Concatenation enumerates left and right dissections at each midpoint whose right-side DFA reaches the end, varying the right child first. Alternation enumerates the dissections of each DFA-matching branch before the next branch. Iteration does the same for the last repetition only, then tries other endpoints. Terminals and backreferences still have only one dissection. Abandoned captures are cleared with zaptreesubs() before the next attempt, so dfa_backref() does not see a stale match. The first success is unchanged when it already satisfies the rest of the pattern, so captures that were already correct do not move. A pattern that used to fail can now succeed. There is no catalog or WAL format change. Recomputing dissections from scratch can be expensive for a highly ambiguous pattern that also contains a backreference. That cost is the same class of worst case the engine already has when a backreference rejects many DFA candidates.
Severity 70. The nearest anchor is 80, because the match operators return a wrong boolean. The score is ten points lower because the patterns are obscure and an unambiguous rewrite is an easy workaround.