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

StringUtils.equals with CharSequence - IndexOutOfBoundsException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 3.4
    • 3.5
    • lang.*
    • None

    Description

      Good day,

      This is my first report here, so I'm really sorry if I did not fill in the form right .

      I just ran into a bug with the method
      public static boolean equals(final CharSequence cs1, final CharSequence cs2).

      If one of the object is not the String object, the method use the CharSequenceUtils to check the equality.

      The problem is that using Math.max(cs1.length(), cs2.length()) give the max length of the 2 objects. Then 1 of the object throw IndexOutOfBoundsException.

      I think it will be better to check the size before using CharSequenceUtils as the method equalsIgnoreCase.

      Maybe this code could correct the bug :

      if (cs1 == cs2) {
      return true;
      } else if (cs1 == null || cs2 == null) {
      return false;
      } else if (cs1.length() != cs2.length()) {
      return false;
      } else if (cs1 instanceof String && cs2 instanceof String) {
      return cs1.equals(cs2);
      } else {
      return CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, cs1.length());
      }

      Kind regards,

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              yogo95 NEGRO Eric
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: