From e5009c3dd2da712c03ee68173a8291d57c851057 Mon Sep 17 00:00:00 2001 From: shelches <42914500+shelches@users.noreply.github.com> Date: Fri, 27 May 2022 17:59:43 -0500 Subject: [PATCH] Fix ClassCastException when using StringBuilder/Buffer #496 (#1210) Convert non-String, non-ConsString objects to a string when creating a ConsString so that StringBuilders and other CharSequence objects can be used natively more easily. Fixes https://github.com/mozilla/rhino/discussions/1206 and https://github.com/mozilla/rhino/issues/496 Co-authored-by: Edward Jensen --- src/org/mozilla/javascript/ConsString.java | 6 +++ .../javascript/tests/Issue1206Test.java | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 testsrc/org/mozilla/javascript/tests/Issue1206Test.java diff --git a/src/org/mozilla/javascript/ConsString.java b/src/org/mozilla/javascript/ConsString.java index 58c06eadbb..cd0d040293 100644 --- a/src/org/mozilla/javascript/ConsString.java +++ b/src/org/mozilla/javascript/ConsString.java @@ -34,6 +34,12 @@ public class ConsString implements CharSequence, Serializable, Wrapper { 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(); diff --git a/testsrc/org/mozilla/javascript/tests/Issue1206Test.java b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java new file mode 100644 index 0000000000..a4b0d548d2 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/Issue1206Test.java @@ -0,0 +1,42 @@ +/* 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 junit.framework.TestCase; +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 extends TestCase { + @Test + public void testConsStringUsingString() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, "hello"); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); + } + + @Test + public void testConsStringUsingStringBuffer() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, new StringBuffer("hello")); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); + } + + @Test + public void testConsStringUsingStringBuilder() { + Context cx = Context.enter(); + Scriptable scope = cx.initStandardObjects(null); + scope.put("var1", scope, new StringBuilder("hello")); + Object result = cx.evaluateString(scope, "var1 = var1 + ' world'", "test", 1, null); + assertEquals("hello world", result); + } +}