Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8065421

String replaceAll can throw StringIndexOutOfBoundsException

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      "_".replaceAll("_", "\\") causes a StringIndexOutOfBoundsException.

      This exception is not documented to be thrown from this method.
      https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

      I think the reason behind this is that the regex engine is seeing the escape character \, with nothing afterward to escape. In my opinion, this should instead be thrown as a PatternSyntaxException, as documented.

      Additionally, "ABC".replaceAll("_", "\\") does not throw an exception, probably because the regex causing the exception is never evaluated. Perhaps this should fail fast, so that an invalid regex will not be a surprise during runtime.


      REPRODUCIBILITY :
      This bug can be reproduced always.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: