You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by mm...@apache.org on 2007/12/05 20:48:26 UTC
svn commit: r601490 - in /harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/io/ObjectOutputStream.java
main/java/java/io/ObjectStreamClass.java
test/api/common/tests/api/java/io/ObjectInputStreamTest.java
Author: mmarkov
Date: Wed Dec 5 11:48:26 2007
New Revision: 601490
URL: http://svn.apache.org/viewvc?rev=601490&view=rev
Log:
Applied patch for HARMONY-4996: [classlib][luni] Incorrect deserialization of Externalizable classes
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/io/ObjectInputStreamTest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java?rev=601490&r1=601489&r2=601490&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java Wed Dec 5 11:48:26 2007
@@ -38,6 +38,11 @@
ObjectStreamConstants {
/*
+ * Mask to zero SC_BLOC_DATA bit.
+ */
+ private static final byte NOT_SC_BLOCK_DATA = (byte) (SC_BLOCK_DATA ^ 0xFF);
+
+ /*
* How many nested levels to writeObject. We may not need this.
*/
private int nestedLevels;
@@ -1531,12 +1536,13 @@
boolean externalizable = false;
externalizable = ObjectStreamClass.isExternalizable(classDesc
.forClass());
- if (protocolVersion != PROTOCOL_VERSION_1) {
- // Change for 1.2. Objects can be saved in old format
- // (PROTOCOL_VERSION_1) or in the 1.2 format (PROTOCOL_VERSION_2).
- // Nested "if" check to optimize checking. Second check is more
- // expensive.
- if (externalizable) {
+
+ if (externalizable) {
+ if (protocolVersion == PROTOCOL_VERSION_1) {
+ flags &= NOT_SC_BLOCK_DATA;
+ } else {
+ // Change for 1.2. Objects can be saved in old format
+ // (PROTOCOL_VERSION_1) or in the 1.2 format (PROTOCOL_VERSION_2).
flags |= SC_BLOCK_DATA;
}
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java?rev=601490&r1=601489&r2=601490&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java Wed Dec 5 11:48:26 2007
@@ -267,6 +267,7 @@
boolean externalizable = isExternalizable(cl);
if (externalizable) {
flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
+ flags |= ObjectStreamConstants.SC_BLOCK_DATA; // use protocol version 2 by default
} else if (serializable) {
flags |= ObjectStreamConstants.SC_SERIALIZABLE;
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/io/ObjectInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/io/ObjectInputStreamTest.java?rev=601490&r1=601489&r2=601490&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/io/ObjectInputStreamTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/io/ObjectInputStreamTest.java Wed Dec 5 11:48:26 2007
@@ -20,6 +20,7 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -27,8 +28,10 @@
import java.io.InputStream;
import java.io.InvalidObjectException;
import java.io.NotActiveException;
+import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectInputValidation;
+import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
@@ -1034,6 +1037,57 @@
testArray = (TestArray) oin.readObject();
Integer[] integers = new Integer[] { 10, 20 };
assertTrue(java.util.Arrays.equals(integers, testArray.array));
+ }
+
+ public static class TestExtObject implements Externalizable {
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeInt(10);
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ in.readInt();
+ }
+ }
+
+ static class TestObjectOutputStream extends ObjectOutputStream {
+ private ObjectStreamClass[] objs;
+ private int pos = 0;
+
+ public TestObjectOutputStream(OutputStream out, ObjectStreamClass[] objs) throws IOException {
+ super(out);
+ this.objs = objs;
+ }
+
+ protected void writeClassDescriptor(ObjectStreamClass osc) throws IOException {
+ objs[pos++] = osc; }
+ }
+
+ static class TestObjectInputStream extends ObjectInputStream {
+ private ObjectStreamClass[] objs;
+ private int pos = 0;
+
+ public TestObjectInputStream(InputStream in, ObjectStreamClass[] objs) throws IOException {
+ super(in);
+ this.objs = objs;
+ }
+
+ protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
+ return (ObjectStreamClass) objs[pos++];
+ }
+ }
+
+ // Regression test for HARMONY-4996
+ public void test_readObject_replacedClassDescriptor() throws Exception {
+ ObjectStreamClass[] objs = new ObjectStreamClass[1000];
+ PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pin = new PipedInputStream(pout);
+ ObjectOutputStream oout = new TestObjectOutputStream(pout, objs);
+ oout.writeObject(new TestExtObject());
+ oout.writeObject("test");
+ oout.close();
+ ObjectInputStream oin = new TestObjectInputStream(pin, objs);
+ oin.readObject();
+ oin.readObject();
}
/**