Artifacts Associated With Ticket 3604074fffffffff
Ticket change [1d1bfdbf18] (rid 90736) by bjorngrathwohl on 2013-02-11 11:04:01:
- assignee initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
When I perform a match with five or more nested kleene stars, like the regular expression (((((a)*)*)*)*)* on the input string "a", Tcl goes into an infinite loop. When there are four or fewer kleene-stars, it does not happen. If I change the expressions to use non-grouping parentheses, like (?:(?:(?:(?:(?:a)*)*)*)*)*, the problem disappears. % set exp "(((((a)*)*)*)*)*" (((((a)*)*)*)*)* % [regex $exp "a"] <LOOP>
- foundin initialized to: "None"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "bjorngrathwohl"
- subsystem initialized to: "None"
- title initialized to: "Inifinite loop when matching regular expression"
- type initialized to: "Bug"
Ticket change [6256bb4b1c] (rid 92398) by sebres on 2013-02-11 19:02:06:
- icomment:
More than strange use of repeatable group match. Of course, the infinite loop is not really expected behavior here, but why '*' again after '*' of self? Just for your info: % regexp -inline {((((a)*)))} a a a a a a % regexp -inline {((((a)*)*)*)*} a a {} {} {} {} - login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [92d5e63812] (rid 105711) by bjorngrathwohl on 2013-02-11 19:55:48:
- icomment:
Yes, I know that it is not a very realistic use of grouping parentheses, but I still think that it is a problem that you can cause an infinite loop in your program with this, rather innocent-looking, expression... Especially when it doesn't loop when given the expression ((((a)*)*)*)*.
- login: "bjorngrathwohl"
- mimetype: "text/plain"
- username: "bjorngrathwohl"
- icomment:
Ticket change [b734cbf98e] (rid 125545) by dgp on 2013-02-11 20:27:15:
- icomment:
What release of Tcl?
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [c5941d7a29] (rid 112022) by dkf on 2013-02-11 20:29:24:
- subsystem changed to: "43. Regexp"
Ticket change [8730c7c74a] (rid 131653) by bjorngrathwohl on 2013-02-11 20:34:54:
- icomment:
I tried on version 8.6, downloaded as binary from http://www.activestate.com/activetcl/downloads, and version 8.5, which came bundled with OS X (I think, don't remember installling it...) I normally don't use Tcl myself.
- login: "bjorngrathwohl"
- mimetype: "text/plain"
- username: "bjorngrathwohl"
- icomment:
Ticket change [41cfae7864] (rid 116253) by dkf on 2013-02-11 20:47:43:
- icomment:
I've confirmed it in 8.4.7, 8.5.2 and 8.6 (trunk).
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [7ee546c761] (rid 97485) by dkf on 2013-02-11 20:54:24:
- icomment:
Problem *might* be in fixempties() in regc_nfa.c, or maybe in unempty() (called from fixempties). Partial stack calling probe (with trunk, numbers are count of samples): 2649 Tcl_RegexpObjCmd 2649 Tcl_GetRegExpFromObj 2649 CompileRegexp 2649 TclReComp 2649 nfatree 2649 nfatree 2649 nfatree 2649 nfatree 2649 nfatree 2649 nfanode 2649 optimize 2649 fixempties 2444 unempty 2367 copyouts 2165 cparc 2078 newarc 2061 newarc 17 allocarc 17 allocarc 87 cparc 202 copyouts 60 freearc 60 freearc 17 unempty 205 fixempties - login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [3fde66a9a7] (rid 121534) by dgp on 2013-02-11 21:51:45:
- icomment:
Loop is in fixempties().
- login: "dgp"
- mimetype: "text/plain"
- subsystem changed to: "None"
- username: "dgp"
- icomment:
Ticket change [38312e496e] (rid 112159) by dkf on 2013-02-11 22:03:12:
- foundin changed to: "obsolete: 8.4.7"
- priority changed to: "7 High"
- subsystem changed to: "43. Regexp"
Ticket change [e1cd6e0766] (rid 87516) by dkf on 2013-02-11 22:08:51:
- icomment:
That's where the loop is, but I don't know if it is where the problem is. :-) In particular, fixempties() and unempty() are rather familiar with each other, so I can't tell where the bug is between those two. (It might be that a normally-not-taken arm of unempty() is returning the wrong value.) I have *no* idea why the problem only happens when we get to depth 5. For all I know, it could be some compensatory code elsewhere that no longer applies. $ make shell [...] % regexp -about ((((a)*)*)*)* 4 REG_UEMPTYMATCH % regexp -about (((((a)*)*)*)*)* ^Cmake: *** [shell] Interrupt Did I mention before that we really need either a proper RE maintainer, or to switch to a different RE engine?
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [604d5fefe3] (rid 115968) by sebres on 2013-02-11 22:40:33:
- icomment:
Maybe just throw an error here, like python? # re.match('((a)*)', 'a').groups() ('a', 'a') # re.match('((a)*)*', 'a').groups() raise error, v # invalid expression error: nothing to repeat >[dkf] switch to a different RE engine I see a big compatibility problem here - many RE engines are incompatible to TCL RE. For example (python, java etc.) follow by "OR"-expression a "boolean" rules "first wins" instead of "hungry - large wins": [python] # re.match('(ab|abc)(cd|d)', 'abcd').groups() ('ab', 'cd') [tcl] % regexp -inline {(ab|abc)(cd|d)} abcd abcd abc d or see 3574242 for example (I do not know whether this is a bug): [tcl] % regexp -inline {((?:ab|abc){1}?)(cd|d){1}?} abcd abcd abc d % regexp -inline {((?:ab|abc){1,1}?)(cd|d){1,1}?} abcd abcd ab cd [python] # re.match('((?:ab|abc){1}?)(cd|d){1}?', 'abcd').groups() ('ab', 'cd') # re.match('((?:ab|abc){1,1}?)(cd|d){1,1}?', 'abcd').groups() ('ab', 'cd') Maybe is it possible to outsource RE engine in to a separate module, like python RE (to let user a choice) - something like this: reguse standard|extended|... - login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [059ef494fd] (rid 89097) by dkf on 2013-02-11 22:53:00:
- icomment:
I'd be OK with throwing an error (in the case of something like "<empty>*" as that's always troublesome) if we could detect it. Well, maybe the engine could do slightly better. I really don't know. This ought to be an error case or a success case, but definitely not a busyloop case.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [ac83b053d2] (rid 101308) by sebres on 2013-02-11 23:06:39:
- icomment:
python is not consistent here also (does not always throw an error in this case) - however it has not a busy loop after 5-th group: # re.match('((a)*(b)*)*', 'abab').groups() ('', 'a', 'b') # re.match('(((a)*(b)*)*)*', 'abab').groups() ('', '', 'a', 'b') # re.match('(((((((a)*(b)*)*)*)*)*)*)*', 'ab').groups() ('', '', '', '', '', '', 'a', 'b') I try to fix this problem for tcl8.5 tomorrow... - login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [fb7b804171] (rid 92071) by sebres on 2013-02-11 23:07:16:
- assignee changed to: "sebres"
Ticket change [3a10399d9d] (rid 112498) by dgp on 2013-02-12 00:39:14:
- icomment:
OK, there are graphs for which the fixempties algorithm just doesn't work. This is one of them. continuing to explore. Will report facts when I'm confident in them.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [bd23da1fa6] (rid 101862) by dgp on 2013-02-12 00:39:15:
- assignee changed to: "nobody"
- foundin changed to: "None"
- priority changed to: "5 Medium"
- subsystem changed to: "None"
Ticket change [e515e3abc1] (rid 88073) by dgp on 2013-02-12 01:06:46:
- icomment:
The trouble is when unempty is called to remove an EMPTY arc, and then copyouts() copies the same EMPTY arc back into place. The simplest graph I can imagine that would have this problem is one with two states A B and arc lists: A: ->A ->B B: ->A ->B where all those arcs are empty. Add as many other states and arcs as you like. One pass through fixempties() will leave that set of EMPTY arcs unchanged, so we loop forever still trying to change them (reduce them to 0). Now it may be that such a graph ought not be possible in this context, and the real bug is somewhere else that permits it to show up. But it wouldn't hurt to not assume that here.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [a25cd09d87] (rid 109875) by dgp on 2013-02-12 01:13:39:
- icomment:
The simplest idea I have for a fix is to modify copyouts() so that it doesn't copy EMPTY arcs. I have very little confidence this is the right thing to do, but I'll see what the test suite has to say.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [4538810eda] (rid 99315) by dgp on 2013-02-12 01:22:38:
- icomment:
OK, that failed. A more limited idea is to only refuse to copy EMPTY arcs that would become vacuous arcs in their new state.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [61bdb99128] (rid 92295) by dgp on 2013-02-12 01:25:22:
- icomment:
test suite likes that fine, and : % regexp -about (((((a)*)*)*)*)* 5 REG_UEMPTYMATCH After I tidy up, I'll put the patch on a branch,
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [0ee121ffee] (rid 94697) by dgp on 2013-02-12 01:52:09:
- icomment:
see bug-3604074 branch for patch.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [07a3bbf2f6] (rid 89437) by dgp on 2013-02-12 01:52:18:
- assignee changed to: "dkf"
- foundin changed to: "obsolete: 8.4.19"
- priority changed to: "9 Immediate"
- subsystem changed to: "43. Regexp"
Ticket change [cec4d6a24f] (rid 96106) by dgp on 2013-02-12 03:32:02:
- icomment:
FWIW, the pre-Spencer RE engine (pre Tcl 8.1) could not handle such expressions.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [2035c8481e] (rid 91277) by dkf on 2013-02-12 04:14:42:
- assignee changed to: "dgp"
- icomment:
You're far more knowledgeable about such things than I am, but I think it looks like a reasonable change to make; I can just about follow the reasoning even if I can't determine if that is the perfect fix. The test certainly looks good; that we pass it is a good sign (though producing an error would be nearly as good in my eyes to be honest!) Feel free to commit.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
Ticket change [1f244b3f21] (rid 91094) by dgp on 2013-02-12 04:26:46:
- assignee changed to: "dkf"
- icomment:
% regexp -about [string repeat ( 7]a[string repeat )* 7] 7 REG_UEMPTYMATCH % regexp -about [string repeat ( 8]a[string repeat )* 8] <looks like there's still a problem...>
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
Ticket change [ef3ae8e0f4] (rid 108957) by dkf on 2013-02-12 16:06:42:
- assignee changed to: "dgp"
- icomment:
Sure, but I don't know how to fix it.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
Ticket change [7ba4171bd4] (rid 96941) by dgp on 2013-02-12 20:33:35:
- assignee changed to: "dkf"
- icomment:
It's the same fundamental problem. fixempties() does not work.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
Ticket change [669a5d8a89] (rid 102742) by dkf on 2013-02-12 21:03:04:
- assignee changed to: "dgp"
- icomment:
But I'm not going to suddenly become able to fix it. Let me be clear, I could not have come up with the patch already proposed (though I can see that it looks reasonable) as I lack the specific experience/expertise.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
Ticket change [d0610baa29] (rid 113170) by dgp on 2013-02-12 21:05:27:
- icomment:
I'm not asking you to do anything. I did seek a review, and you've done that. I'm just recording my progress in case it sparks anything in anyone watching.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [056cd61b66] (rid 89062) by dkf on 2013-02-12 21:07:12:
- icomment:
In that case it's dratted SF's stupid form that keeps assigning things back to me. Sorry for overinterpreting that.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [3b62428520] (rid 112735) by dgp on 2013-02-12 21:08:46:
- icomment:
Any idea whether the postgresql folks have found/fixed the same issue?
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [481aee36c5] (rid 99828) by dgp on 2013-02-12 21:29:05:
- icomment:
looking at 9.2.3 sources, apparently not. :P
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [ad6959c55f] (rid 101466) by sebres on 2013-02-13 00:24:59:
- icomment:
posgres tested: postgres=# select 1 where 'a' ~ '((((a)*)*)*)*'; ?column? ---------- 1 (1 row) postgres=# select 1 where 'a' ~ '(((((a)*)*)*)*)*'; !busyloop! - login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [2dea5c69ea] (rid 119135) by sebres on 2013-02-13 01:48:22:
- icomment:
I heard just now, postgres 9.2.3-1.pgdg60+1 (debian squeeze, 32 bit), it could not be reproduced. Very strange... in change log can't find nothing about regexp.
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [6d28cd2eec] (rid 128640) by dgp on 2013-02-13 03:00:41:
- icomment:
if you can locate the patch, that would be interesting to look at. Meanwhile, the failing scenario seems to be when the subgraph made of of just the EMPTY arcs contains a cycle. When that is true, fixempties() can fail. It doesn't have to, it can get lucky with ordering and succeed, but success is not guaranteed when EMPTY cycles can show up. Forbidding creation of single state cycles is helpful, but not the complete answer.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [cf8befedcc] (rid 113001) by sebres on 2013-02-13 16:48:46:
- icomment:
9.1 can be reproduced 9.2.2 not Candidates are: 1. 9-1-3 Fix regular expression back-references with * attached (Tom Lane) Rather than enforcing an exact string match, the code would effectively accept any string that satisfies the pattern sub-expression referenced by the back-reference symbol. A similar problem still afflicts back-references that are embedded in a larger quantified expression, rather than being the immediate subject of the quantifier. This will be addressed in a future PostgreSQL release. 2. 9-1-5 Fix extraction of common prefixes from regular expressions (Tom Lane) The code could get confused by quantified parenthesized subexpressions, such as ^(foo)?bar. This would lead to incorrect index optimization of searches for such patterns. 3. 9-2 Sync regular expression code with TCL 8.5.11 and improve internal processing (Tom Lane)
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [edb8981902] (rid 103943) by sebres on 2013-02-13 19:13:46:
- icomment:
> dgp, please have a look here: http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a9287de1760450e7fe3b4309ee1ba7ea2af39217
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [779ccd1bc1] (rid 89805) by dgp on 2013-02-14 01:44:50:
- icomment:
The postgresql patch does not directly attack the flaws of fixempties(). I can only guess that the changes it makes somehow prevent the troublesome cases from reaching it. At least the ones we know about and test.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [16e8525244] (rid 124793) by dgp on 2013-02-14 01:51:13:
- icomment:
The 8-deep expression that still hangs in bug-3604074 branch reaches a point where there are 8 states which have outgoing EMPTY arcs to states that also have outgoing EMPTY arcs. Looking at just those states and just EMPTY arcs, the graph is: A: G H E C F D B: E C F G H A C: E B D G H F D: C F H B G A E: C F D G A H F: B E G H A C G: A H D F C B H: A D F C E G After one pass through the fixempties() loop, the new graph is: A: F C E H G B B: H G F C E D C: H G D B E A D: G B H F C E E: A G D F C B F: A H G E B D G: C F D H A E H: E C F D A B Then the next pass through the loop will return the graph to the first state. To fundamentally solve this problem within fixempties() requires changing the algorithm to one which will not fall into this kind of a limit cycle.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [74e3e5ae2c] (rid 105296) by dkf on 2013-02-14 02:41:16:
- icomment:
The other approach might be to set a cap on the number of times round the loop (maybe 100? 1000? I don't know how much) and to error out when the limit is reached. Like that, we'd turn an infinite loop into a definite failure. Just throwing the idea out there in case that's felt to be a better approach.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [135eca6031] (rid 124206) by dgp on 2013-02-14 03:49:22:
- icomment:
Yes, if I don't find the right sort of graph reduction algorithm advice with reasonable effort, that's the way to go to at least make sure we avoid hanging Tcl.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [9714ffd88c] (rid 106446) by dgp on 2013-02-14 04:20:12:
- icomment:
Possibly relevant link: http://swtch.com/~rsc/regexp/regexp2.html Excerpt: "This particular backtracking implementation has one shortcoming that production implementations usually do not: regular expressions like (a*)* can cause infinite loops in the compiled program..."
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [f9c83cf372] (rid 91755) by dgp on 2013-02-15 04:06:05:
- assignee changed to: "dkf"
- icomment:
New branch bug-3604074 offers a better fix. Covers all (..(a)*...)* cases up until the number of states prompts refusal to compile the expression.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
Ticket change [eb2c7e642a] (rid 103491) by dgp on 2013-02-15 22:33:29:
- assignee changed to: "dgp"
- closedate changed to: "2013-02-15T15:33:29"
- closer changed to: "dgp"
- icomment:
allow_comments - 1 Fix merged to all release branches
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- username: "dgp"