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

test failed with StringIndexOutOfBoundsException in com.sun.xml.internal.dtdparser.XmlNames.isName

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 8
    • xml

      A DESCRIPTION OF THE PROBLEM :
      For method com.sun.xml.internal.dtdparser.XmlNames.isName tools.jar of jdk1.8.0_301, test failed with empty input.
      I think you it will be better if judge the input string is empty or not. Not to mention that it has already judged whether it is null.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      the test case:
       @Test
          public void test_isName_2_2(){

              String string0 = "";
              com.sun.xml.internal.dtdparser.XmlNames.isName(string0);
          }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      return false
      ACTUAL -
      java.lang.StringIndexOutOfBoundsException: String index out of range: 0

      at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
      at java.base/java.lang.String.charAt(String.java:711)
      at com.sun.xml.internal.dtdparser.XmlNames.isName(XmlNames.java:51)

      ---------- BEGIN SOURCE ----------
      public static boolean isName(String value) {
              if (value == null) {
                  return false;
              } else {
                  char c = value.charAt(0);
                  if (!XmlChars.isLetter(c) && c != '_' && c != ':') {
                      return false;
                  } else {
                      for(int i = 1; i < value.length(); ++i) {
                          if (!XmlChars.isNameChar(value.charAt(i))) {
                              return false;
                          }
                      }

                      return true;
                  }
              }
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public static boolean isName(String value) {
              if (value == null || value.isEmpty() ) {
                  return false;
              } else {
                  char c = value.charAt(0);
                  if (!XmlChars.isLetter(c) && c != '_' && c != ':') {
                      return false;
                  } else {
                      for(int i = 1; i < value.length(); ++i) {
                          if (!XmlChars.isNameChar(value.charAt(i))) {
                              return false;
                          }
                      }

                      return true;
                  }
              }
          }

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: