Skip to content

Commit 645b636

Browse files
authored
Hardened type resolution checks in QueryParameterizer (#289)
Also updated the README to mention docker for running tests.
1 parent 1d48147 commit 645b636

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ python -m site
5050
./gradlew check
5151
```
5252

53+
You need to be able to run [Docker](https://www.docker.com/) for some tests. Make sure you have it installed and have the necessary permissions to run with your user.
54+
5355
### Run the Core Codemods
5456
You can download and run the latest release from this repository in order to run the core codemods as a CLI:
5557

core-codemods/src/main/java/io/codemodder/codemods/QueryParameterizer.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import com.github.javaparser.ast.expr.Expression;
66
import com.github.javaparser.ast.expr.NameExpr;
77
import com.github.javaparser.ast.expr.StringLiteralExpr;
8+
import com.github.javaparser.resolution.types.ResolvedType;
89
import io.codemodder.ast.ASTs;
910
import io.codemodder.ast.LocalVariableDeclaration;
1011
import java.util.ArrayDeque;
1112
import java.util.ArrayList;
1213
import java.util.Deque;
1314
import java.util.List;
15+
import java.util.Optional;
1416
import java.util.stream.Collectors;
1517
import java.util.stream.Stream;
1618

@@ -51,6 +53,14 @@ List<Deque<Expression>> getInjections() {
5153
return injections;
5254
}
5355

56+
private Optional<ResolvedType> calculateResolvedType(final Expression e) {
57+
try {
58+
return Optional.of(e.calculateResolvedType());
59+
} catch (final RuntimeException exception) {
60+
return Optional.empty();
61+
}
62+
}
63+
5464
/**
5565
* Finds the leaves of an expression tree whose internal nodes are BinaryExpr, EnclosedExpr and
5666
* NameExpr. Anything else is considered a leaf. Returns a Stream containing the leaves in
@@ -59,9 +69,11 @@ List<Deque<Expression>> getInjections() {
5969
private Stream<Expression> findLeaves(final Expression e) {
6070
// EnclosedExpr and BinaryExpr are considered as internal nodes, so we recurse
6171
if (e instanceof EnclosedExpr) {
62-
if (e.calculateResolvedType().describe().equals("java.lang.String"))
72+
if (calculateResolvedType(e)
73+
.filter(rt -> rt.describe().equals("java.lang.String"))
74+
.isPresent()) {
6375
return findLeaves(e.asEnclosedExpr().getInner());
64-
else {
76+
} else {
6577
return Stream.of(e);
6678
}
6779
}
@@ -74,7 +86,9 @@ else if (e instanceof BinaryExpr
7486
}
7587
// NameExpr of String types should be recursively searched for more expressions.
7688
else if (e instanceof NameExpr
77-
&& e.calculateResolvedType().describe().equals("java.lang.String")) {
89+
&& calculateResolvedType(e)
90+
.filter(rt -> rt.describe().equals("java.lang.String"))
91+
.isPresent()) {
7892
// TODO consider fields and extract inits if any
7993
final var maybeSourceLVD =
8094
ASTs.findEarliestLocalVariableDeclarationOf(e, e.asNameExpr().getNameAsString())
@@ -152,7 +166,11 @@ private boolean convertibleToString(final Expression exp) {
152166
// See
153167
// https://download.oracle.com/otn-pub/jcp/jdbc-4_2-mrel2-spec/jdbc4.2-fr-spec.pdf
154168
// for type conversions by setObject
155-
final var type = exp.calculateResolvedType();
169+
final var typeRef = calculateResolvedType(exp);
170+
if (typeRef.isEmpty()) {
171+
return false;
172+
}
173+
final var type = typeRef.get();
156174

157175
// primitive type?
158176
if (type.isPrimitive()) return false;

0 commit comments

Comments
 (0)