Skip to content

Commit 4674bd7

Browse files
committed
Fix type var detection in TypeToken-capturing-type-variable.ql
1 parent 0fa3893 commit 4674bd7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

codeql-custom-queries-java/queries/gson/TypeToken-capturing-type-variable.ql

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@
1717
* Alternatively when using Kotlin the type variable can be made `reified`; this makes sure
1818
* the actual type is captured by the `TypeToken`.
1919
*
20+
* Newer Gson versions disallow capturing type variables by default, see the
21+
* [Troubleshooting Guide](https://github.com/google/gson/blob/main/Troubleshooting.md#typetoken-type-variable)
22+
* for more information and alternatives.
23+
*
24+
* @id todo
2025
* @kind problem
2126
*/
2227

2328
import java
2429

2530
private TypeVariable getAReferencedTypeVariable(Type t) {
2631
result = t
27-
or result = t.(ParameterizedType).getATypeArgument()
28-
or result = t.(Array).getComponentType()
32+
// Look for type variable recursively, e.g. for `List<List<T>>`
33+
or result = getAReferencedTypeVariable([
34+
t.(ParameterizedType).getATypeArgument(),
35+
t.(Array).getComponentType()
36+
])
2937
// Don't have to consider other types (e.g. intersection type) for now because they cannot appear when creating TypeToken subclass
3038
}
3139

@@ -37,4 +45,4 @@ where
3745
and typeVariable = getAReferencedTypeVariable(parameterizedTypeToken)
3846
// Ignore Kotlin reified type variable because that is actually safe
3947
and not typeVariable.isReified()
40-
select typeTokenCreation, "Capturing type variable $@ is not type safe", typeVariable, typeVariable.getName()
48+
select typeTokenCreation, "Capturing type variable '$@' is not type safe", typeVariable, typeVariable.getName()

0 commit comments

Comments
 (0)