db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r769948 - in /db/derby/code/trunk/java: build/org/apache/derbyBuild/ client/org/apache/derby/client/am/ engine/org/apache/derby/impl/jdbc/ engine/org/apache/derby/loc/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derb...
Date Wed, 29 Apr 2009 21:40:55 GMT
Author: kmarsden
Date: Wed Apr 29 21:40:54 2009
New Revision: 769948

URL: http://svn.apache.org/viewvc?rev=769948&view=rev
Log:
DERBY-2769 Implement error handling/parameter checking in Clob.setString

Contributed by Yun Lee (yun dot lee dot bj at gmail dot com)


Modified:
    db/derby/code/trunk/java/build/org/apache/derbyBuild/splitmessages.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClobTest.java

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/splitmessages.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/splitmessages.java?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/splitmessages.java (original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/splitmessages.java Wed Apr 29 21:40:54
2009
@@ -119,6 +119,7 @@
         clientMessageIds.add(SQLState.LANG_STRING_TOO_LONG);
         clientMessageIds.add(SQLState.INVALID_COLUMN_ARRAY_LENGTH);
         clientMessageIds.add(SQLState.PROPERTY_INVALID_VALUE);
+        clientMessageIds.add(SQLState.LANG_SUBSTR_START_ADDING_LEN_OUT_OF_RANGE);
     }
 
 	public static void main(String[] args) throws Exception {

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Clob.java Wed Apr 29 21:40:54
2009
@@ -21,14 +21,13 @@
 
 package org.apache.derby.client.am;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.InputStream;
+
 import java.io.IOException;
 import java.io.Reader;
 import java.sql.SQLException;
-import org.apache.derby.shared.common.reference.SQLState;
+
 import org.apache.derby.client.net.EncodedInputStream;
+import org.apache.derby.shared.common.reference.SQLState;
 
 public class Clob extends Lob implements java.sql.Clob {
     //---------------------navigational members-----------------------------------
@@ -625,7 +624,18 @@
                 new ClientMessageId(SQLState.BLOB_POSITION_TOO_LARGE),
                 new Long(pos));
         }
-        if ((offset < 0) || offset > str.length() ) {
+        
+        if (str == null) {
+            throw new SqlException(agent_.logWriter_,
+                    new ClientMessageId(
+                            SQLState.BLOB_NULL_PATTERN_OR_SEARCH_STR));
+        }
+        
+        if (str.length() == 0) {
+            return 0;
+        }
+        
+        if ((offset < 0) || offset >= str.length() ) {
             throw new SqlException(agent_.logWriter_,
                 new ClientMessageId(SQLState.BLOB_INVALID_OFFSET),
                 new Integer(offset));
@@ -636,6 +646,13 @@
                 new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH),
                 new Integer(len));
         }
+        
+        if (offset + len > str.length()) {
+            throw new SqlException(agent_.logWriter_,
+                    new ClientMessageId(
+                            SQLState.LANG_SUBSTR_START_ADDING_LEN_OUT_OF_RANGE),
+                    new Integer(offset), new Integer(len), str);
+        }
 
         if (len == 0) {
             return 0;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java Wed Apr 29 21:40:54
2009
@@ -546,9 +546,40 @@
     public int setString(long pos, String str, int offset, int len)
             throws SQLException {
         checkValidity();
-        if (pos < 1)
+        if (pos < 1) {
             throw Util.generateCsSQLException(
                 SQLState.BLOB_BAD_POSITION, new Long(pos));
+        }
+        
+        if (pos > length() + 1) {
+            throw Util.generateCsSQLException(
+        	    SQLState.BLOB_POSITION_TOO_LARGE);
+        }
+        
+        if (str == null) {
+            throw Util.generateCsSQLException(
+        	    SQLState.BLOB_NULL_PATTERN_OR_SEARCH_STR);
+        }
+        
+        if (str.length() == 0) {
+            return 0;
+        }
+        
+        if (offset < 0 || offset >= str.length()) {
+            throw Util.generateCsSQLException(SQLState.BLOB_INVALID_OFFSET);
+        }
+        
+        if (len < 0) {
+            throw Util.generateCsSQLException(
+        	    SQLState.BLOB_NONPOSITIVE_LENGTH);
+        }
+        
+        if (len + offset > str.length()) {
+            throw Util.generateCsSQLException(
+                    SQLState.LANG_SUBSTR_START_ADDING_LEN_OUT_OF_RANGE,
+                    new Integer(offset), new Integer(len), str);
+        }
+        
         try {
             if (!this.clob.isWritable()) {
                 makeWritableClobClone();

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Wed Apr 29 21:40:54
2009
@@ -674,6 +674,14 @@
             </msg>
 
             <msg>
+                <name>22011.S.1</name>
+                <text>The range specified for the substring with offset {0} and len
{1} is out of range for the String: {2}.</text>
+                <arg>offset</arg>
+                <arg>len</arg>
+                <arg>str</arg>
+            </msg>
+            
+            <msg>
                 <name>22012</name>
                 <text>Attempt to divide by zero.</text>
             </msg>

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
Wed Apr 29 21:40:54 2009
@@ -699,6 +699,7 @@
 	String LANG_DATE_SYNTAX_EXCEPTION                                  = "22007.S.181";
     String LANG_INVALID_FUNCTION_ARGUMENT                              = "22008.S";
 	String LANG_SUBSTR_START_OR_LEN_OUT_OF_RANGE                        = "22011";
+	String LANG_SUBSTR_START_ADDING_LEN_OUT_OF_RANGE                        = "22011.S.1";
 	String LANG_DIVIDE_BY_ZERO                                         = "22012";
     String LANG_SQRT_OF_NEG_NUMBER                                     = "22013";
     String LANG_INVALID_PARAMETER_FOR_SEARCH_POSITION                  = "22014";

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClobTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClobTest.java?rev=769948&r1=769947&r2=769948&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClobTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClobTest.java
Wed Apr 29 21:40:54 2009
@@ -385,6 +385,108 @@
         //insertDataWithToken(token, prefix, 2*1024-7, SET_CHARACTER_STREAM);
         executeTestPositionWithStringToken(token, prefix);
     }
+    
+    /**
+     * Test setString() refuses wrong offset. 
+     */
+    public void testSetStringOnWrongOffset() throws SQLException {
+        clob.setString(1, "TEST");
+        long upperLimit = clob.length() + 1;
+        String str = "AGAIN";
+        
+        try {
+            clob.setString(upperLimit, str, -1, 2);
+            fail("setString() refuses negative offset!");
+        } catch (SQLException e) {
+            assertSQLState("XJ078", e);
+        }
+        
+        try {
+            clob.setString(upperLimit, str, str.length() + 1, 1);
+            fail("setString() refuses offset greater than str.length()!");
+        } catch (SQLException e) {
+            assertSQLState("XJ078", e);
+        }
+        
+        //if (offset + len) == str.length(), it's accepted.
+        clob.setString(upperLimit, str, str.length() - 1, 1);
+        
+        try {
+            clob.setString(upperLimit, str, str.length(), 0);
+            fail("offset should be smaller than the length of str");
+        } catch (SQLException e) {
+            assertSQLState("XJ078", e);
+        }
+        
+        try {
+            clob.setString(upperLimit, str, str.length() - 1, 2);
+            fail("setString() refuses offset + len > str.length()!");
+        } catch (SQLException e) {
+            assertSQLState("22011", e);
+        }
+    }
+    
+    /**
+     * Test setString() refuses wrong len. 
+     */
+    public void testSetStringWithWrongLen() throws SQLException {
+        clob.setString(1, "TEST");
+        long upperLimit = clob.length() + 1;
+        String str = "AGAIN";
+        
+        try {
+            clob.setString(upperLimit, str, 0, -1);
+            fail("setString() refuses negative len!");
+        } catch (SQLException e) {
+            assertSQLState("XJ071", e);
+        }
+        
+        try {
+            clob.setString(upperLimit, str, 0, str.length() + 1);
+            fail("setString() refuses wrong len out of range!");
+        } catch (SQLException e) {
+            assertSQLState("22011", e);
+        }
+    }
+    
+    /**
+     * Test setString() refuses pos bigger than clob.length() + 1.
+     */
+    public void testSetStringWithBigPos() throws SQLException {
+        clob.setString(1, "TEST");
+        long upperLimit = clob.length() + 1;
+        
+        try {
+            clob.setString(upperLimit + 1, "AGAIN", 0, 2);
+            fail("pos is out of range!");
+        } catch (SQLException e) {
+            assertSQLState("XJ076", e);
+        }
+    }
+    
+    /**
+     * Test setStrinng() refuses a Null String.
+     */
+    public void testSetStringWithNull() throws SQLException {
+        clob.setString(1, "TEST");
+        long upperLimit = clob.length() + 1;
+        
+        try {
+            clob.setString(upperLimit, null, 0, 2);
+            fail("can not accepted null String!");
+        } catch (SQLException e) {
+            assertSQLState("XJ072", e);
+        }
+    }
+    
+    /**
+     * Test setString() accepts a empty String, and just return 0.
+     */
+    public void testSetStringWithEmptyString() throws SQLException {
+        clob.setString(1, "TEST");
+        long upperLimit = clob.length() + 1;              
+        assertEquals(0, clob.setString(upperLimit, "", 0, 0));           
+    }
 
     /**
      * Truncating a Clob to the empty string.



Mime
View raw message