Name: saf@russia Date: 09/06/96
This bug was found by St.Petersburg Java SQE team (by Stanislav Avzan).
The java.io.PipedInputStream.read(b,off,len) method does not work with incorrect
values of off according to the Java language specification.
The Java Language specification
(Version 1.0 - August 1, 1996)
says the following (please see item 22.3.3):
22.3.3 public int read(byte[] b, int off, int len)
throws IOException, NullPointerException,
IndexOutOfBoundsException
The general contract of read(b, off, len) is that it reads some number
of bytes from the input stream and stores them into the buffer array b. An
attempt is made to read as many as len bytes, but a smaller number may
be read, possibly zero. The number of bytes actually read is returned as
an integer.
This method blocks until input data is available, end of file is detected, or
an exception is thrown.
If b is null, a NullPointerException is thrown.
If off is negative, or len is negative, or off+len is greater than the
length of the array b, then an IndexOutOfBoundsException is thrown.
<.....>"
So this method should throw IndexOutOfBoundsException
if off is negative or off is greater than b.length-len.
But in fact it performs some reading prior to throwing it.
Here is the minimized test demonstrating the bug:
----- test5.java ---------------------------------------
import java.io.*;
public class test5 {
public static void main( String[] argv ) {
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream();
try {
is.connect(os);
} catch(Throwable e) {
System.out.println("Test failed: unexpected <"+e+"> thrown");
}
byte[] b = new byte[10];
try {
os.write(101);
os.close();
is.read(b,-2,1);
is.read(b,12,1);
System.out.println("Test failed: IndexOutOfBoundsException expected");
} catch(IndexOutOfBoundsException e) {
try {
int a = is.read();
if (a==101)
System.out.println("Test passed: IndexOutOfBoundsException thrown");
else
System.out.println("Test failed: unexpected reading performed");
} catch(IOException e1) {
System.out.println("Test failed: unexpected <"+e1+"> thrown");
}
} catch(Throwable e) {
System.out.println("Test failed: unexpected <"+e+"> thrown");
}
}
}
----- The output of the test: -------------------------
$JAVA test5
Test failed: unexpected reading performed
-------------------------------------------------------
Workaround:
None
======================================================================