Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-11-14
16:12
Fix [a6029e]: regexp with backreferences could loop forever check-in: 0edb559bd5 user: jan.nijtmans tags: core-9-0-branch
2026-10-17
07:16 • Ticket [a6029ee505] regexp hangs with backreferences when a possible empty match... artifact: 2ccfa0e829 user: serhiy.storchaka
2026-10-14
12:02
Fix [a6029e]: regexp with backreferences could loop forever check-in: ac36095ed4 user: jan.nijtmans tags: trunk, main
2026-10-10
19:29 • Ticket [a6029ee505] regexp hangs with backreferences when a possible empty match... artifact: 0b5eed668e user: serhiy.storchaka
19:27
Restore Tom Lane's original comments for the fix of [a6029ee505]. closed check-in: 6b79d40133 user: serhiy.storchaka tags: regexp-backref-search-loop
19:09
Fix [a6029ee505]: regexp with backreferences could loop forever when a possible empty match failed. ... check-in: afdec0627f user: serhiy.storchaka tags: regexp-backref-search-loop
18:47 • New ticket [a6029ee505] regexp hangs with backreferences when a possible empty m... artifact: f60cd72d52 user: serhiy.storchaka

regexp hangs with backreferences when a possible empty match fails

Opena6029ee50552e4e89fd57a4a18c83379b64da46cCreated 53 days agoUpdated 47 days ago
Descriptionwiki2026-10-10 18:47:21
These never return:
regexp {(b*)a\1|\1} ba      ;# should return 1 (matches "a")
regexp {$()|^\1} a          ;# should return 0
regexp {.. ()|\1} a         ;# should return 0
regexp {()*\1} a            ;# should return 1
The first one was found by comparing random patterns with backreferences against PostgreSQL; the other three are from the PostgreSQL regression tests.

complicatedFindLoop() asks the search DFA for the earliest possible match end close, tries every start point up to it, and when all of them fail, searches again from the same close. The DFA treats a backreference as matching anything, so it reports a possible empty match there again, and the loop never advances. This is only possible with backreferences. It started with the fix for [6585b21ca8] in 8.6.1; ()*\1 hangs since 8.6.5.

PostgreSQL fixed this in 2015, after the same change, in commit f2c4ffc330: advance close after a failed range (close++), and in citerdissect() try a real iteration before accepting zero iterations of an empty match, so that ()*\1 matches instead of failing. This fix was not included in the regexp bundle [497b93405b]. With both changes ported, all four examples give the PostgreSQL results, and the regexp tests pass.

Referenced by2
  • 6b79d40133Restore Tom Lane's original comments for the fix of [a6029ee505]. check-in2026-10-10 19:27:54
  • afdec0627fFix [a6029ee505]: regexp with backreferences could loop forever when a possible empty match failed. Advance the search after a failed range, and try a real iteration before accepting zero iterations in citerdissect(), as in PostgreSQL commit f2c4ffc330. New tests reg-14.32 to 14.36. check-in2026-10-10 19:09:29
serhiy.storchakawiki2026-10-10 19:29:32

Proposed fix in branch regexp-backref-search-loop: the port of PostgreSQL commit f2c4ffc330. Tests reg-14.32 to 14.36 (the four examples and ()+\1).

serhiy.storchakawiki2026-10-17 07:16:33

Merged into main in [ac36095ed4], not yet into 9.0 and 8.6. Besides the hang, it also changes which groups are set when an iteration matches an empty string: the engine now prefers one empty iteration to zero (as PostgreSQL does), so [regexp -indices -inline {((a?)*|(b?)*)+} aaa] gives {0 2} {3 2} {3 2} {-1 -1} on main, as in 8.4 to 8.6.4, and {0 2} {3 2} {-1 -1} {-1 -1} in 8.6.5 to 9.0 (since [c8dfe06653]).

Log in (anonymous is fine) to add a comment.