Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ClassCastException when using StringBuilder/Buffer #496 #1210

Merged
merged 7 commits into from
May 27, 2022
6 changes: 6 additions & 0 deletions src/org/mozilla/javascript/ConsString.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Issue1206Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the tests currently just check whether the code doesn't throw an exception

Think it would be better if besides that, they'd also assert the result of cx.evaluateString to be as expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Those tests have been updated to assert the results of those cx.evaluateString calls.

The fork/branch has been updated. I think this might be ready for review by the Rhino team! Thanks!

@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);
}
}