commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 9433] New: - buffer under/overrun on Strings.strip, stripStart & stripEnd
Date Mon, 27 May 2002 07:55:26 GMT
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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message