chpooo / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Kryo#writeObjectOrNull() is broken #67

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In both the current trunk and v2.09 the method Kryo#writeObjectOrNull(Output, 
Object) does not accept null objects.

Here are the first few lines of that method:

public void writeObjectOrNull (Output output, Object object) {
    if (output == null) throw new IllegalArgumentException("output cannot be null.");
    depth++;
    try {
        Serializer serializer = getRegistration(object.getClass()).getSerializer();

The issue appears to be that object.getClass() is being called before checking 
if object is null, so a null pointer exception is being thrown from the last 
line shown above.

My current workaround is to use the 3 argument version of the method which 
accepts a serializer and doesn't do the getClass() call on object.

Should be an easy fix, thanks!

Original issue reported on code.google.com by pbak...@groupon.com on 30 May 2012 at 11:21

GoogleCodeExporter commented 8 years ago
Gah that is terrible. Easy fix... ha! :p

Passing null to writeObjectOrNull(Output, Object) means the type of object has 
been lost. Without the type, the method itself has to handle serializing null, 
it can't delegate this to the serializer, which would be more efficient. This 
is unfortunate because you would only call writeObjectOrNull when you really do 
know the type, and also we already know the type in readObjectOrNull.

There are 2 solutions:

1) The method could be changed to writeObjectOrNull(Output, Object, Class) so 
that the type is not lost. This method signature is a little ugly, but then 
writeObjectOrNull is not used very often (which is obvious by the simple fact 
that this issue exists!).

2) writeObjectOrNull and readObjectOrNull could be changed to handle nulls 
without making use of the serializer. This is less efficient by 1 byte only 
when the serialize accepts nulls AND references is false.

I went ahead and implemented #1, since the type should be known if 
writeObjectOrNull is being called. Tests added.

Original comment by nathan.s...@gmail.com on 31 May 2012 at 8:08

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r258.

Original comment by nathan.s...@gmail.com on 31 May 2012 at 8:08

GoogleCodeExporter commented 8 years ago
Looks good, thanks for fixing it so quickly! Any idea when you're cutting the 
next release?

Original comment by pbak...@groupon.com on 31 May 2012 at 8:51

GoogleCodeExporter commented 8 years ago
Release 2.11 just now.

Original comment by nathan.s...@gmail.com on 31 May 2012 at 11:02