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

ConsString.flattend should not cast to a String but call toString() instead #1368

Closed
jcompagner opened this issue Aug 9, 2023 · 4 comments
Closed

Comments

@jcompagner
Copy link

https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ConsString.java#L78

everywhere in ConsString left, right are CharSequence objects

so it can be that from java when you append code the java method returns a StringBuilder object and that is fine for ConsString for a long time until it wants to flatten then it bombs out with a classcast because we assume all charsequences (left/right) are or a ConsString or a String, but there are more implementations of CharSequnce.

so that code i think should just be changed to:

final String str = next.toString();

to make it a string, if it is a string there is no effect that just returns itself so that is just as fast.

@rbri
Copy link
Collaborator

rbri commented Aug 9, 2023

any chance to provide a test case (see org.mozilla.javascript.tests.ConsStringTest)

@jcompagner
Copy link
Author

    public void testAppendStringBuilder() {
        ConsString current = new ConsString("a", new StringBuilder("b"));
        current = new ConsString(current, "c");
        current = new ConsString(current, "d");

        assertEquals("abcd", current.toString());

        current = new ConsString("x", new ConsString("a", "b"));
        assertEquals("xab", current.toString());

        current = new ConsString(new ConsString("a", "b"), new ConsString("c", "d"));
        assertEquals("abcd", current.toString());
    }

@rbri
Copy link
Collaborator

rbri commented Aug 9, 2023

this was fixed with #1210 - do you use the latest code?

@jcompagner
Copy link
Author

jcompagner commented Aug 9, 2023

ah i was still using our source (and testing against that) and that was build from version 1.7.14 (and i noticed the cast so i didn't look really further because i still saw that same code.
But i guess making sure they are strings in the constructor is even better, because with a string builder you could potentially get weird results (you concat, then adjust the stringbuilder, before you flatten the ConsString)

Edit: and i see now that 1.7.14 is still the latest release so thats i guess why i didn't notice it..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants