Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-857

StringIndexOutOfBoundsException in CharSequenceTranslator

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.2
    • Component/s: lang.text.translate.*
    • Labels:

      Description

      I found that there is bad surrogate pair handling in the CharSequenceTranslator

      This is a simple test case for this problem.
      \uD83D\uDE30 is a surrogate pair.

      @Test
      public void testEscapeSurrogatePairs() throws Exception {
          assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
      }
      

      You'll get the exception as shown below.

      java.lang.StringIndexOutOfBoundsException: String index out of range: 2
      	at java.lang.String.charAt(String.java:658)
      	at java.lang.Character.codePointAt(Character.java:4668)
      	at org.apache.commons.lang3.text.translate.CharSequenceTranslator.translate(CharSequenceTranslator.java:95)
      	at org.apache.commons.lang3.text.translate.CharSequenceTranslator.translate(CharSequenceTranslator.java:59)
      	at org.apache.commons.lang3.StringEscapeUtils.escapeCsv(StringEscapeUtils.java:556)
      

      Patch attached, the method affected:

      1. public final void translate(CharSequence input, Writer out) throws IOException

        Attachments

        1. CharSequenceTranslator_translate.patch
          0.8 kB
          Kazuki Hamasaki

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ashphy Kazuki Hamasaki
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: