classpath-patches
[Top][All Lists]
Advanced

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

Re: [cp-patches] RFC: StringIndexOutOfBoundsException fixes in String co


From: Christian Thalinger
Subject: Re: [cp-patches] RFC: StringIndexOutOfBoundsException fixes in String contructors
Date: Wed, 02 Nov 2005 11:21:47 +0100

On Wed, 2005-11-02 at 08:25 +0100, Mark Wielaard wrote:
> For bonus points maybe add what the faulty index actually is to the
> message like:
> 
>     if (offset + count < 0 || offset + count > ascii.length)
>       throw new StringIndexOutOfBoundsException("offset + count: "
>                                                 + offset + count);
> 
> > Ok?
> 
> Yes thanks.

Ok, here is the patch i'll commit.  Fixes also some additional comments.

TWISTI


2005-11-02  Christian Thalinger  <address@hidden>

        * java/lang/String.java (String): Added 
        StringIndexOutOfBoundsException check (overflow) and a message 
        to the exceptions.


Index: java/lang/String.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/String.java,v
retrieving revision 1.72
diff -u -3 -p -r1.72 String.java
--- java/lang/String.java       16 Sep 2005 19:13:35 -0000      1.72
+++ java/lang/String.java       2 Nov 2005 10:08:13 -0000
@@ -233,6 +233,7 @@ public final class String implements Ser
    * @param count the number of characters from data to copy
    * @throws NullPointerException if data is null
    * @throws IndexOutOfBoundsException if (offset &lt; 0 || count &lt; 0
+   *         || offset + count &lt; 0 (overflow)
    *         || offset + count &gt; data.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    */
@@ -256,6 +257,7 @@ public final class String implements Ser
    * @param count the number of characters from ascii to copy
    * @throws NullPointerException if ascii is null
    * @throws IndexOutOfBoundsException if (offset &lt; 0 || count &lt; 0
+   *         || offset + count &lt; 0 (overflow)
    *         || offset + count &gt; ascii.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    * @see #String(byte[])
@@ -267,8 +269,13 @@ public final class String implements Ser
    */
   public String(byte[] ascii, int hibyte, int offset, int count)
   {
-    if (offset < 0 || count < 0 || offset + count > ascii.length)
-      throw new StringIndexOutOfBoundsException();
+    if (offset < 0)
+      throw new StringIndexOutOfBoundsException("offset: " + offset);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException("count: " + count);
+    if (offset + count < 0 || offset + count > ascii.length)
+      throw new StringIndexOutOfBoundsException("offset + count: "
+                                               + (offset + count));
     value = new char[count];
     this.offset = 0;
     this.count = count;
@@ -327,8 +334,13 @@ public final class String implements Ser
   public String(byte[] data, int offset, int count, String encoding)
     throws UnsupportedEncodingException
   {
-    if (offset < 0 || count < 0 || offset + count > data.length)
-      throw new StringIndexOutOfBoundsException();
+    if (offset < 0)
+      throw new StringIndexOutOfBoundsException("offset: " + offset);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException("count: " + count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException("offset + count: "
+                                               + (offset + count));
     try 
       {
         CharsetDecoder csd = Charset.forName(encoding).newDecoder();
@@ -402,8 +414,13 @@ public final class String implements Ser
    */
   public String(byte[] data, int offset, int count)
   {
-    if (offset < 0 || count < 0 || offset + count > data.length)
-      throw new StringIndexOutOfBoundsException();
+    if (offset < 0)
+      throw new StringIndexOutOfBoundsException("offset: " + offset);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException("count: " + count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException("offset + count: "
+                                               + (offset + count));
     int o, c;
     char[] v;
     String encoding;
@@ -512,8 +529,13 @@ public final class String implements Ser
    */
   String(char[] data, int offset, int count, boolean dont_copy)
   {
-    if (offset < 0 || count < 0 || offset + count > data.length)
-      throw new StringIndexOutOfBoundsException();
+    if (offset < 0)
+      throw new StringIndexOutOfBoundsException("offset: " + offset);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException("count: " + count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException("offset + count: "
+                                               + (offset + count));
     if (dont_copy)
       {
         value = data;
@@ -1605,6 +1627,7 @@ public final class String implements Ser
    * @return String containing the chars from data[offset..offset+count]
    * @throws NullPointerException if data is null
    * @throws IndexOutOfBoundsException if (offset &lt; 0 || count &lt; 0
+   *         || offset + count &lt; 0 (overflow)
    *         || offset + count &gt; data.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    * @see #String(char[], int, int)
@@ -1625,6 +1648,7 @@ public final class String implements Ser
    * @return String containing the chars from data[offset..offset+count]
    * @throws NullPointerException if data is null
    * @throws IndexOutOfBoundsException if (offset &lt; 0 || count &lt; 0
+   *         || offset + count &lt; 0 (overflow)
    *         || offset + count &gt; data.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    * @see #String(char[], int, int)






reply via email to

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