From bbda56c15afe26278ebfd1969500215f4dba686f Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Wed, 20 Apr 2022 14:14:33 -0500 Subject: [PATCH 1/6] Fix ClassCastException #1206 --- src/org/mozilla/javascript/ConsString.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/mozilla/javascript/ConsString.java b/src/org/mozilla/javascript/ConsString.java index d0abe2d1c8..d90e47be77 100644 --- a/src/org/mozilla/javascript/ConsString.java +++ b/src/org/mozilla/javascript/ConsString.java @@ -69,7 +69,7 @@ private synchronized String flatten() { } } - final String str = (String) next; + final String str = next.toString(); charPos -= str.length(); str.getChars(0, str.length(), chars, charPos); next = stack.isEmpty() ? null : stack.removeFirst(); From 3340d98167318bba0c2421688b3e5066e33797f3 Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Mon, 25 Apr 2022 12:46:05 -0500 Subject: [PATCH 2/6] Fix ClassCastException when using StringBuilder/Buffer #496 --- .../javascript/tests/Issue1206Test.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 testsrc/org/mozilla/javascript/tests/Issue1206Test.java diff --git a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java new file mode 100644 index 0000000000..a64cd9e0d9 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java @@ -0,0 +1,36 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.tests; + +import org.junit.Test; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; + +/** Tests the ConsString class to ensure it properly supports String, StringBuffer and StringBuilder. */ +public class Issue1206Test { + @Test + public void testConsStringUsingString() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, "hello"); + cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + } + + @Test + public void testConsStringUsingStringBuffer() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, new StringBuffer("hello")); + cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + } + + @Test + public void testConsStringUsingStringBuilder() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, new StringBuilder("hello")); + cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + } +} From 8340c2629a59a77282243901b7360131d9ffaa91 Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Mon, 25 Apr 2022 14:45:12 -0500 Subject: [PATCH 3/6] Fix ClassCastException when using StringBuilder/Buffer mozilla#496 --- testsrc/org/mozilla/javascript/tests/Issue1206Test.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java index a64cd9e0d9..0e961a3418 100644 --- a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java +++ b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java @@ -8,7 +8,9 @@ import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; -/** Tests the ConsString class to ensure it properly supports String, StringBuffer and StringBuilder. */ +/** + * Tests the ConsString class to ensure it properly supports String, StringBuffer and StringBuilder. + */ public class Issue1206Test { @Test public void testConsStringUsingString() { From 88de68fc0c556d103e75dabe522f693896fae94a Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Thu, 19 May 2022 10:06:47 -0500 Subject: [PATCH 4/6] Fix ClassCastException when using StringBuilder/Buffer #496 --- src/org/mozilla/javascript/ConsString.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/org/mozilla/javascript/ConsString.java b/src/org/mozilla/javascript/ConsString.java index d90e47be77..4513fbe899 100644 --- a/src/org/mozilla/javascript/ConsString.java +++ b/src/org/mozilla/javascript/ConsString.java @@ -32,6 +32,12 @@ public class ConsString implements CharSequence, Serializable { private boolean isFlat; public ConsString(CharSequence str1, CharSequence str2) { + if (!(str1 instanceof String) && !(str1 instanceof ConsString)) { + str1 = str1.toString(); + } + if (!(str2 instanceof String) && !(str2 instanceof ConsString)) { + str2 = str2.toString(); + } left = str1; right = str2; length = left.length() + right.length(); @@ -69,7 +75,7 @@ private synchronized String flatten() { } } - final String str = next.toString(); + final String str = (String) next; charPos -= str.length(); str.getChars(0, str.length(), chars, charPos); next = stack.isEmpty() ? null : stack.removeFirst(); From 503607efe854418dfb21088feb5a23732b997f59 Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Fri, 20 May 2022 11:10:56 -0500 Subject: [PATCH 5/6] Fix ClassCastException when using StringBuilder/Buffer mozilla#496 --- .../org/mozilla/javascript/tests/Issue1206Test.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java index 0e961a3418..52f35f2493 100644 --- a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java +++ b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java @@ -4,6 +4,8 @@ package org.mozilla.javascript.tests; +import junit.framework.TestCase; + import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; @@ -11,13 +13,14 @@ /** * Tests the ConsString class to ensure it properly supports String, StringBuffer and StringBuilder. */ -public class Issue1206Test { +public class Issue1206Test extends TestCase { @Test public void testConsStringUsingString() { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(null); scope.put("var1", scope, "hello"); - cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); } @Test @@ -25,7 +28,8 @@ public void testConsStringUsingStringBuffer() { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(null); scope.put("var1", scope, new StringBuffer("hello")); - cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); } @Test @@ -33,6 +37,7 @@ public void testConsStringUsingStringBuilder() { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(null); scope.put("var1", scope, new StringBuilder("hello")); - cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); } } From 651b1559143e6d0b529c9264185ab4b30282c460 Mon Sep 17 00:00:00 2001 From: Edward Jensen Date: Mon, 23 May 2022 10:45:48 -0500 Subject: [PATCH 6/6] Fix ClassCastException when using StringBuilder/Buffer mozilla#496 --- testsrc/org/mozilla/javascript/tests/Issue1206Test.java | 1 - 1 file changed, 1 deletion(-) diff --git a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java index 52f35f2493..a4b0d548d2 100644 --- a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java +++ b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java @@ -5,7 +5,6 @@ package org.mozilla.javascript.tests; import junit.framework.TestCase; - import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable;