JDK-4387368 : Invalid serialPersistentFields field name causes NullPointerException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2000-11-08
  • Updated: 2001-07-26
  • Resolved: 2001-03-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Description

Name: krC82822			Date: 11/08/2000


8 Nov 2000, eval1127@eng -- see also # 4337857, 4334265, 4065313
------------------------
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

I believe the following invalid ObjectStreamField definition should
result in the program failing with a WriteAbortedException or similar
exception rather than crashing with a NullPointerException.

import java.io.*;

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

    public String toString()
    {
        return "{i="+i+"}";
    }

    public static void main(String[] args) throws Exception
    {
        X x = new X();
        FileOutputStream fos = new FileOutputStream("x.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(x);
        oos.close();
        System.out.println("Wrote: "+x);
        System.exit(0);
    }
}

bash-2.04$ java -cp . X
Exception in thread "main" java.lang.NullPointerException
        at java.io.ObjectOutputStream.getPrimitiveFieldValues(Native Method)
        at java.io.ObjectOutputStream.outputClassFields
(ObjectOutputStream.java:1826)
        at java.io.ObjectOutputStream.defaultWriteObject
(ObjectOutputStream.java:480)
        at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:1214)

        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:366)
        at X.main(X.java:20)
bash-2.04$
(Review ID: 111078) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin FIXED IN: merlin-beta INTEGRATED IN: merlin-beta VERIFIED IN: merlin-beta2
2004-06-14

WORK AROUND Name: krC82822 Date: 11/08/2000 Don't screw up the serialPersistentFields definition ======================================================================
2004-06-11

EVALUATION This is a bug; as stated in the description, some exception other than NullPointerException should be thrown (most likely an IOException subclass). Note that the NullPointerException does not occur if the PutField API is used to set the outgoing field values, which is the intended usage in cases where serialPersistentFields is used to define serializable fields that don't exist in the class. michael.warres@east 2000-11-08 ObjectOutputStream.writeObject() now throws an InvalidClassException if an attempt is made to write an object via default serialization which defines "unmatched" serializable fields. michael.warres@east 2001-03-21
2001-03-21