classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [cp-patches] RFC: StringIndexOutOfBoundsException fixes inString con


From: Christian Thalinger
Subject: RE: [cp-patches] RFC: StringIndexOutOfBoundsException fixes inString contructors
Date: Tue, 06 Dec 2005 19:52:31 +0100

On Wed, 2005-11-02 at 12:50 +0100, Jeroen Frijters wrote:
> Christian Thalinger wrote:
> > +    if (offset + count < 0 || offset + count > ascii.length)
> 
> You can write this more efficiently as:
> 
>     if (ascii.length - offset < count)
> 
> (This assumes that offset and count have previously been checked to be
> non-negative.)

Hi!

Sorry for the long delay, but here it is.  I left a comment in there so
it's clear what we are checking for.  Commited as attached.

TWISTI


2005-12-06  Christian Thalinger  <address@hidden>

        * java/lang/String.java (String): Better out-of-bounds and 
        overflow checks.


Index: java/lang/String.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/String.java,v
retrieving revision 1.76
diff -u -3 -p -r1.76 String.java
--- java/lang/String.java       8 Nov 2005 16:04:49 -0000       1.76
+++ java/lang/String.java       6 Dec 2005 18:47:30 -0000
@@ -273,7 +273,8 @@ public final class String implements Ser
       throw new StringIndexOutOfBoundsException("offset: " + offset);
     if (count < 0)
       throw new StringIndexOutOfBoundsException("count: " + count);
-    if (offset + count < 0 || offset + count > ascii.length)
+    // equivalent to: offset + count < 0 || offset + count > ascii.length
+    if (ascii.length - offset < count)
       throw new StringIndexOutOfBoundsException("offset + count: "
                                                + (offset + count));
     value = new char[count];
@@ -338,7 +339,8 @@ public final class String implements Ser
       throw new StringIndexOutOfBoundsException("offset: " + offset);
     if (count < 0)
       throw new StringIndexOutOfBoundsException("count: " + count);
-    if (offset + count < 0 || offset + count > data.length)
+    // equivalent to: offset + count < 0 || offset + count > data.length
+    if (data.length - offset < count)
       throw new StringIndexOutOfBoundsException("offset + count: "
                                                + (offset + count));
     try 
@@ -418,7 +420,8 @@ public final class String implements Ser
       throw new StringIndexOutOfBoundsException("offset: " + offset);
     if (count < 0)
       throw new StringIndexOutOfBoundsException("count: " + count);
-    if (offset + count < 0 || offset + count > data.length)
+    // equivalent to: offset + count < 0 || offset + count > data.length
+    if (data.length - offset < count)
       throw new StringIndexOutOfBoundsException("offset + count: "
                                                + (offset + count));
     int o, c;
@@ -533,7 +536,8 @@ public final class String implements Ser
       throw new StringIndexOutOfBoundsException("offset: " + offset);
     if (count < 0)
       throw new StringIndexOutOfBoundsException("count: " + count);
-    if (offset + count < 0 || offset + count > data.length)
+    // equivalent to: offset + count < 0 || offset + count > data.length
+    if (data.length - offset < count)
       throw new StringIndexOutOfBoundsException("offset + count: "
                                                + (offset + count));
     if (dont_copy)






reply via email to

[Prev in Thread] Current Thread [Next in Thread]