Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 31993579eb3c5bc099df73bab13eaea588d99223
Ticket: 7d47ba7d9497fda88c064b6152d3a2126c3cd9d2
Stack overruns in regexp code
User & Date: tgl on 2015-10-17 03:38:02
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    There are various ways to drive the regexp code into stack overflow.  An example is that
    
    regexp {(^$)+} {z}
    
    will cause a stack overrun in markreachable() in 8.6.4.  (On my x86_64 machine, if I set "ulimit -s 1024" it takes about 4 minutes to get to this point, but it will get there.)  I believe this is driven by the problem discussed in http://core.tcl.tk/tcl/tktview?name=8f245009b0 which I plan to submit a fix for, but the point that concerns me right now is that there are multiple places that recurse to a depth limited only by the size of the regexp NFA, and if you don't have lots of stack space you are going to get a SIGSEGV on stack overflow.
    
    The Postgres project has decided to deal with this hazard by uniformly installing stack overflow checks in all recursive functions in the regex code; see
    http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=b63fc28776c5d2efdb4de326ad0f0b5b88f82220
    
    I would like to submit a Tcl-adapted version of that patch, but I do not know what implementation technology would be considered acceptable, so the point of this bug report is to ask.
    
    I see that you guys previously noted the possibility of stack overrun in duptraverse() and fixed it with a hard-wired recursion depth count limit.  That answer doesn't seem too appealing to me: it complicates the function's API and it's very unclear what number to use for the limit.  So propagating the same methodology into a dozen other places is not attractive.
    
    The technology Postgres is using depends on measuring the distance from a function's local variables to a stack base pointer established at execution startup.  I do not see how we can use that exact approach in Tcl, since Tcl is fundamentally a library not an application and can't run at execution start; but I wonder if it would be okay to decree that we'll allow a maximum stack depth of so-and-so-many kilobytes measured from the entry to regcomp() or regexec() respectively.  That could be mechanized by taking the address of the "struct vars" struct as being the measurement reference.
    
    Comments or better ideas appreciated.
    
  5. foundin changed to: "8.6.4"
  6. is_private changed to: "0"
  7. login: "tgl"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "26864b65f1c6ce8bda86f87ed9c554e19ac0390d"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "tgl"
  14. subsystem changed to: "43. Regexp"
  15. title changed to: "Stack overruns in regexp code"
  16. type changed to: "Bug"