Skip to content

Commit 102b5e7

Browse files
committed
tests: Port recursion tests from avmplus
These tests have been adapted from the avmplus repo. See https://github.com/adobe/avmplus/tree/858d034a3bd3a54d9b70909386435cf4aec81d21/test
1 parent b6867bd commit 102b5e7

File tree

30 files changed

+620
-0
lines changed

30 files changed

+620
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* -*- mode: java; tab-width: 4 -*- */
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
package {
6+
import flash.display.MovieClip; public class Test extends MovieClip {}
7+
}
8+
9+
import com.adobe.test.Assert;
10+
//-----------------------------------------------------------------------------
11+
12+
// var SECTION = "pcre_could_be_empty_branch";
13+
// var VERSION = "";
14+
// var TITLE = "Shouldn't crash on regexps with many nested parentheses embedded in ()* constructs";
15+
16+
var testcases = getTestCases();
17+
18+
function getTestCases()
19+
{
20+
var array = new Array();
21+
var item = 0;
22+
23+
// setting this to < 407 results in evaluating the 'search' to 0, from 407, it is -1
24+
var NUM_PAREN = 406;
25+
26+
var pattern = '';
27+
for (var i = 0; i < NUM_PAREN; i++) pattern += '(';
28+
for (var i = 0; i < NUM_PAREN; i++) pattern += 'a)';
29+
pattern += '*';
30+
31+
try {
32+
var re = new RegExp(pattern);
33+
34+
var res = "aaaaa".search(re);
35+
array[item++] = Assert.expectEq( "'aaaaa'.search(re)", 0, res);
36+
}
37+
catch (e: Error) {
38+
if (e.message.match("#1023"))
39+
array[item++] = Assert.expectEq( "'aaaaa'.search(re)", 0, 0);
40+
else
41+
throw(e);
42+
}
43+
return array;
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<flex-config>
2+
<compiler>
3+
<source-path>
4+
<path-element>.</path-element>
5+
<path-element>../../lib</path-element>
6+
</source-path>
7+
<debug>false</debug>
8+
<omit-trace-statements>false</omit-trace-statements>
9+
<show-actionscript-warnings>false</show-actionscript-warnings>
10+
<strict>false</strict>
11+
</compiler>
12+
<output>test.swf</output>
13+
</flex-config>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'aaaaa'.search(re) PASSED!
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
num_ticks = 1
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* -*- mode: java; tab-width: 4 -*- */
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
package {
6+
import flash.display.MovieClip; public class Test extends MovieClip {}
7+
}
8+
9+
import com.adobe.test.Assert;
10+
//-----------------------------------------------------------------------------
11+
12+
// var SECTION = "pcre_is_startline";
13+
// var VERSION = "";
14+
// var TITLE = "Shouldn't crash on regexps with many nested parentheses";
15+
16+
var testcases = getTestCases();
17+
18+
function getTestCases()
19+
{
20+
var array = new Array();
21+
var item = 0;
22+
23+
var NO_BACKREFS = false;
24+
var DO_BACKREFS = true;
25+
26+
//--------------------------------------------------
27+
28+
// causes recursion
29+
testThis(500, NO_BACKREFS, 'a', 'b');
30+
// no recursion in this case
31+
testThis(500, DO_BACKREFS, 'a', 'b');
32+
33+
//--------------------------------------------------
34+
35+
/*
36+
* Creates a regexp pattern like ((..(((?=a)a|(?=a)b)))..)) + using non-capturing brackets instead
37+
* of capturing ones.
38+
* and tests str.search()
39+
*
40+
* It is essential for this test pattern to contain a a|b alternating construct.
41+
* Using such construct triggers the recursive execution of pcre_compile.cpp::is_startline.
42+
* If | is not used, there is no recursion at all, which is interesting because this means,
43+
* pcre is able to find out somehow, that the regex contains a | somewhere deep.
44+
* A regexp like /(((((a)))))(a|b)/ cause only a 1-2 level deep recursion of is_startline.
45+
*
46+
* It may worth playing with conditional and assertion brackets too.
47+
* */
48+
function testThis(numParens, doBackRefs, strAlt1, strAlt2)
49+
{
50+
var openParen = doBackRefs? '(' : '(?:';
51+
var closeParen = ')';
52+
var pattern = '';
53+
54+
for (var i=0; i<numParens; i++)
55+
pattern += openParen;
56+
pattern += "(?=" + strAlt1 + ")" + strAlt1 + "|(?=" + strAlt1 + ")" + strAlt2;
57+
for (i=0; i<numParens; i++)
58+
pattern += closeParen;
59+
60+
try {
61+
var re = new RegExp(pattern);
62+
63+
if (doBackRefs) {
64+
var res = strAlt1.search(re);
65+
array[item++] = Assert.expectEq( "strAlt1.search(re)", -1, res);
66+
} else {
67+
var res = strAlt1.search(re);
68+
array[item++] = Assert.expectEq( "strAlt1.search(re)", 0, res);
69+
}
70+
}
71+
catch (e: Error) {
72+
if (e.message.match("#1023"))
73+
array[item++] = Assert.expectEq( "strAlt1.search(re)", 0, 0);
74+
else
75+
throw(e);
76+
}
77+
}
78+
79+
return array;
80+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<flex-config>
2+
<compiler>
3+
<source-path>
4+
<path-element>.</path-element>
5+
<path-element>../../lib</path-element>
6+
</source-path>
7+
<debug>false</debug>
8+
<omit-trace-statements>false</omit-trace-statements>
9+
<show-actionscript-warnings>false</show-actionscript-warnings>
10+
<strict>false</strict>
11+
</compiler>
12+
<output>test.swf</output>
13+
</flex-config>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
strAlt1.search(re) PASSED!
2+
strAlt1.search(re) FAILED! expected: -1 got: 0
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
num_ticks = 1

0 commit comments

Comments
 (0)