Skip to content
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

Fix TypeVariable/WildcardType recursion causing stackoverflows #948

Merged
merged 6 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions moshi/src/main/java/com/squareup/moshi/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,27 @@ public static GenericArrayType arrayOf(Type componentType) {
* ? extends Object}.
*/
public static WildcardType subtypeOf(Type bound) {
return new WildcardTypeImpl(new Type[] { bound }, EMPTY_TYPE_ARRAY);
Type[] upperBounds;
if (bound instanceof WildcardType) {
upperBounds = ((WildcardType) bound).getUpperBounds();
} else {
upperBounds = new Type[] { bound };
}
return new WildcardTypeImpl(upperBounds, EMPTY_TYPE_ARRAY);
}

/**
* Returns a type that represents an unknown supertype of {@code bound}. For example, if {@code
* bound} is {@code String.class}, this returns {@code ? super String}.
*/
public static WildcardType supertypeOf(Type bound) {
return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { bound });
Type[] lowerBounds;
if (bound instanceof WildcardType) {
lowerBounds = ((WildcardType) bound).getLowerBounds();
} else {
lowerBounds = new Type[] { bound };
}
return new WildcardTypeImpl(new Type[] { Object.class }, lowerBounds);
}

public static Class<?> getRawType(Type type) {
Expand Down
30 changes: 24 additions & 6 deletions moshi/src/main/java/com/squareup/moshi/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
Expand Down Expand Up @@ -169,38 +171,52 @@ public static Type removeSubtypeWildcard(Type type) {
}

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
return resolve(context, contextRawType, toResolve, new HashSet<TypeVariable>());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don‘t love this solution because it allocates a collection always even though this case is quite rare.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we initialize with an empty capacity? Still an instance but minimizes the footprint for non-typevar users

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets do that, but use null as the sentinel for empty?

}

private static Type resolve(Type context, Class<?> contextRawType, Type toResolve,
Collection<TypeVariable> visitedTypeVariables) {
// This implementation is made a little more complicated in an attempt to avoid object-creation.
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
if (visitedTypeVariables.contains(typeVariable)) {
// cannot reduce due to infinite recursion
return toResolve;
} else {
visitedTypeVariables.add(typeVariable);
}
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable) return toResolve;

} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
Type newComponentType = resolve(context, contextRawType, componentType,
visitedTypeVariables);
return componentType == newComponentType
? original
: arrayOf(newComponentType);

} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
Type newComponentType = resolve(context, contextRawType, componentType,
visitedTypeVariables);
return componentType == newComponentType
? original
: arrayOf(newComponentType);

} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
Type newOwnerType = resolve(context, contextRawType, ownerType, visitedTypeVariables);
boolean changed = newOwnerType != ownerType;

Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
Type resolvedTypeArgument = resolve(context, contextRawType, args[t],
visitedTypeVariables);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
Expand All @@ -220,12 +236,14 @@ public static Type resolve(Type context, Class<?> contextRawType, Type toResolve
Type[] originalUpperBound = original.getUpperBounds();

if (originalLowerBound.length == 1) {
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0],
visitedTypeVariables);
if (lowerBound != originalLowerBound[0]) {
return supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
Type upperBound = resolve(context, contextRawType, originalUpperBound[0],
visitedTypeVariables);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2017 Gson Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.squareup.moshi;

import com.squareup.moshi.internal.Util;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
* Test fixes for infinite recursion on {@link Util#resolve(java.lang.reflect.Type, Class,
* java.lang.reflect.Type)}, described at <a href="https://github.com/google/gson/issues/440">Issue #440</a>
* and similar issues.
* <p>
* These tests originally caused {@link StackOverflowError} because of infinite recursion on attempts to
* resolve generics on types, with an intermediate types like 'Foo2&lt;? extends ? super ? extends ... ? extends A&gt;'
* <p>
* Adapted from https://github.com/google/gson/commit/a300148003e3a067875b1444e8268b6e0f0e0e02 in
* service of https://github.com/square/moshi/issues/338.
*/
public final class RecursiveTypesResolveTest {

private static class Foo1<A> {
public Foo2<? extends A> foo2;
}

private static class Foo2<B> {
public Foo1<? super B> foo1;
}

/**
* Test simplest case of recursion.
*/
@Test public void recursiveResolveSimple() {
JsonAdapter<Foo1> adapter = new Moshi.Builder().build().adapter(Foo1.class);
assertNotNull(adapter);
}

//
// Tests belows check the behaviour of the methods changed for the fix
//

@Test public void doubleSupertype() {
assertEquals(Types.supertypeOf(Number.class),
Types.supertypeOf(Types.supertypeOf(Number.class)));
}

@Test public void doubleSubtype() {
assertEquals(Types.subtypeOf(Number.class),
Types.subtypeOf(Types.subtypeOf(Number.class)));
}

@Test public void superSubtype() {
assertEquals(Types.subtypeOf(Object.class),
Types.supertypeOf(Types.subtypeOf(Number.class)));
}

@Test public void subSupertype() {
assertEquals(Types.subtypeOf(Object.class),
Types.subtypeOf(Types.supertypeOf(Number.class)));
}
}
34 changes: 34 additions & 0 deletions moshi/src/test/java/com/squareup/moshi/TypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,40 @@ interface StringIntegerMap extends Map<String, Integer> {
}
}

//
// Regression tests for https://github.com/square/moshi/issues/338
//
// Adapted from https://github.com/google/gson/pull/1128
//

private static final class RecursiveTypeVars<T> {
RecursiveTypeVars<? super T> superType;
}

@Test public void recursiveTypeVariablesResolve() {
JsonAdapter<RecursiveTypeVars<String>> adapter = new Moshi.Builder().build().adapter(Types
.newParameterizedTypeWithOwner(TypesTest.class, RecursiveTypeVars.class, String.class));
assertThat(adapter).isNotNull();
}

@Test public void recursiveTypeVariablesResolve1() {
JsonAdapter<TestType> adapter = new Moshi.Builder().build().adapter(TestType.class);
assertThat(adapter).isNotNull();
}

@Test public void recursiveTypeVariablesResolve2() {
JsonAdapter<TestType2> adapter = new Moshi.Builder().build().adapter(TestType2.class);
assertThat(adapter).isNotNull();
}

private static class TestType<X> {
TestType<? super X> superType;
}

private static class TestType2<X, Y> {
TestType2<? super Y, ? super X> superReversedType;
}

@JsonClass(generateAdapter = false)
static class TestJsonClass {

Expand Down