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

Throw NumberFormatException instead of IndexOutOfBoundsException in NumberUtils.getMantissa(String, int)

    XMLWordPrintableJSON

Details

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

    Description

      There is a missing check in the NumberUtils.getMantissa(String, Integer) method where a possible IndexOutOfBoundsException could be thrown when invalid str and stopPos are given.

      private static String getMantissa(final String str, final int stopPos) {        
          final char firstChar = str.charAt(0);
          final boolean hasSign = firstChar == '-' || firstChar == '+';
          return hasSign ? str.substring(1, stopPos) : str.substring(0, stopPos); } 

      When getMantissa("-", 0); is called, the substring method throws IndexOutOfBoundsException. This could happen when calling the public NumberUtils.createNumber("-");.

      A checking can be added to the method and throw NumberFormatException when the number is invalid.

      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: