Issue Details (XML | Word | Printable)

Key: DERBY-4061
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Kristian Waagan
Reporter: Trejkaz
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

InputStream returned from Blob.getBinaryStream(long, long) terminates the stream by returning 0, should return -1

Created: 16/Feb/09 10:05 PM   Updated: 04/May/09 06:22 PM
Component/s: JDBC
Affects Version/s: 10.3.3.0, 10.4.2.0, 10.5.1.1
Fix Version/s: 10.3.3.1, 10.4.2.1, 10.5.1.1

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works derby-4061-1a.diff 2009-02-20 10:09 AM Kristian Waagan 2 kB
File Licensed for inclusion in ASF works derby-4061-1b.diff 2009-02-20 01:49 PM Kristian Waagan 2 kB

Resolution Date: 24/Feb/09 02:34 PM
Labels:


 Description  « Hide
Take the following code to copy a sub-stream from a BLOB:

Blob blob = rs.getBlob(1);
InputStream stream = blob.getBinaryStream(1000, 2000);
try {
  IOUtils.copy(stream, out);
} finally {
  stream.close();
}

This will loop forever in IOUtils.copy, as the InputStream is returning 0 at end of stream instead of -1. It probably should return -1 to comply with the Javadoc for InputStream.

Notes:
I have not yet checked Blob.getBinaryStream(), or any of the Clob methods, or the client driver. So some of these may have a similar issue.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Kristian Waagan added a comment - 17/Feb/09 07:23 AM
I think the problem is restricted to UpdatableBlobStream only. The combinations I tried with Clob succeeded, and the client driver also succeeds with the repro for both Blob and Clob.
The problem in UpdatableBlobStream, is that the underlying source is asked to read zero bytes, which it does... The calling code fails to handle this scenario, which is a valid one seen from InputStream (from JavaDoc: "If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte.").

Kristian Waagan made changes - 20/Feb/09 08:38 AM
Field Original Value New Value
Assignee Kristian Waagan [ kristwaa ]
Kristian Waagan added a comment - 20/Feb/09 10:09 AM
Patch 1a fixes the bug and adds a regression test. I think there is at least one more bug in the class, I'll investigate later.
Note that the regression test will cause a hang if the fix is removed.

Patch ready for review.
Running the regression suite.

Kristian Waagan made changes - 20/Feb/09 10:09 AM
Attachment derby-4061-1a.diff [ 12400598 ]
Kristian Waagan made changes - 20/Feb/09 10:11 AM
Affects Version/s 10.5.0.0 [ 12313010 ]
Derby Info [Patch Available]
Affects Version/s 10.3.3.0 [ 12313142 ]
Kristian Waagan made changes - 20/Feb/09 10:14 AM
Status Open [ 1 ] In Progress [ 3 ]
Knut Anders Hatlen added a comment - 20/Feb/09 01:09 PM
I think there is one tiny problem with the patch, but I haven't actually tested that it is a problem. I believe that in this code

+ if (remaining == 0 && len != 0) {
+ return -1;
+ }

(len != 0) should be replaced with (len > 0). Otherwise, we return -1 when len is negative instead of throwing an IndexOutOfBoundsException.

In the test there's a typo (infinit -> infinite), and the variable n is unused.

Kristian Waagan added a comment - 20/Feb/09 01:30 PM
Regression tests passed without failures on Solaris 10, Java SE 6.

Repository Revision Date User Message
ASF #746236 Fri Feb 20 13:48:42 UTC 2009 kristwaa DERBY-4061:InputStream returned from Blob.getBinaryStream(long, long) terminates the stream by returning 0, should return -1.
Fixed bug where calling code would enter infinite loop because 0 was returned instead of -1 (EOF). The problem occurred because the actual number of bytes to read was calculated and the case where zero bytes remained wasn't handled properly.
Patch file: DERBY-4061-1b.diff
Files Changed
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java

Kristian Waagan added a comment - 20/Feb/09 01:49 PM
Thanks for the review and the comments, Knut.

Patch 1b addresses the comments.
Committed to trunk with revision 746236.

Kristian Waagan made changes - 20/Feb/09 01:49 PM
Attachment derby-4061-1b.diff [ 12400608 ]
Kristian Waagan made changes - 20/Feb/09 01:49 PM
Fix Version/s 10.5.0.0 [ 12313010 ]
Derby Info [Patch Available]
Repository Revision Date User Message
ASF #747393 Tue Feb 24 14:32:20 UTC 2009 kristwaa DERBY-4061: InputStream returned from Blob.getBinaryStream(long, long) terminates the stream by returning 0, should return -1.
Merged fix from trunk (revision 746236, DERBY-4061-1b.diff).
Files Changed
MODIFY /db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java

Repository Revision Date User Message
ASF #747395 Tue Feb 24 14:33:51 UTC 2009 kristwaa DERBY-4061: InputStream returned from Blob.getBinaryStream(long, long) terminates the stream by returning 0, should return -1.
Merged fix from trunk (revision 746236, DERBY-4061-1b.diff).
Files Changed
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java

Kristian Waagan added a comment - 24/Feb/09 02:34 PM
Backported fix to 10.4 with revision 747393 and to 10.3 with revision 747395.

Kristian Waagan made changes - 24/Feb/09 02:34 PM
Fix Version/s 10.3.3.1 [ 12313143 ]
Resolution Fixed [ 1 ]
Fix Version/s 10.4.2.1 [ 12313401 ]
Status In Progress [ 3 ] Resolved [ 5 ]
Kristian Waagan added a comment - 25/Feb/09 05:00 PM
Tinderbox tests for 10.4 and 10.3 ran successfully.
This issue can be verified and, if found to be fixed, closed by the reporter.

Myrna van Lunteren made changes - 04/May/09 06:22 PM
Affects Version/s 10.5.1.1 [ 12313771 ]
Affects Version/s 10.5.0.0 [ 12313010 ]
Fix Version/s 10.5.1.1 [ 12313771 ]
Fix Version/s 10.5.0.0 [ 12313010 ]