harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r929185 - in /harmony/enhanced/trunk/classlib/modules/luni/src: main/java/java/io/ObjectInputStream.java test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java
Date Tue, 30 Mar 2010 16:44:14 GMT
Author: hindessm
Date: Tue Mar 30 16:44:14 2010
New Revision: 929185

URL: http://svn.apache.org/viewvc?rev=929185&view=rev
Log:
When we assign a value to a field, we need to check that it is compatible
with the field of the instantiated object.  The old implementation checked
compatibility with the field defined in the stream being read.

Modified:
    harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/io/ObjectInputStream.java
    harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java

Modified: harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/io/ObjectInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/io/ObjectInputStream.java?rev=929185&r1=929184&r2=929185&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/io/ObjectInputStream.java
(original)
+++ harmony/enhanced/trunk/classlib/modules/luni/src/main/java/java/io/ObjectInputStream.java
Tue Mar 30 16:44:14 2010
@@ -1291,21 +1291,23 @@ public class ObjectInputStream extends I
                 }
                 if (fieldDesc != null) {
                     if (toSet != null) {
-                        Class<?> fieldType = fieldDesc.getType();
+                        Class<?> fieldType = getFieldClass(obj, fieldName);
                         Class<?> valueType = toSet.getClass();
-                        if (!fieldType.isAssignableFrom(valueType)) {
-                            throw new ClassCastException(Messages.getString(
+                        if (fieldType != null) {
+                            if (!fieldType.isAssignableFrom(valueType)) {
+                                throw new ClassCastException(Messages.getString(
                                     "luni.C0", new String[] { //$NON-NLS-1$
                                     fieldType.toString(), valueType.toString(),
                                             classDesc.getName() + "." //$NON-NLS-1$
                                                     + fieldName }));
-                        }
-                        try {
-                            if (fieldID != ObjectStreamField.FIELD_IS_ABSENT) { 
-                                accessor.setObject(obj, fieldID, toSet);
                             }
-                        } catch (NoSuchFieldError e) {
-                            // Ignored
+                            try {
+                                if (fieldID != ObjectStreamField.FIELD_IS_ABSENT) { 
+                                    accessor.setObject(obj, fieldID, toSet);
+                                }
+                            } catch (NoSuchFieldError e) {
+                                // Ignored
+                            }
                         }
                     }
                 }
@@ -1313,6 +1315,26 @@ public class ObjectInputStream extends I
         }
     }
 
+    private static Class<?> getFieldClass(final Object obj,
+                                          final String fieldName) {
+        return AccessController.doPrivileged(new PrivilegedAction<Class<?>>()
{
+                public Class<?> run() {
+                    Class<?> objClass = obj.getClass();
+                    while (objClass != null) {
+                        try {
+                            Class<?> fc =
+                                objClass.getDeclaredField(fieldName).getType();
+                            return fc;
+                        } catch (NoSuchFieldException e) {
+                            // Ignored
+                        }
+                        objClass = objClass.getSuperclass();
+                    }
+                    return null;
+                }
+            });
+    }
+
     /**
      * Reads a float (32 bit) from the source stream.
      * 

Modified: harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java?rev=929185&r1=929184&r2=929185&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java
(original)
+++ harmony/enhanced/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java
Tue Mar 30 16:44:14 2010
@@ -35,6 +35,7 @@ import java.io.ObjectInputValidation;
 import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
+import java.io.ObjectStreamException;
 import java.io.OutputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
@@ -1140,6 +1141,27 @@ public class ObjectInputStreamTest exten
         oin.readObject();
     }
 
+    public void test_readObject_replacedClassField() throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(out);
+        FieldReplacementTestClass obj = new FieldReplacementTestClass(1234);
+        oos.writeObject(obj);
+        out.flush();
+        out.close();
+
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 
+        ObjectInputStream ois = new ObjectInputStream(in);
+
+        try {
+            FieldReplacementTestClass result =
+                (FieldReplacementTestClass)ois.readObject();
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+        ois.close();
+    }
+
     /**
      * Sets up the fixture, for example, open a network connection. This method
      * is called before a test is executed.
@@ -1149,6 +1171,32 @@ public class ObjectInputStreamTest exten
         super.setUp();
         oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
     }
+
+    public static class FieldReplacementTestClass implements Serializable {
+        private FieldClass c;
+        public FieldReplacementTestClass(int i) {
+            super();
+            c = new FieldClass(i);
+        }
+    }
+    public static class FieldClass implements Serializable {
+        private int i;
+        public FieldClass(int i) {
+            super();
+            this.i = i;
+        }
+        protected Object writeReplace() throws ObjectStreamException {
+            return new ReplacementFieldClass(i);
+        }
+    }
+    public static class ReplacementFieldClass implements Serializable {
+        private int i;
+        public ReplacementFieldClass(int i) {
+            super();
+            this.i = i;
+        }
+    }
+
 }
 
 class TestArray implements Serializable



Mime
View raw message