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

Wrong number checking in NumberUtils cause StringIndexOutOfBoundsException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 3.15.0
    • None

    Description

      There is a wrong conditional check in NumberUtils.createNumber method, that could result in StringIndexOutOfBoundsException with specially crafted invalid string.

       

          public static Number createNumber(final String str) {
              ...
              final int decPos = str.indexOf('.');
              final int expPos = str.indexOf('e') + str.indexOf('E') + 1; // assumes both not present
              // if both e and E are present, this is caught by the checks on expPos (which prevent IOOBE)
              if (decPos > -1) { // there is a decimal point
                  if (expPos > -1) { // there is an exponent
                      if (expPos < decPos || expPos > length) { // prevents double exponent causing IOOBE
                          throw new NumberFormatException(str + " is not a valid number.");
                      }
                      dec = str.substring(decPos + 1, expPos); 

      Although checking is implied for the case of both e and E are present, there is an exceptional case which are not taken care of. If we provide the String E123e.3, both decPos and expPos will be 5. Then it get pass the expPos < decPos check and the substring will throw a StringIndexOutOfBoundsException because decPos + 1 > expPos.

      To fix this issue, the condition should be expPos <= decPst.

       

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              arthur.chan Sheung Chi Chan
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: