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

StringIndexOutOfBoundsException in CharSequenceTranslator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 3.2
    • lang.text.translate.*

    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

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

              Dates

                Created:
                Updated:
                Resolved: