Skip to content

Commit a8e944a

Browse files
author
zhourenjian
committed
Fix bug of JavaScript compressing may result in Stack Overflow
1 parent bdcd14e commit a8e944a

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/RegExCompress.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,22 @@ public static String regexCompress(String str) {
103103
String cssCodes = null;
104104
int idx1 = str.indexOf("$WTC$$.registerCSS");
105105
int idx2 = -1;
106-
String specialFunKey = "@324@();\r\n";
107-
if (idx1 != -1) {
108-
idx2 = str.indexOf("\");\r\n", idx1);
109-
if (idx2 != -1) {
106+
int idx = idx1;
107+
while (idx != -1) {
108+
int index = str.indexOf("\");\r\n", idx);
109+
if (index != -1) {
110+
idx2 = index + 5;
110111
ignoreCSS = true;
111-
cssCodes = str.substring(idx1, idx2 + 5);
112-
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
112+
idx = str.indexOf("$WTC$$.registerCSS", idx2);
113+
} else {
114+
break;
113115
}
114116
}
117+
String specialFunKey = "@324@();\r\n";
118+
if (ignoreCSS) {
119+
cssCodes = str.substring(idx1, idx2);
120+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2);
121+
}
115122
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
116123
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 1:3
117124
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4
@@ -125,7 +132,7 @@ public static String regexCompress(String str) {
125132
"(\\s+)",
126133
"$1$2$8$9");
127134
if (ignoreCSS) {
128-
int idx = str.indexOf(specialFunKey);
135+
idx = str.indexOf(specialFunKey);
129136
if (idx != -1) {
130137
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
131138
} else {
@@ -140,15 +147,22 @@ public static String regexCompress2(String str) {
140147
String cssCodes = null;
141148
int idx1 = str.indexOf("$WTC$$.registerCSS");
142149
int idx2 = -1;
143-
String specialFunKey = "@324@();\r\n";
144-
if (idx1 != -1) {
145-
idx2 = str.indexOf("\");\r\n", idx1);
146-
if (idx2 != -1) {
150+
int idx = idx1;
151+
while (idx != -1) {
152+
int index = str.indexOf("\");\r\n", idx);
153+
if (index != -1) {
154+
idx2 = index + 5;
147155
ignoreCSS = true;
148-
cssCodes = str.substring(idx1, idx2 + 5);
149-
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
156+
idx = str.indexOf("$WTC$$.registerCSS", idx2);
157+
} else {
158+
break;
150159
}
151160
}
161+
String specialFunKey = "@324@();\r\n";
162+
if (ignoreCSS) {
163+
cssCodes = str.substring(idx1, idx2);
164+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2);
165+
}
152166
String whiteSpace = "[ \\f\\t\\v]";
153167
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
154168
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 2:2,3
@@ -163,7 +177,7 @@ public static String regexCompress2(String str) {
163177
"(" + whiteSpace + "+)",
164178
"$1$2$8$9");
165179
if (ignoreCSS) {
166-
int idx = str.indexOf(specialFunKey);
180+
idx = str.indexOf(specialFunKey);
167181
if (idx != -1) {
168182
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
169183
} else {

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/UTF8Concat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static void main(String[] args) {
6767
if (s.startsWith(j2sKeySig)) {
6868
s = s.substring(j2sKeySig.length());
6969
}
70+
System.out.println("Packing " + src.getName() + " ...");
7071
//*
7172
if (noCompressing) {
7273
buf.append(s);

0 commit comments

Comments
 (0)