classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] RFC: Patch for duplicate entries in serialPersistentFields


From: Guilhem Lavaux
Subject: [cp-patches] RFC: Patch for duplicate entries in serialPersistentFields
Date: Sat, 10 Dec 2005 20:16:59 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)

Hi,

Here is a patch which (partially) fix a regression in kaffe. If you define a class like that:

class A
{
private static final ObjectStreamField[] serialPersistentFields =
                {
                        new ObjectStreamField("i", int.class),
                        new ObjectStreamField("i", int.class)
                };

/* ... */
};

then the JDK throws an InvalidClassException in ObjectStreamClass.lookup.
This goes against the API. However it is true that we must take into account the presence of duplicate fields. So I propose we throw an InvalidClassException on writing the object if this sort of situation happens.

I have written the attached patch for this.

Regards,

Guilhem.

ChangeLog entry:

2005-12-10  Guilhem Lavaux  <address@hidden>

        * java/io/ObjectOutputStream.java
        (writeClassDescription): Throw an InvalidClassException if
        fields is INVALID_FIELDS.

        * java/io/ObjectStreamClass.java
        (setFields): Make fields as INVALID if we detect duplicate      
        entries in serialPersistentFields.

Index: java/io/ObjectOutputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectOutputStream.java,v
retrieving revision 1.63
diff -u -r1.63 ObjectOutputStream.java
--- java/io/ObjectOutputStream.java     1 Nov 2005 14:54:23 -0000       1.63
+++ java/io/ObjectOutputStream.java     10 Dec 2005 18:45:02 -0000
@@ -442,6 +442,10 @@
         realOutput.writeByte(flags);
 
         ObjectStreamField[] fields = osc.fields;
+
+       if (fields == ObjectStreamClass.INVALID_FIELDS)
+         throw new InvalidClassException("serialPersistentFields in class " + 
osc.getName() + " is invalid");
+
         realOutput.writeShort(fields.length);
 
         ObjectStreamField field;
Index: java/io/ObjectStreamClass.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectStreamClass.java,v
retrieving revision 1.43
diff -u -r1.43 ObjectStreamClass.java
--- java/io/ObjectStreamClass.java      16 Sep 2005 17:24:04 -0000      1.43
+++ java/io/ObjectStreamClass.java      10 Dec 2005 18:45:02 -0000
@@ -63,6 +63,8 @@
 
 public class ObjectStreamClass implements Serializable
 {
+  static final ObjectStreamField[] INVALID_FIELDS = new ObjectStreamField[0];
+
   /**
    * Returns the <code>ObjectStreamClass</code> for <code>cl</code>.
    * If <code>cl</code> is null, or is not <code>Serializable</code>,
@@ -608,6 +610,28 @@
            fields = getSerialPersistentFields(cl);
            if (fields != null)
              {
+               ObjectStreamField[] fieldsName = new 
ObjectStreamField[fields.length];
+               System.arraycopy(fields, 0, fieldsName, 0, fields.length);
+
+               Arrays.sort (fieldsName, new Comparator() {
+                       public int compare(Object o1, Object o2)
+                       {
+                         ObjectStreamField f1 = (ObjectStreamField)o1;
+                         ObjectStreamField f2 = (ObjectStreamField)o2;
+                           
+                         return f1.getName().compareTo(f2.getName());
+                       }
+                   });
+               
+               for (int i=1; i < fields.length; i++)
+                 {
+                   if 
(fieldsName[i-1].getName().equals(fieldsName[i].getName()))
+                       {
+                           fields = INVALID_FIELDS;
+                           return;
+                       }
+                 }
+
                Arrays.sort (fields);
                // Retrieve field reference.
                for (int i=0; i < fields.length; i++)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]