On 7/21/06, Andrew Zhang <[EMAIL PROTECTED]> wrote:
On 7/21/06, Vladimir Ivanov <[EMAIL PROTECTED]> wrote: > > On 7/21/06, Andrew Zhang <[EMAIL PROTECTED]> wrote: > >Invoking assertArray(array.length, 0, -1) will throw NullPointerException > >instead of IndexOutOfBoundException. It's not compatible with RI. > >Do you think so? > > No. We should check reference before using :) To corectly process a NPE we > should write: > > method m(Object[] array, int offset, int length) { > if (array == null) > throw new NullPointerException(); > assertArrayIndex(array.length, offset, length); > .......... > } But RI throws IndexOutOfBoundsException for (null,-1,-1). :) (at least, in most cases). Would you please have a check? Thanks!
I don't check the most cases, but just combine sources from issues 791 and 801. Seems, in different packages/ methods RI have different behavior: ============= test.java ==================== import java.nio.*; public class test { public static void main (String[] args) { try { CharBuffer.allocate(10).put((char[]) null, -11, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("c: " + e); } try { FloatBuffer.allocate(10).put((float[]) null, -1, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("f: " + e); } try { DoubleBuffer.allocate(10).put((double[]) null, -1, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("d: " + e); } try { IntBuffer.allocate(10).put((int[]) null, -11, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("i: " + e); } try { LongBuffer.allocate(10).put((long[]) null, -1, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("l: " + e); } try { ShortBuffer.allocate(10).put((short[]) null, -1, Integer.MAX_VALUE); } catch (Exception e) { System.out.println("s: " + e); } try { new String((int[]) null, -2, Integer.MAX_VALUE);; } catch (Exception e) { System.out.println("str: " + e); } } } ======================================== Output: C:\tmp\tmp17>C:\jrockit-jdk1.5.0-windows-ia32\bin\java.exe -cp . -showversion test java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) BEA WebLogic JRockit(R) (build dra-38972-20041208-2001-win-ia32, R25.0.0-75, GC: System optimized over throughput (initial strategy singleparpar)) c: java.lang.NullPointerException f: java.lang.NullPointerException d: java.lang.NullPointerException i: java.lang.NullPointerException l: java.lang.NullPointerException s: java.lang.NullPointerException str: java.lang.StringIndexOutOfBoundsException: String index out of range: -2 C:\tmp\tmp17>C:\harmony\trunk_0427\deploy\jdk\jre\bin\java.exe -cp . -showversion test java version 1.5 (subset) (c) Copyright 1991, 2006 The Apache Software Foundation or its licensors, as applicable. c: java.lang.NullPointerException f: java.lang.NullPointerException d: java.lang.NullPointerException i: java.lang.NullPointerException l: java.lang.NullPointerException s: java.lang.IndexOutOfBoundsException str: java.lang.NullPointerException So, we need 2 methods with different checks order: void assertArrayIndex(Object[] array, int offset, int length){ if (offset < 0) throw new IndexOutOfBoundsException("Negative offset"); if (length < 0) throw new IndexOutOfBoundsException("Negative length"); if ((long)offset + (long)length > array.length) //can throw NullPointerException throw new IndexOutOfBoundsException("offset+length exceed buffer size"); } and void assertArrayIndex(int arrayLength, int offset, int length) { .... } thanks, Vladimir PS. seems, behavior for ShortBuffer.allocate(10).put((short[]) null, -1, ..); should be unified with other methods or write something like
> void assertArrayIndex(Object[] array, int offset, int length){ > if (array == null) > throw new NullPointerException(); > if (offset < 0) > throw new IndexOutOfBoundsException("Negative offset"); > if (length < 0) > throw new IndexOutOfBoundsException("Negative length"); > if ((long)offset + (long)length > array.length) > throw new IndexOutOfBoundsException("offset+length exceed buffer > size"); > } > > thanks, Vladimir > > On 7/21/06, Andrew Zhang <[EMAIL PROTECTED]> wrote: > > > On 7/21/06, Vladimir Ivanov <[EMAIL PROTECTED]> wrote: > > > > > > Seems, the same problem is existing not in NIO module only. In any > case, > > > it > > > will be useful to use one method instead of many copies of code. > That's > > > for > > > me, I just can't find the appropriate class for 'package' functions so > > fix > > > it inline. > > > Seems, the function like: > > > void assertArrayIndex(int arrayLength, int offset, int length){ > > > if(offset < 0 || length < 0 || (long)offset + (long)length > > > > arrayLength){ > > > throw new IndexOutOfBoundsException(); > > > } > > > } > > > or may be even something like that > > > void assertArrayIndex(int arrayLength, int offset, int length){ > > > if (offset < 0) > > > throw new IndexOutOfBoundsException("Negative offset"); > > > if (length < 0) throw new > > > IndexOutOfBoundsException("Negative length"); > > > if ((long)offset + (long)length > arrayLength) > > > throw new IndexOutOfBoundsException("offset+length exceed > > > buffer > > > size"); > > > } > > > > > > Hi Vladimir, I have some comments on your revised method. :) > > Using int as the first parameter will cause problem. Consider: > > Invoking assertArray(array.length, 0, -1) will throw > NullPointerException > > instead of IndexOutOfBoundException. It's not compatible with RI. > > Do you think so? > > > > Thanks! > > > > > > Thanks, Vladimir > > > > > > > > > On 7/21/06, Andrew Zhang <[EMAIL PROTECTED]> wrote: > > > > > > > > Hi Vladimir, > > > > > > > > I noticed the same problem is existing in many places of NIO module. > > > > > > > > How about writing a utility function for array index exception > check? > > > > like: > > > > > > > > void assertArrayIndex(Object[] array, int offset, int length){ > > > > if(offset < 0 || length < 0 || (long)offset + (long)length > > > > array.length > > > > ){ > > > > throw new IndexOutOfBoundsException(); > > > > } > > > > } > > > > > > > > How do you think about it? Any suggestions? > > > > > > > > Thanks! > > > > Best regards, > > > > > > > > > > > > On 7/21/06, Paulex Yang (JIRA) <[EMAIL PROTECTED]> wrote: > > > > > > > > > > [ http://issues.apache.org/jira/browse/HARMONY-932?page=all] > > > > > > > > > > Paulex Yang closed HARMONY-932. > > > > > ------------------------------- > > > > > > > > > > Estimated Complexity: Novice > > > > > > > > > > Verified by Vladimir. > > > > > > > > > > > > [classlib][nio]java.nio.?hannel.DatagramChannel.read(ByteBuffer[] > > , > > > > int, > > > > > Integer.MAX_VALUE) throws NotYetConnectedException while RI throws > > > > > IndexOutOfBoundsException > > > > > > > > > > > > > > > > > > > > > --------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > > > > > > > > > > > Key: HARMONY-932 > > > > > > URL: > > > http://issues.apache.org/jira/browse/HARMONY-932 > > > > > > Project: Harmony > > > > > > Issue Type: Bug > > > > > > Components: Classlib > > > > > > Reporter: Vladimir Ivanov > > > > > > Assigned To: Paulex Yang > > > > > > Attachments: DatagramChannelImpl.patch > > > > > > > > > > > > > > > > > > The Harmony methods java.nio.?hannel.DatagramChannel.read/write > > > > (ByteBuffer[] > > > > > , int, Integer.MAX_VALUE) throws NotYetConnectedException while RI > > > > throws > > > > > IndexOutOfBoundsException. > > > > > > ==================== test.java ====================== > > > > > > import java.nio.channels.*; > > > > > > import java.nio.*; > > > > > > public class test { > > > > > > public static void main (String[] args) { > > > > > > try { > > > > > > DatagramChannel.open().read(new ByteBuffer[] {}, 2, > > > > > Integer.MAX_VALUE); > > > > > > } catch (Exception e) { > > > > > > e.printStackTrace(); > > > > > > } > > > > > > try { > > > > > > DatagramChannel.open().write(new ByteBuffer[] {}, 2, > > > > > Integer.MAX_VALUE); > > > > > > } catch (Exception e) { > > > > > > e.printStackTrace(); > > > > > > } > > > > > > } > > > > > > } > > > > > > ================================================= > > > > > > Output: > > > > > > C:\tmp\tmp17>C:\jdk1.5.0_06\bin\java.exe -cp . -showversion test > > > > > > java version "1.5.0_06" > > > > > > Java(TM) 2 Runtime Environment, Standard Edition (build > > 1.5.0_06-b05 > > > ) > > > > > > Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode) > > > > > > java.lang.IndexOutOfBoundsException > > > > > > at sun.nio.ch.DatagramChannelImpl.read( > > > > DatagramChannelImpl.java > > > > > :366) > > > > > > at test.main(test.java:8) > > > > > > java.lang.IndexOutOfBoundsException > > > > > > at sun.nio.ch.DatagramChannelImpl.write( > > > > DatagramChannelImpl.java > > > > > :429) > > > > > > at test.main(test.java:13) > > > > > > C:\tmp\tmp17>C:\jrockit-jdk1.5.0-windows-ia32\bin\java.exe -cp . > > > > > -showversion test > > > > > > java version "1.5.0" > > > > > > Java(TM) 2 Runtime Environment, Standard Edition (build > 1.5.0-b64) > > > > > > BEA WebLogic JRockit(R) (build dra-38972-20041208-2001-win-ia32, > > > > > R25.0.0-75, GC: System optimized over throughput (initial strategy > > > > > singleparpar)) > > > > > > OutOfMemoryError > > > > > > -------end of stacktrace > > > > > > C:\tmp\tmp17>C:\harmony\trunk_0427\deploy\jdk\jre\bin\java.exe > -cp > > . > > > > > -showversion test > > > > > > java version 1.5 (subset) > > > > > > (c) Copyright 1991, 2006 The Apache Software Foundation or its > > > > > licensors, as applicable. > > > > > > java.nio.channels.NotYetConnectedException > > > > > > at > > > > > > > org.apache.harmony.nio.internal.DatagramChannelImpl.checkOpenConnected > > > ( > > > > > DatagramChannelImpl.java:567) > > > > > > at > > org.apache.harmony.nio.internal.DatagramChannelImpl.read( > > > > > DatagramChannelImpl.java:387) > > > > > > at test.main(test.java:8) > > > > > > java.nio.channels.NotYetConnectedException > > > > > > at > > > > > > > org.apache.harmony.nio.internal.DatagramChannelImpl.checkOpenConnected > > > ( > > > > > DatagramChannelImpl.java:567) > > > > > > at > > org.apache.harmony.nio.internal.DatagramChannelImpl.write > > > ( > > > > > DatagramChannelImpl.java:477) > > > > > > at test.main(test.java:13) > > > > > > > > > > > > > > > > > > > > > C:\tmp\tmp17>C:\harmony\drlvm\trunk\build\win_ia32_msvc_debug\deploy\jre\bin\ij > > > > > -Dvm.assert_dialog=false -cp . -showversion test > > > > > > Apache Harmony DRLVM HEAD-4807 (2006-05-02), Windows/ia32/msvc > > 1310, > > > > > debug > > > > > > java.nio.channels.NotYetConnectedException > > > > > > at > > > > > > > org.apache.harmony.nio.internal.DatagramChannelImpl.checkOpenConnected > > > ( > > > > > DatagramChannelImpl.java:567) > > > > > > at > > org.apache.harmony.nio.internal.DatagramChannelImpl.read( > > > > > DatagramChannelImpl.java:387) > > > > > > at test.main(test.java:8) > > > > > > java.nio.channels.NotYetConnectedException > > > > > > at > > > > > > > org.apache.harmony.nio.internal.DatagramChannelImpl.checkOpenConnected > > > ( > > > > > DatagramChannelImpl.java:567) > > > > > > at > > org.apache.harmony.nio.internal.DatagramChannelImpl.write > > > ( > > > > > DatagramChannelImpl.java:477) > > > > > > at test.main(test.java:13) > > > > > > > > > > -- > > > > > 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 > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > Andrew Zhang > > > > China Software Development Lab, IBM > > > > > > > > > > > > > > > > > > > > -- > > Andrew Zhang > > China Software Development Lab, IBM > > > > > > -- Andrew Zhang China Software Development Lab, IBM