You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2009/04/17 23:50:52 UTC

svn commit: r766163 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/classfile/ engine/org/apache/derby/iapi/services/io/ engine/org/apache/derby/impl/store/raw/data/ testing/org/apache/derbyTesting/unitTests/junit/

Author: kahatlen
Date: Fri Apr 17 21:50:52 2009
New Revision: 766163

URL: http://svn.apache.org/viewvc?rev=766163&view=rev
Log:
DERBY-3941: Unsafe use of DataInput.skipBytes()

Replaced calls to DataInput.skipBytes() with new utility method
DataInputUtil.skipFully().

Patch contributed by Yun Lee.

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java   (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java   (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/classfile/ClassInvestigator.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredFieldHeader.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredPage.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/classfile/ClassInvestigator.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/classfile/ClassInvestigator.java?rev=766163&r1=766162&r2=766163&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/classfile/ClassInvestigator.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/classfile/ClassInvestigator.java Fri Apr 17 21:50:52 2009
@@ -22,20 +22,15 @@
 package org.apache.derby.iapi.services.classfile;
 
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.util.Collections;
 import java.util.Enumeration;
-
-import java.io.IOException;
-import java.util.Vector;
-
-import org.apache.derby.iapi.services.classfile.VMDescriptor;
-import org.apache.derby.iapi.services.classfile.VMDescriptor;
 import java.util.HashSet;
-
 import java.util.Hashtable;
 import java.util.Vector;
-import java.util.Enumeration;
-import java.util.Collections;
+
+import org.apache.derby.iapi.services.io.DataInputUtil;
 
 
 /** 
@@ -305,13 +300,12 @@
 
 		ClassInput ci = new ClassInput(new java.io.ByteArrayInputStream(ae.infoIn));
 
-
-		ci.skipBytes(4); // puts us at code_length
+		DataInputUtil.skipFully(ci, 4);// puts us at code_length
 		int len = ci.getU4();
-		ci.skipBytes(len); // puts us at exception_table_length
+		DataInputUtil.skipFully(ci, len);// puts us at exception_table_length
 		int count = ci.getU2();
 		if (count != 0)
-			ci.skipBytes(8 * count);
+			DataInputUtil.skipFully(ci, 8 * count);
 
 		int nonAttrLength = 4 + 4 + len + 2 + (8 * count);
 
@@ -332,7 +326,7 @@
 				System.err.println("ERROR - Unknown code attribute " + name);
 
 			len = ci.getU4();
-			ci.skipBytes(len);
+			DataInputUtil.skipFully(ci, len);
 		}
 
 		if (newCount != 0) {

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java?rev=766163&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java Fri Apr 17 21:50:52 2009
@@ -0,0 +1,61 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.services.io.DataInputUtil
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derby.iapi.services.io;
+
+import java.io.DataInput;
+import java.io.IOException;
+
+/**
+ * A util class for DataInput.
+ */
+public final class DataInputUtil {
+
+    /**
+     * Skips requested number of bytes,
+     * throws EOFException if there is too few bytes in the DataInput.
+     * @param in
+     *      DataInput to be skipped.
+     * @param skippedBytes
+     *      number of bytes to skip. if skippedBytes <= zero, do nothing.
+     * @throws EOFException
+     *      if EOF meets before requested number of bytes are skipped.
+     * @throws IOException
+     *      if IOException occurs. It doesn't contain EOFException.
+     * @throws NullPointerException
+     *      if the param 'in' equals null.
+     */
+    public static void skipFully(DataInput in, int skippedBytes)
+    throws IOException {
+        if (in == null) {
+            throw new NullPointerException();
+        }
+
+        while (skippedBytes > 0) {
+            int skipped = in.skipBytes(skippedBytes);
+            if (skipped == 0) {
+                in.readByte();
+                skipped++;
+            }
+            skippedBytes -= skipped;
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/DataInputUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredFieldHeader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredFieldHeader.java?rev=766163&r1=766162&r2=766163&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredFieldHeader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredFieldHeader.java Fri Apr 17 21:50:52 2009
@@ -20,19 +20,17 @@
  */
 package org.apache.derby.impl.store.raw.data;
 
-import org.apache.derby.iapi.store.raw.RecordHandle;
-import org.apache.derby.iapi.services.sanity.SanityManager;
-
-import java.io.IOException;
 import java.io.EOFException;
-
+import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.OutputStream;
 
+import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.services.io.ArrayInputStream;
 import org.apache.derby.iapi.services.io.CompressedNumber;
+import org.apache.derby.iapi.services.io.DataInputUtil;
+import org.apache.derby.iapi.services.sanity.SanityManager;
 
-import java.io.InputStream;
 
 /**
     A class to provide static methods to manipulate fields in the field header.
@@ -685,7 +683,7 @@
                     CompressedNumber.sizeInt(fieldDataLength);
 
                 if (diffLen != 0)
-                    in.skipBytes(diffLen);
+                    DataInputUtil.skipFully(in, diffLen);
             } 
 
             return(fieldDataLength);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredPage.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredPage.java?rev=766163&r1=766162&r2=766163&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredPage.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/StoredPage.java Fri Apr 17 21:50:52 2009
@@ -36,6 +36,7 @@
 import org.apache.derby.iapi.services.io.ArrayInputStream;
 import org.apache.derby.iapi.services.io.ArrayOutputStream;
 import org.apache.derby.iapi.services.io.CompressedNumber;
+import org.apache.derby.iapi.services.io.DataInputUtil;
 import org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream;
 import org.apache.derby.iapi.services.io.ErrorObjectInput;
 import org.apache.derby.iapi.services.io.FormatIdInputStream;
@@ -4662,7 +4663,7 @@
                                 inUserCode = null;
                                 int unread = lrdi.clearLimit();
                                 if (unread != 0)
-                                    lrdi.skipBytes(unread);
+                                    DataInputUtil.skipFully(lrdi, unread);
                             }
                             else
                             {
@@ -4711,7 +4712,7 @@
                         inUserCode = null;
                         int unread = lrdi.clearLimit();
                         if (unread != 0)
-                            lrdi.skipBytes(unread);
+                            DataInputUtil.skipFully(lrdi, unread);
                     }
 
                 }
@@ -5258,7 +5259,7 @@
 						inUserCode = null;
 						int unread = dataIn.clearLimit();
 						if (unread != 0)
-							dataIn.skipBytes(unread);
+							DataInputUtil.skipFully(dataIn, unread);
 					}
                     else
                     {
@@ -5315,7 +5316,7 @@
 				inUserCode = null;
 				int unread = dataIn.clearLimit();
 				if (unread != 0)
-					dataIn.skipBytes(unread);
+					DataInputUtil.skipFully(dataIn, unread);
 
 				continue;
 			}
@@ -5561,7 +5562,7 @@
                                     inUserCode = null;
                                     int unread = dataIn.clearLimit();
                                     if (unread != 0)
-                                        dataIn.skipBytes(unread);
+                                        DataInputUtil.skipFully(dataIn, unread);
                                 }
                                 else
                                 {
@@ -5626,7 +5627,7 @@
                             inUserCode = null;
                             int unread = dataIn.clearLimit();
                             if (unread != 0)
-                                dataIn.skipBytes(unread);
+                                DataInputUtil.skipFully(dataIn, unread);
                         }
                     }
                     else
@@ -7711,7 +7712,7 @@
 		int fieldDataLength = StoredFieldHeader.readFieldDataLength(in, fieldStatus, slotFieldSize);
 
 		if (fieldDataLength != 0) {
-			in.skipBytes(fieldDataLength);
+			DataInputUtil.skipFully(in, fieldDataLength);
 		}
 	}
 

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java?rev=766163&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java Fri Apr 17 21:50:52 2009
@@ -0,0 +1,75 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.unitTests.junit.DataInputUtilTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+package org.apache.derbyTesting.unitTests.junit;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.EOFException;
+import java.io.IOException;
+
+import org.apache.derby.iapi.services.io.DataInputUtil;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test case for DataInputUtil.
+ */
+public class DataInputUtilTest extends TestCase {
+
+    public DataInputUtilTest(String name) {
+        super(name);
+    }
+
+    public void testSkipFully() throws IOException{
+        int length = 1024;
+
+        DataInput di = new DataInputStream(
+                new ByteArrayInputStream(new byte[length]));
+        DataInputUtil.skipFully(di, length);
+        try {
+            di.readByte();
+            fail("Should have met EOF!");
+        } catch (EOFException e) {
+            assertTrue(true);
+        }
+
+        di = new DataInputStream(
+                new ByteArrayInputStream(new byte[length]));
+        DataInputUtil.skipFully(di, length - 1);
+        di.readByte();
+        try {
+            di.readByte();
+            fail("Should have met EOF!");
+        } catch (EOFException e) {
+            assertTrue(true);
+        }
+    }
+
+    /**
+     * Returns a suite of tests.
+     */
+    public static Test suite() {
+        return new TestSuite(DataInputUtilTest.class, "DataInputUtilTest tests");
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/DataInputUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java?rev=766163&r1=766162&r2=766163&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java Fri Apr 17 21:50:52 2009
@@ -58,6 +58,7 @@
         suite.addTest(PathUtilTest.suite());
         suite.addTest(VirtualFileTest.suite());
         suite.addTest(ReaderToUTF8StreamTest.suite());
+        suite.addTest(DataInputUtilTest.suite());
 
         return suite;
     }