You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2002/05/27 09:55:26 UTC

DO NOT REPLY [Bug 9433] New: - buffer under/overrun on Strings.strip, stripStart & stripEnd

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9433>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9433

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

           Summary: buffer under/overrun on Strings.strip, stripStart &
                    stripEnd
           Product: Commons
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Sandbox
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: reed.esau@pobox.com


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) ) ) {
+            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) ) ) {
+            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) : "";
     }
 
     /**

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>