[Bug libgcj/15447] New: Lack of exception raised in ByteBufferImpl & co.

denis dot walesch at free dot fr gcc-bugzilla@gcc.gnu.org
Sat May 15 16:02:00 GMT 2004


After slicing a ByteBuffer, getting or setting values does'nt reflect this action 
 and the exceptions promised by the interface of get/set are not raised. As i am
a bad english writer because it's not my native language, you'll find following
a little test case.

--- cut here ------

import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.BufferUnderflowException;

public class BufferTest {

  public static void dumpBytes(ByteBuffer b) {
    System.out.println( "dumpBytes : " );
    for( int i=0; i<b.limit(); i++ ) {
      System.out.println( "byte #" + i + " = " +
                          Integer.toHexString( b.get(i) ) );
    }
  }

  public static void main(String args[]) {
    byte a[] = new byte[32];
    for( int i=0; i<32; i++ ) a[i] = (byte)i;

    ByteBuffer b = ByteBuffer.wrap(a, 3, 5).slice();
    System.out.println( "wrap()" );
    dumpBytes(b.wrap(a, 3, 5));

    System.out.println( "b.position() = "+b.position() );
    System.out.println( "b.limit() = "+b.limit() );

    dumpBytes(b.slice());

    try {
      while(true) {
        System.out.println( "get(position()) = " +
                            b.get() );
      }
    }
    catch( BufferUnderflowException bue ) {
      System.out.println( "Ok vide !!!" );
    }

  }
}

--- cut here ------

I can't access to cvs where i work to get the latest release and my home
internet access is rather slow to a so big thing as gcc. But browsing the latest
snapshot via cvsweb, i see that the access to values after slice has been
corrected but not the exception that may be raised.

Following, you'll find a patch of nio package of gcc 3.4.0 that both correct the
access of the underlying buffer (now corrected) and the exception that must be
raised.

--- cut here ------
diff -Naur nio.orig/ByteBuffer.java nio/ByteBuffer.java
--- nio.orig/ByteBuffer.java	Fri May 14 11:24:02 2004
+++ nio/ByteBuffer.java	Fri May 14 11:44:59 2004
@@ -65,11 +65,12 @@
   /**
    * Allocates a new direct byte buffer.
    */ 
+
   public static ByteBuffer allocateDirect (int capacity)
   {
     return DirectByteBufferImpl.allocate (capacity);
   }
-
+  
   /**
    * Allocates a new <code>ByteBuffer</code> object with a given capacity.
    */
diff -Naur nio.orig/ByteBufferImpl.java nio/ByteBufferImpl.java
--- nio.orig/ByteBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/ByteBufferImpl.java	Fri May 14 11:42:37 2004
@@ -126,10 +126,16 @@
 
   /**
    * Relative get method. Reads the next <code>byte</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public byte get ()
   {
-    byte result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    byte result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -138,6 +144,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public ByteBuffer put (byte value)
@@ -145,7 +153,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -159,7 +170,10 @@
    */
   final public byte get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -175,7 +189,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/CharBufferImpl.java nio/CharBufferImpl.java
--- nio.orig/CharBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/CharBufferImpl.java	Fri May 14 11:42:37 2004
@@ -115,10 +115,16 @@
   
   /**
    * Relative get method. Reads the next <code>char</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public char get ()
   {
-    char result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    char result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -127,6 +133,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public CharBuffer put (char value)
@@ -134,7 +142,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -152,7 +163,7 @@
         || index >= limit ())
       throw new IndexOutOfBoundsException ();
     
-    return backing_buffer [index];
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -172,7 +183,7 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/DoubleBufferImpl.java nio/DoubleBufferImpl.java
--- nio.orig/DoubleBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/DoubleBufferImpl.java	Fri May 14 11:42:37 2004
@@ -97,10 +97,16 @@
 
   /**
    * Relative get method. Reads the next <code>double</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public double get ()
   {
-    double result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    double result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -109,6 +115,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public DoubleBuffer put (double value)
@@ -116,7 +124,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -130,7 +141,10 @@
    */
   final public double get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -146,7 +160,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/FloatBufferImpl.java nio/FloatBufferImpl.java
