db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r986345 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/Blob.java engine/org/apache/derby/impl/jdbc/EmbedBlob.java testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java
Date Tue, 17 Aug 2010 14:56:55 GMT
Author: kahatlen
Date: Tue Aug 17 14:56:55 2010
New Revision: 986345

URL: http://svn.apache.org/viewvc?rev=986345&view=rev
Log:
DERBY-3898: Blob.setBytes differs between embedded and client driver
when the specified length is invalid

Added fix and test case for a remaining corner case: When the sum of
offset and length is greater than Integer.MAX_VALUE, the client driver
silently ignores the error whereas the embedded driver fails with an
IndexOutOfBoundsException.

The unexpected results are caused by a check for

  offset + len > bytes.length

where offset+len overflows and evaluates to a negative value. The fix
changes this condition to the equivalent

  len > bytes.length - offset

which won't overflow (because both bytes.length and offset are known
to be non-negative at this point in the code, and subtracting one
non-negative int from another is guaranteed to result in a value in
the range [-Integer.MAX_VALUE, Integer.MAX_VALUE]).

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java?rev=986345&r1=986344&r2=986345&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java Tue Aug 17 14:56:55
2010
@@ -469,7 +469,7 @@ public class Blob extends Lob implements
         if (len == 0) {
             return 0;
         }
-        if (len + offset > bytes.length) {
+        if (len > bytes.length - offset) {
             throw new SqlException(agent_.logWriter_,
                     new ClientMessageId(SQLState.BLOB_LENGTH_TOO_LONG),
                     new Integer(len));

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?rev=986345&r1=986344&r2=986345&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Tue Aug 17 14:56:55
2010
@@ -898,7 +898,7 @@ final class EmbedBlob extends Connection
         if (len == 0) {
             return 0;
         }
-        if (len + offset > bytes.length) {
+        if (len > bytes.length - offset) {
             throw Util.generateCsSQLException(SQLState.BLOB_LENGTH_TOO_LONG,
                     new Long(len));
         }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java?rev=986345&r1=986344&r2=986345&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/BlobSetBytesBoundaryTest.java
Tue Aug 17 14:56:55 2010
@@ -90,7 +90,16 @@ public class BlobSetBytesBoundaryTest ex
         } catch (SQLException sqle) {
             assertSQLState("XJ079", sqle);
         }
-        
+
+        // Also check that we fail with the expected error if the sum of
+        // offset and length is greater than Integer.MAX_VALUE.
+        try {
+            blob.setBytes(1, new byte[100], 10, Integer.MAX_VALUE);
+            fail("setBytes() should fail when offset+length > bytes.length");
+        } catch (SQLException sqle) {
+            assertSQLState("XJ079", sqle);
+        }
+
         stmt.close();
     }
     



Mime
View raw message