Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2013-02-15
22:33 • Closed ticket [3604074fff]: Inifinite loop when matching regular expression ... artifact: eb2c7e642a user: dgp
04:06 • Ticket [3604074fff]: 5 changes artifact: f9c83cf372 user: dgp
2013-02-14
04:20 • Ticket [3604074fff]: 4 changes artifact: 9714ffd88c user: dgp
03:49 • Ticket [3604074fff]: 4 changes artifact: 135eca6031 user: dgp
02:41 • Ticket [3604074fff]: 4 changes artifact: 74e3e5ae2c user: dkf
01:51 • Ticket [3604074fff]: 4 changes artifact: 16e8525244 user: dgp
01:44 • Ticket [3604074fff]: 4 changes artifact: 779ccd1bc1 user: dgp
2013-02-13
19:13 • Ticket [3604074fff]: 4 changes artifact: edb8981902 user: sebres
16:48 • Ticket [3604074fff]: 4 changes artifact: cf8befedcc user: sebres
03:00 • Ticket [3604074fff]: 4 changes artifact: 6d28cd2eec user: dgp
01:48 • Ticket [3604074fff]: 4 changes artifact: 2dea5c69ea user: sebres
00:24 • Ticket [3604074fff]: 4 changes artifact: ad6959c55f user: sebres
2013-02-12
21:29 • Ticket [3604074fff]: 4 changes artifact: 481aee36c5 user: dgp
21:08 • Ticket [3604074fff]: 4 changes artifact: 3b62428520 user: dgp
21:07 • Ticket [3604074fff]: 4 changes artifact: 056cd61b66 user: dkf
21:05 • Ticket [3604074fff]: 4 changes artifact: d0610baa29 user: dgp
21:03 • Ticket [3604074fff]: 5 changes artifact: 669a5d8a89 user: dkf
20:33 • Ticket [3604074fff]: 5 changes artifact: 7ba4171bd4 user: dgp
16:06 • Ticket [3604074fff]: 5 changes artifact: ef3ae8e0f4 user: dkf
04:26 • Ticket [3604074fff]: 5 changes artifact: 1f244b3f21 user: dgp
04:14 • Ticket [3604074fff]: 5 changes artifact: 2035c8481e user: dkf
03:32 • Ticket [3604074fff]: 4 changes artifact: cec4d6a24f user: dgp
01:52 • Ticket [3604074fff]: 4 changes artifact: 07a3bbf2f6 user: dgp
01:52 • Ticket [3604074fff]: 4 changes artifact: 0ee121ffee user: dgp
01:25 • Ticket [3604074fff]: 4 changes artifact: 61bdb99128 user: dgp
01:22 • Ticket [3604074fff]: 4 changes artifact: 4538810eda user: dgp
01:13 • Ticket [3604074fff]: 4 changes artifact: a25cd09d87 user: dgp
01:06 • Ticket [3604074fff]: 4 changes artifact: e515e3abc1 user: dgp
00:39 • Ticket [3604074fff]: 4 changes artifact: bd23da1fa6 user: dgp
00:39 • Ticket [3604074fff]: 4 changes artifact: 3a10399d9d user: dgp
2013-02-11
23:07 • Ticket [3604074fff]: 1 change artifact: fb7b804171 user: sebres
23:06 • Ticket [3604074fff]: 4 changes artifact: ac83b053d2 user: sebres
22:53 • Ticket [3604074fff]: 4 changes artifact: 059ef494fd user: dkf
22:40 • Ticket [3604074fff]: 4 changes artifact: 604d5fefe3 user: sebres
22:08 • Ticket [3604074fff]: 4 changes artifact: e1cd6e0766 user: dkf
22:03 • Ticket [3604074fff]: 3 changes artifact: 38312e496e user: dkf
21:51 • Ticket [3604074fff]: 5 changes artifact: 3fde66a9a7 user: dgp
20:54 • Ticket [3604074fff]: 4 changes artifact: 7ee546c761 user: dkf
20:47 • Ticket [3604074fff]: 4 changes artifact: 41cfae7864 user: dkf
20:34 • Ticket [3604074fff]: 4 changes artifact: 8730c7c74a user: bjorngrathwohl
20:29 • Ticket [3604074fff]: 1 change artifact: c5941d7a29 user: dkf
20:27 • Ticket [3604074fff]: 4 changes artifact: b734cbf98e user: dgp
19:55 • Ticket [3604074fff]: 4 changes artifact: 92d5e63812 user: bjorngrathwohl
19:02 • Ticket [3604074fff]: 4 changes artifact: 6256bb4b1c user: sebres
11:04 • New ticket [3604074fff]. artifact: 1d1bfdbf18 user: bjorngrathwohl

