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

[lang] buffer under/overrun on Strings.strip, stripStart & stripEnd

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.1
    • None
    • None
    • Operating System: All
      Platform: All

    • 9433

    Description

      Example:

      stripStart(" ", null);

      fails with a buffer overrun. Here's a patch:

      — Strings.java Mon May 27 00:41:41 2002
      +++ Strings.java.patched Mon May 27 00:41:51 2002
      @@ -1151,16 +1151,16 @@
      int end = str.length();

      if(ch == null) {

      • while( Character.isWhitespace( str.charAt(end-1) ) )
        Unknown macro: {+ while( end > 0 && Character.isWhitespace( str.charAt(end-1) ) ) { end--; }
        } else {
        char chr = ch.charAt(0);
        - while( str.charAt(end-1) == chr ) {
        + while( end > 0 && str.charAt(end-1) == chr ) { end--; } }
      • return str.substring(0, end);
        + return (end > 0) ? str.substring(0, end) : "";
        }

      /**
      @@ -1168,18 +1168,19 @@
      */
      static public String stripStart(String str, String ch) {
      int start = 0;
      + int end = str.length();

      if(ch == null) {

      • while( Character.isWhitespace( str.charAt(start) ) )
        Unknown macro: {+ while( start < end && Character.isWhitespace( str.charAt(start) ) ) { start++; }
        } else {
        char chr = ch.charAt(0);
        - while( str.charAt(start) == chr ) {
        + while( start < end && str.charAt(start) == chr ) { start++; } }
      • return str.substring(start);
        + return (start < end) ? str.substring(start) : "";
        }

      /**

      Attachments

        Activity

          People

            Unassigned Unassigned
            reed.esau@pobox.com Reed Esau
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: