Closed
Bug 131226
Opened 23 years ago
Closed 23 years ago
Recursive use of regular expression causes hang
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: jwatt, Assigned: rogerl)
Details
(Keywords: hang)
Attachments
(1 file)
357 bytes,
text/html
|
Details |
using 0.9.9
attachment to follow
Reporter | ||
Comment 1•23 years ago
|
||
Reporter | ||
Comment 2•23 years ago
|
||
should have said, the code causing the hang is:
var str = "x";
var re = /(.)/;
while (re.exec(str));
Comment 3•23 years ago
|
||
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: 23 years ago
Resolution: --- → INVALID
Comment 4•23 years ago
|
||
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 → ---
Comment 5•23 years ago
|
||
*** This bug has been marked as a duplicate of 13350 ***
Status: REOPENED → RESOLVED
Closed: 23 years ago → 23 years ago
Resolution: --- → DUPLICATE
Comment 6•23 years ago
|
||
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
Reporter | ||
Comment 7•23 years ago
|
||
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.
Description
•