--- nio.orig/FloatBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/FloatBufferImpl.java	Fri May 14 11:42:37 2004
@@ -97,10 +97,16 @@
 
   /**
    * Relative get method. Reads the next <code>float</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public float get ()
   {
-    float result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    float result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -109,6 +115,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public FloatBuffer put (float value)
@@ -116,7 +124,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -130,7 +141,10 @@
    */
   final public float get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -146,7 +160,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/IntBufferImpl.java nio/IntBufferImpl.java
--- nio.orig/IntBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/IntBufferImpl.java	Fri May 14 11:42:37 2004
@@ -97,10 +97,16 @@
 
   /**
    * Relative get method. Reads the next <code>int</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public int get ()
   {
-    int result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    int result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -109,6 +115,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public IntBuffer put (int value)
@@ -116,7 +124,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -130,7 +141,10 @@
    */
   final public int get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -146,7 +160,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/LongBufferImpl.java nio/LongBufferImpl.java
--- nio.orig/LongBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/LongBufferImpl.java	Fri May 14 11:42:37 2004
@@ -97,10 +97,16 @@
 
   /**
    * Relative get method. Reads the next <code>long</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public long get ()
   {
-    long result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    long result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -109,6 +115,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public LongBuffer put (long value)
@@ -116,7 +124,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -130,7 +141,10 @@
    */
   final public long get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -146,7 +160,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
diff -Naur nio.orig/ShortBufferImpl.java nio/ShortBufferImpl.java
--- nio.orig/ShortBufferImpl.java	Fri May 14 11:24:02 2004
+++ nio/ShortBufferImpl.java	Fri May 14 11:42:37 2004
@@ -97,10 +97,16 @@
 
   /**
    * Relative get method. Reads the next <code>short</code> from the buffer.
+   *
+   * @exception BufferUnderflowException If there are no remaining
+   * <code>bytes</code> in this buffer.
    */
   final public short get ()
   {
-    short result = backing_buffer [position ()];
+    if( remaining() > 0 )    
+      throw new BufferUnderflowException();
+    
+    short result = backing_buffer [array_offset+position ()];
     position (position () + 1);
     return result;
   }
@@ -109,6 +115,8 @@
    * Relative put method. Writes <code>value</code> to the next position
    * in the buffer.
    * 
+   * @exception BufferOverflowException If there no remaining 
+   * <code>bytes</code> in this buffer.
    * @exception ReadOnlyBufferException If this buffer is read-only.
    */
   final public ShortBuffer put (short value)
@@ -116,7 +124,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
 	  	    
-    backing_buffer [position ()] = value;
+    if( remaining() > 0 )    
+      throw new BufferOverflowException();
+    
+    backing_buffer [array_offset+position ()] = value;
     position (position () + 1);
     return this;
   }
@@ -130,7 +141,10 @@
    */
   final public short get (int index)
   {
-    return backing_buffer [index];
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    return backing_buffer [array_offset+index];
   }
   
   /**
@@ -146,7 +160,10 @@
     if (readOnly)
       throw new ReadOnlyBufferException ();
     	    
-    backing_buffer [index] = value;
+    if( index < 0 || limit() < index )
+      throw new IndexOutOfBoundsException();
+    
+    backing_buffer [array_offset+index] = value;
     return this;
   }
   
--- cut here ------

I hope this help.

-- 
           Summary: Lack of exception raised in ByteBufferImpl & co.
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: denis dot walesch at free dot fr
                CC: gcc-bugs at gcc dot gnu dot org,java-prs at gcc dot gnu
                    dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15447



More information about the Gcc-bugs mailing list