United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4387368 : Invalid serialPersistentFields field name causes NullPointerException

Details
Type:
Bug
Submit Date:
2000-11-08
Status:
Closed
Updated Date:
2001-07-26
Project Name:
JDK
Resolved Date:
2001-03-22
Component:
core-libs
OS:
generic
Sub-Component:
java.io:serialization
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.3.0
Fixed Versions:
1.4.0 (beta)

Related Reports

Sub Tasks

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
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
WORK AROUND



Name: krC82822			Date: 11/08/2000


Don't screw up the serialPersistentFields definition
======================================================================
                                     
2004-06-11
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



Hardware and Software, Engineered to Work Together