Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: f60cd72d52bb96cc3cb23f56d339f78f3da9c157bd68a0c58d6b1fdb7e170da9
Ticket: a6029ee50552e4e89fd57a4a18c83379b64da46c
regexp hangs with backreferences when a possible empty match fails
User & Date: serhiy.storchaka on 2026-10-10 18:47:21
Changes

  1. cmimetype changed to: "text/x-fossil-wiki"
  2. comment changed to:
    These never return:
    <verbatim>
    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
    </verbatim>
    The first one was found by comparing random patterns with backreferences against PostgreSQL; the other three are from the PostgreSQL regression tests.
    
    <tt>complicatedFindLoop()</tt> asks the search DFA for the earliest possible match end <tt>close</tt>, tries every start point up to it, and when all of them fail, searches again from the same <tt>close</tt>. 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; <tt>()*\1</tt> hangs since 8.6.5.
    
    PostgreSQL fixed this in 2015, after the same change, in [https://github.com/postgres/postgres/commit/f2c4ffc330|commit f2c4ffc330]: advance <tt>close</tt> after a failed range (<tt>close++</tt>), and in <tt>citerdissect()</tt> try a real iteration before accepting zero iterations of an empty match, so that <tt>()*\1</tt> 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.
    
  3. foundin changed to: "8.6.18, 9.0.5, 9.1.0"
  4. login: "serhiy.storchaka"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. severity changed to: "Important"
  8. status changed to: "Open"
  9. submitter changed to: "serhiy.storchaka"
  10. subsystem changed to: "43. Regexp"
  11. title changed to:
    regexp hangs with backreferences when a possible empty match fails
    
  12. type changed to: "Bug"