-
Notifications
You must be signed in to change notification settings - Fork 817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent creation of cyclic Generics stack when serializing specific generics object trees #342
Changes from all commits
b9b6ddc
2142849
4b604d8
fe55dbb
c0dadd9
25b2174
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* Copyright (c) 2008, Nathan Sweet | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following | ||
* conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following | ||
* disclaimer in the documentation and/or other materials provided with the distribution. | ||
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | ||
|
||
package com.esotericsoftware.kryo; | ||
|
||
import java.util.LinkedList; | ||
|
||
/** Helper class that resolves a type name variable to a concrete class using the current class serialization stack | ||
* | ||
* @author Jeroen van Erp <jeroen@hierynomus.com> */ | ||
public class GenericsResolver { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class is currently declared public. Is GenericResolver going to be exposed to the users? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I would agree, but |
||
private LinkedList<Generics> stack = new LinkedList<Generics>(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deque was introduced in Java7 only. Do we really need to use Deqeue here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually Java6. If we want to maintain Java5 compatibility, a |
||
GenericsResolver () { | ||
} | ||
|
||
public Class getConcreteClass (String typeVar) { | ||
for (Generics generics : stack) { | ||
Class clazz = generics.getConcreteClass(typeVar); | ||
if (clazz != null) return clazz; | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isSet () { | ||
return !stack.isEmpty(); | ||
} | ||
|
||
void pushScope (Generics scope) { | ||
stack.addFirst(scope); | ||
} | ||
|
||
void popScope () { | ||
stack.removeFirst(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.esotericsoftware.kryo; | ||
|
||
import com.esotericsoftware.kryo.io.FastOutput; | ||
import com.esotericsoftware.minlog.Log; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.*; | ||
|
||
public class FieldSerializerGenericsTest { | ||
|
||
@Test | ||
public void testNoStackOverflowForSimpleGenericsCase () { | ||
FooRef fooRef = new FooRef(); | ||
GenericFoo<FooRef> genFoo1 = new GenericFoo<FooRef>(fooRef); | ||
GenericFoo<FooRef> genFoo2 = new GenericFoo<FooRef>(fooRef); | ||
List<GenericFoo<?>> foos = new ArrayList<GenericFoo<?>>(); | ||
foos.add(genFoo2); | ||
foos.add(genFoo1); | ||
new FooContainer(foos); | ||
Kryo kryo = new Kryo(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
kryo.writeObject(new FastOutput(outputStream), genFoo1); | ||
} | ||
|
||
@Test | ||
public void testNoStackOverflowForComplexGenericsCase () { | ||
BarRef barRef = new BarRef(); | ||
GenericBar<BarRef> genBar1 = new GenericBar<BarRef>(barRef); | ||
GenericBar<BarRef> genBar2 = new GenericBar<BarRef>(barRef); | ||
List<GenericBar<?>> bars = new ArrayList<GenericBar<?>>(); | ||
bars.add(genBar2); | ||
bars.add(genBar1); | ||
new GenericBarContainer<GenericBar>(new BarContainer(bars)); | ||
Kryo kryo = new Kryo(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
kryo.writeObject(new FastOutput(outputStream), genBar1); | ||
} | ||
|
||
static class GenericBarContainer<T extends Bar> { | ||
BarContainer barContainer; | ||
|
||
public GenericBarContainer(BarContainer barContainer) { | ||
this.barContainer = barContainer; | ||
for (GenericBar<?> foo : barContainer.foos) { | ||
foo.container = this; | ||
} | ||
} | ||
} | ||
static class BarContainer { | ||
List<GenericBar<?>> foos; | ||
|
||
public BarContainer (List<GenericBar<?>> foos) { | ||
this.foos = foos; | ||
} | ||
} | ||
interface Bar {} | ||
static class BarRef implements Bar {} | ||
static class GenericBar<B extends Bar> implements Bar { | ||
private Map<String, Object> map = Collections.singletonMap("myself", (Object) this); | ||
B foo; | ||
GenericBarContainer<?> container; | ||
|
||
public GenericBar (B foo) { | ||
this.foo = foo; | ||
} | ||
} | ||
|
||
interface Foo {} | ||
static class FooRef implements Foo {} | ||
static class FooContainer { | ||
List<GenericFoo<?>> foos = new ArrayList<GenericFoo<?>>(); | ||
|
||
public FooContainer (List<GenericFoo<?>> foos) { | ||
this.foos = foos; | ||
for (GenericFoo<?> foo : foos) { | ||
foo.container = this; | ||
} | ||
} | ||
} | ||
static class GenericFoo<B extends Foo> implements Foo { | ||
private Map<String, Object> map = Collections.singletonMap("myself", (Object) this); | ||
B foo; | ||
FooContainer container; | ||
|
||
public GenericFoo (B foo) { | ||
this.foo = foo; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the standard license header to any new file you introduced.