harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Svetlana Samoilenko (JIRA)" <j...@apache.org>
Subject [jira] Created: (HARMONY-90) StringBuffer.setLength(int) doesn't throw IndexOutOfBoundsException if the argument is negative
Date Tue, 14 Feb 2006 05:10:08 GMT
StringBuffer.setLength(int) doesn't throw IndexOutOfBoundsException if the argument is negative
-----------------------------------------------------------------------------------------------

         Key: HARMONY-90
         URL: http://issues.apache.org/jira/browse/HARMONY-90
     Project: Harmony
        Type: Bug
  Components: Classlib  
    Reporter: Svetlana Samoilenko


According to j2se 1.4.2 and 1.5 specification java.lang.StringBuffer.setLength(int) must throw
IndexOutOfBoundsException if the argument is negative

Code to reproduce: 
public class test2 { 
    public static void main(String[] args) throws Exception {  
        StringBuffer buffer = new StringBuffer("abcde");           
        try {
            buffer.setLength(-1);
            System.out.println("FAIL. IndexOutOfBoundsException must be thrown.");  
        } catch (IndexOutOfBoundsException e) {
            System.out.println("PASS. "+e);  
        }  
    }
}

Steps to Reproduce: 
1. Build Harmony (check-out on 2006-01-30) j2se subset as described in README.txt. 
2. Compile test2.java using BEA 1.4 javac 
> javac -d . test2.java 
3. Run java using compatible VM (J9) 
> java -showversion test2

Output: 
C:\tmp>C:\jrockit-j2sdk1.4.2_04\bin\java.exe -showversion test2 
java version "1.4.2_04" 
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05) 
BEA WebLogic JRockit(TM) 1.4.2_04 JVM (build ari-31788-20040616-1132-win-ia32, Native Threads,
GC strategy: parallel) 
PASS. java.lang.StringIndexOutOfBoundsException: String index out of range: -1

C:\tmp>C:\harmony\trunk\deploy\jre\bin\java -showversion test2 
(c) Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable. 
FAIL. IndexOutOfBoundsException must be thrown

Suggected fix:
Index: trunk/modules/luni/src/main/java/java/lang/StringBuffer.java
===================================================================
---   trunk/modules/luni/src/main/java/java/lang/StringBuffer.java   (revision 377365)
+++ trunk/modules/luni/src/main/java/java/lang/StringBuffer.java   (working copy)
@@ -798,7 +798,10 @@
             * @see #length
             */
            public synchronized void setLength(int length) {
-           if (length > value.length)
+              if (length < 0) {
+                  throw new IndexOutOfBoundsException("argument is negative");
+              }           
+              if (length > value.length)

Suggested junit test case:
------------------------ StringBufferTest.java -------------------------------------------------

import junit.framework.*; 

public class StringBufferTest extends TestCase { 
    public static void main(String[] args) { 
        junit.textui.TestRunner.run(StringBuffer.class); 
    } 
    public void test_read () { 
        StringBuffer buffer = new StringBuffer("abcde");           
        try {
            buffer.setLength(-1);
            fail("IndexOutOfBoundsException must be thrown");  
        } catch (IndexOutOfBoundsException e) {
            //expected
        }    
    } 
}



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


Mime
View raw message