Skip to content

Commit 0266fa7

Browse files
author
zhourenjian
committed
Fix bug of JavaScript compressing may result in Stack Overflow
1 parent 849296c commit 0266fa7

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/net/sf/j2s/core/compiler/RegExCompress.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,22 @@ public static String regexCompress(String str) {
4848
String cssCodes = null;
4949
int idx1 = str.indexOf("$WTC$$.registerCSS");
5050
int idx2 = -1;
51-
String specialFunKey = "@324@();\r\n";
52-
if (idx1 != -1) {
53-
idx2 = str.indexOf("\");\r\n", idx1);
54-
if (idx2 != -1) {
51+
int idx = idx1;
52+
while (idx != -1) {
53+
int index = str.indexOf("\");\r\n", idx);
54+
if (index != -1) {
55+
idx2 = index + 5;
5556
ignoreCSS = true;
56-
cssCodes = str.substring(idx1, idx2 + 5);
57-
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
57+
idx = str.indexOf("$WTC$$.registerCSS", idx2);
58+
} else {
59+
break;
5860
}
5961
}
62+
String specialFunKey = "@324@();\r\n";
63+
if (ignoreCSS) {
64+
cssCodes = str.substring(idx1, idx2);
65+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2);
66+
}
6067
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
6168
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 1:3
6269
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4
@@ -70,7 +77,7 @@ public static String regexCompress(String str) {
7077
"(\\s+)",
7178
"$1$2$8$9");
7279
if (ignoreCSS) {
73-
int idx = str.indexOf(specialFunKey);
80+
idx = str.indexOf(specialFunKey);
7481
if (idx != -1) {
7582
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
7683
} else {
@@ -85,14 +92,21 @@ public static String regexCompress2(String str) {
8592
String cssCodes = null;
8693
int idx1 = str.indexOf("$WTC$$.registerCSS");
8794
int idx2 = -1;
88-
String specialFunKey = "@324@();\r\n";
89-
if (idx1 != -1) {
90-
idx2 = str.indexOf("\");\r\n", idx1);
91-
if (idx2 != -1) {
95+
int idx = idx1;
96+
while (idx != -1) {
97+
int index = str.indexOf("\");\r\n", idx);
98+
if (index != -1) {
99+
idx2 = index + 5;
92100
ignoreCSS = true;
93-
cssCodes = str.substring(idx1, idx2 + 5);
94-
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
101+
idx = str.indexOf("$WTC$$.registerCSS", idx2);
102+
} else {
103+
break;
104+
}
95105
}
106+
String specialFunKey = "@324@();\r\n";
107+
if (ignoreCSS) {
108+
cssCodes = str.substring(idx1, idx2);
109+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2);
96110
}
97111
String whiteSpace = "[ \\f\\t\\v]";
98112
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
@@ -108,7 +122,7 @@ public static String regexCompress2(String str) {
108122
"(" + whiteSpace + "+)",
109123
"$1$2$8$9");
110124
if (ignoreCSS) {
111-
int idx = str.indexOf(specialFunKey);
125+
idx = str.indexOf(specialFunKey);
112126
if (idx != -1) {
113127
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
114128
} else {

0 commit comments

Comments
 (0)