StringIndexOutOfBoundException in NativeRegExp.appendReplacement

A. Sundararajan sundararajan.athijegannathan at oracle.com
Tue Jun 3 15:47:55 UTC 2014


Hi Chris,

Yes, it has been backported to 8 update.

jdk9 commit : http://hg.openjdk.java.net/jdk9/dev/nashorn/rev/e445404a69f5
jdk8u-dev commit : 
http://hg.openjdk.java.net/jdk8u/jdk8u-dev/nashorn/rev/0005562330fa

It should appear in the near future jdk8u20 builds

Thanks,

-Sundar

On Tuesday 03 June 2014 09:11 PM, Chris Pettitt wrote:
> Hi Sundar,
>
> Thanks for the quick response on this issue. I saw that this is going into JDK 9. Is there any chance to get this into a JDK 8 update too?
>
> Thanks,
> Chris
>
> P.S. Sorry I did not reply to your original email. I was using digest for nashorn-dev, so I didn't have your email to respond to, but I've switched now.
>
> ________________________________________
> From: Chris Pettitt
> Sent: Monday, June 02, 2014 13:24
> To: nashorn-dev at openjdk.java.net
> Subject: StringIndexOutOfBoundException in NativeRegExp.appendReplacement
>
> Hello,
>
> I believe I have found a bug in NativeRegExp.appendReplacement around handling of '$'. Per [1], A '$' in newstring that does not match one of the forms in Table 22 should be left as is. The appendReplacement function handles this correctly for most cases, but breaks with the following input:
>
>      jjs> "a".replace("a", "$")
>      java.lang.StringIndexOutOfBoundsException: String index out of range: 1
>
> The problem is that appendReplacement assumes that a character will follow the '$' character:
>
>          int cursor = 0;
>          Object[] groups = null;
>
>          while (cursor < replacement.length()) {
>              char nextChar = replacement.charAt(cursor);
>              if (nextChar == '$') {
>                  // Skip past $
>                  cursor++;
>                  nextChar = replacement.charAt(cursor);    // This line fails for the above input, there is no character as index 1.
>
> While the code should be using "$$" as a replacement text, the spec seems to indicate that "$" should work if no characters follow. I tested this with a few JS engines (node, chrome, firefox) and all handle this by replacing "a" with "$", which conforms to my reading of the spec.
>
> Thanks,
> Chris
>
>
> [1]: http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.11



More information about the nashorn-dev mailing list