classpath-patches
[Top][All Lists]
Advanced

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

[cp-patches] RFC: StringIndexOutOfBoundsException fixes in String contru


From: Christian Thalinger
Subject: [cp-patches] RFC: StringIndexOutOfBoundsException fixes in String contructors
Date: Tue, 01 Nov 2005 23:11:00 +0100

Hi!

While fixing a CACAO problem in gnu.testlet.java.lang.String.StringTest,
i noticed that we do not check one case:

offset + count < 0

Thus we throw a ArrayIndexOutOfBoundsException instead of a
StringIndexOutOfBoundsException.  The attached patch adds this checks
and, while everybody is talking about better exception message, adds the
index to the message.

Ok?

TWISTI


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

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


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       1 Nov 2005 22:10:05 -0000
@@ -233,7 +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 &gt; data.length)
+   *         || offset + count &lt; 0 || offset + count &gt; data.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    */
   public String(char[] data, int offset, int count)
@@ -256,7 +256,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 &gt; ascii.length)
+   *         || offset + count &lt; 0 || offset + count &gt; ascii.length)
    *         (while unspecified, this is a StringIndexOutOfBoundsException)
    * @see #String(byte[])
    * @see #String(byte[], String)
@@ -267,8 +267,12 @@ 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);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException(count);
+    if (offset + count < 0 || offset + count > ascii.length)
+      throw new StringIndexOutOfBoundsException(offset + count);
     value = new char[count];
     this.offset = 0;
     this.count = count;
@@ -327,8 +331,12 @@ 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);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException(count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException(offset + count);
     try 
       {
         CharsetDecoder csd = Charset.forName(encoding).newDecoder();
@@ -402,8 +410,12 @@ 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);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException(count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException(offset + count);
     int o, c;
     char[] v;
     String encoding;
@@ -512,8 +524,12 @@ 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);
+    if (count < 0)
+      throw new StringIndexOutOfBoundsException(count);
+    if (offset + count < 0 || offset + count > data.length)
+      throw new StringIndexOutOfBoundsException(offset + count);
     if (dont_copy)
       {
         value = data;






reply via email to

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