Closed Bug 131226 Opened 18 years ago Closed 18 years ago

Recursive use of regular expression causes hang

Categories

(Core :: JavaScript Engine, defect, critical)

x86
Windows ME
defect
Not set
critical

Tracking

()

VERIFIED DUPLICATE of bug 13350

People

(Reporter: jwatt, Assigned: rogerl)

Details

(Keywords: hang)

Attachments

(1 file)

using 0.9.9

attachment to follow
Keywords: hang
should have said, the code causing the hang is: 

    var str = "x";
    var re = /(.)/;
    while (re.exec(str));
Have to mark this one invalid. The testcase hangs IE6, too. 
The reason is, there is no global flag set on the regular expression.
If you modify your testcase like this, everything works:

                    var str = "x";
                    var re = /(.)/g;
                    while (re.exec(str));


Here is a demonstration in the standalone JS shell:


js> var str = "x";
js> var re = /(.)/g;
js> re.lastIndex;
0


js> var x1 = re.exec(str);
js> x1;
x,x
js> Boolean(x1);
true
js> re.lastIndex;
1 <<<<<<<<<<<<<<<<<<<<<<<<< notice this is now at the end of the string "x"


js> var x2 = re.exec(str);
js> x2;
null  <<<<<<<<<<<<<<<<<<<<< because the match was attempted from lastIndex=1
js> Boolean(x2);
false <<<<<<<<<<<<<<<<<<<<< this will terminate your while-loop
js> re.lastIndex;
0


Notice how re.lastIndex is incremented when a match is found. 
The next attempt at a match will begin at re.lastIndex. Once this reaches
the end of the string, the match will fail, so your while-loop terminates,
and re.lastIndex is set back to 0.


But this only happens if the global flag is set. If it is not set,
re.lastIndex remains at 0 after each re.exec(str). If this happens
to result in a match, this match will happen over and over and over.
It's like coding:

                        while(true);
Status: NEW → RESOLVED
Closed: 18 years ago
Resolution: --- → INVALID
Actually, let me reopen this to mark it as a duplicate of DOM bug 13350,
"DOM needs to police JS infinite loops, schedule garbage collection"

The browser shouldn't hang, even if a coder does

                        while(true);
Status: RESOLVED → REOPENED
Resolution: INVALID → ---

*** This bug has been marked as a duplicate of 13350 ***
Status: REOPENED → RESOLVED
Closed: 18 years ago18 years ago
Resolution: --- → DUPLICATE
Marking Verified. 

Jonathan, thank you for this report. You have been cc'ed on bug 13350,
so you may follow its progress -
Status: RESOLVED → VERIFIED
Sure, I know why it causes the hang, but thanks for explaining it anyway. :)
Sorry for the dup, I do try to search - honest. :)
You need to log in before you can comment on or make changes to this bug.