Inifinite loop when matching regular expression

Closed / Fixed3604074fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 14 years ago
Descriptionplain2013-02-11 11:04:01
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>
sebresplain2013-02-11 19:02:06
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 {} {} {} {}
bjorngrathwohlplain2013-02-11 19:55:48
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)*)*)*)*.
dgpplain2013-02-11 20:27:15
What release of Tcl?
dkf Ticket [3604074fff]: 1 change [History diff]2013-02-11 20:29:24
bjorngrathwohlplain2013-02-11 20:34:54
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.
dkfplain2013-02-11 20:47:43
I've confirmed it in 8.4.7, 8.5.2 and 8.6 (trunk).
dkfplain2013-02-11 20:54:24
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
dgpplain2013-02-11 21:51:45
Loop is in fixempties().
dkf Ticket [3604074fff]: 3 changes [History diff]2013-02-11 22:03:12
dkfplain2013-02-11 22:08:51
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?
sebresplain2013-02-11 22:40:33
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|...
dkfplain2013-02-11 22:53:00
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.
sebresplain2013-02-11 23:06:39
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...
sebres Ticket [3604074fff]: 1 change [History diff]2013-02-11 23:07:16
dgpplain2013-02-12 00:39:14
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.
dgp Ticket [3604074fff]: 4 changes [History diff]2013-02-12 00:39:15
dgpplain2013-02-12 01:06:46
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.
dgpplain2013-02-12 01:13:39
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.
dgpplain2013-02-12 01:22:38
OK, that failed.  A more limited idea
is to only refuse to copy EMPTY arcs
that would become vacuous arcs in
their new state.
dgpplain2013-02-12 01:25:22
test suite likes that fine, and :

% regexp -about (((((a)*)*)*)*)*
5 REG_UEMPTYMATCH

After I tidy up, I'll put the patch on a branch,
dgpplain2013-02-12 01:52:09
see bug-3604074 branch for patch.
dgp Ticket [3604074fff]: 4 changes [History diff]2013-02-12 01:52:18
dgpplain2013-02-12 03:32:02
FWIW, the pre-Spencer RE engine (pre Tcl 8.1) could not
handle such expressions.
dkfplain2013-02-12 04:14:42
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.
dgpplain2013-02-12 04:26:46
% 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...>
dkfplain2013-02-12 16:06:42
Sure, but I don't know how to fix it.
dgpplain2013-02-12 20:33:35
It's the same fundamental problem.  fixempties() does not work.
dkfplain2013-02-12 21:03:04
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.
dgpplain2013-02-12 21:05:27
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.
dkfplain2013-02-12 21:07:12
In that case it's dratted SF's stupid form that keeps assigning things back to me. Sorry for overinterpreting that.
dgpplain2013-02-12 21:08:46
Any idea whether the postgresql folks have found/fixed
the same issue?
dgpplain2013-02-12 21:29:05
looking at 9.2.3 sources, apparently not. :P
sebresplain2013-02-13 00:24:59
posgres tested:
postgres=# select 1 where 'a' ~ '((((a)*)*)*)*';
 ?column?
----------
        1
(1 row)
postgres=# select 1 where 'a' ~ '(((((a)*)*)*)*)*';
!busyloop!
sebresplain2013-02-13 01:48:22
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.
dgpplain2013-02-13 03:00:41
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.
sebresplain2013-02-13 16:48:46
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)
sebresplain2013-02-13 19:13:46
> dgp, please have a look here: 
http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a9287de1760450e7fe3b4309ee1ba7ea2af39217
dgpplain2013-02-14 01:44:50
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.
dgpplain2013-02-14 01:51:13
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.
dkfplain2013-02-14 02:41:16
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.
dgpplain2013-02-14 03:49:22
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.
dgpplain2013-02-14 04:20:12
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..."
dgpplain2013-02-15 04:06:05
New branch bug-3604074 offers a better fix.  Covers all
(..(a)*...)* cases up until the number of states prompts
refusal to compile the expression.
dgpplain2013-02-15 22:33:29
allow_comments - 1

Fix merged to all release branches
Log in (anonymous is fine) to add a comment.