harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lian...@apache.org
Subject svn commit: r480811 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ObjectInputStream.java test/java/tests/api/java/io/ObjectInputStreamTest.java
Date Thu, 30 Nov 2006 02:47:40 GMT
Author: liangyx
Date: Wed Nov 29 18:47:39 2006
New Revision: 480811

URL: http://svn.apache.org/viewvc?view=rev&rev=480811
Log:
Apply patch for HARMONY-2340 ([classlib][luni]ObjectInputStream.readClassDesc throws NullPointerException
when it is called outside of readObject())

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

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java?view=diff&rev=480811&r1=480810&r2=480811
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
Wed Nov 29 18:47:39 2006
@@ -721,7 +721,7 @@
                 streamClass.setLoadFields(new ObjectStreamField[0]);
                 registerObjectRead(streamClass, new Integer(nextHandle()),
                         false);
-                streamClass.setSuperclass(readClassDesc());
+                checkedSetSuperClassDesc(streamClass, readClassDesc());                
                 return streamClass;
             case TC_REFERENCE:
                 return (ObjectStreamClass) readCyclicReference();
@@ -1591,7 +1591,7 @@
         // Consume unread class annotation data and TC_ENDBLOCKDATA
         discardData();
         ObjectStreamClass superClass = readClassDesc();
-        classDesc.setSuperclass(superClass);
+        checkedSetSuperClassDesc(classDesc, superClass);         
         // Check SUIDs, note all SUID for Enum is 0L
         if (0L != classDesc.getSerialVersionUID()
                 || 0L != superClass.getSerialVersionUID()) {
@@ -1696,8 +1696,7 @@
 
         // Consume unread class annotation data and TC_ENDBLOCKDATA
         discardData();
-        newClassDesc.setSuperclass(readClassDesc());
-
+        checkedSetSuperClassDesc(newClassDesc, readClassDesc());      
         return newClassDesc;
     }
 
@@ -1749,6 +1748,8 @@
 
         // We must register the class descriptor before reading field
         // descriptors.
+        //if called outside of readObject, the descriptorHandle might be null
+        descriptorHandle = (null == descriptorHandle? new Integer(nextHandle()):descriptorHandle);
         registerObjectRead(newClassDesc, descriptorHandle, false);
         descriptorHandle = null;
 
@@ -2739,4 +2740,13 @@
             return fullName.substring(k + 1);
         }        
     }
+    
+    //Avoid recursive defining.
+    private static void checkedSetSuperClassDesc(ObjectStreamClass desc,
+			ObjectStreamClass superDesc) throws StreamCorruptedException {
+		if (desc.equals(superDesc)) {
+			throw new StreamCorruptedException();
+		}
+		desc.setSuperclass(superDesc);
+	}
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectInputStreamTest.java?view=diff&rev=480811&r1=480810&r2=480811
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectInputStreamTest.java
Wed Nov 29 18:47:39 2006
@@ -688,6 +688,7 @@
     }
     
     static class TestClassForSerialization implements Serializable {
+		private static final long serialVersionUID = 1L;
 	}
 
     public void test_ClassDescriptor() throws IOException,
@@ -705,6 +706,55 @@
 		Object obj = ois.readObject();
 		ois.close();
 		assertEquals(cls, obj);
+	}
+        
+
+	// Regression Test for JIRA-2340
+    public static class ObjectOutputStreamWithWriteDesc1 extends
+			ObjectOutputStream {
+		public ObjectOutputStreamWithWriteDesc1(OutputStream os)
+				throws IOException {
+			super(os);
+		}
+
+		public void writeClassDescriptor(ObjectStreamClass desc)
+				throws IOException {
+			super.writeClassDescriptor(desc);
+		}
+	}
+
+	public static class ObjectIutputStreamWithReadDesc1 extends
+			ObjectInputStream {		
+
+		public ObjectIutputStreamWithReadDesc1(InputStream is)
+				throws IOException {
+			super(is);			
+		}
+
+		public ObjectStreamClass readClassDescriptor() throws IOException,
+				ClassNotFoundException {
+			return super.readClassDescriptor();
+
+		}
+	}
+	
+	public void test_readClassDescriptor() throws IOException,
+			ClassNotFoundException {
+
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		ObjectOutputStreamWithWriteDesc1 oos = new ObjectOutputStreamWithWriteDesc1(
+				baos);
+		ObjectStreamClass desc = ObjectStreamClass
+		.lookup(TestClassForSerialization.class);
+		oos.writeClassDescriptor(desc);
+		oos.close();
+		
+		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+		ObjectIutputStreamWithReadDesc1 ois = new ObjectIutputStreamWithReadDesc1(
+				bais);
+		Object obj = ois.readClassDescriptor();
+		ois.close();
+		assertEquals(desc.getClass(), obj.getClass());
 	}
    
 



Mime
View raw message