Skip to content

Commit 05fcceb

Browse files
committed
Java: Refactor clearlyNotNullExpr into a base case that does not rely on SSA.
1 parent 63afeb8 commit 05fcceb

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

java/ql/lib/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,44 @@ EqualityTest varEqualityTestExpr(SsaVariable v1, SsaVariable v2, boolean isEqual
4040
isEqualExpr = result.polarity()
4141
}
4242

43-
/** Gets an expression that is provably not `null`. */
44-
Expr clearlyNotNullExpr(Expr reason) {
45-
result instanceof ClassInstanceExpr and reason = result
43+
Expr baseNotNullExpr() {
44+
result instanceof ClassInstanceExpr
4645
or
47-
result instanceof ArrayCreationExpr and reason = result
46+
result instanceof ArrayCreationExpr
4847
or
49-
result instanceof TypeLiteral and reason = result
48+
result instanceof TypeLiteral
5049
or
51-
result instanceof ThisAccess and reason = result
50+
result instanceof ThisAccess
5251
or
53-
result instanceof StringLiteral and reason = result
52+
result instanceof StringLiteral
5453
or
55-
result instanceof AddExpr and result.getType() instanceof TypeString and reason = result
54+
result instanceof AddExpr and result.getType() instanceof TypeString
5655
or
5756
exists(Field f |
5857
result = f.getAnAccess() and
5958
(f.hasName("TRUE") or f.hasName("FALSE")) and
60-
f.getDeclaringType().hasQualifiedName("java.lang", "Boolean") and
61-
reason = result
59+
f.getDeclaringType().hasQualifiedName("java.lang", "Boolean")
6260
)
6361
or
64-
result.(CastExpr).getExpr() = clearlyNotNullExpr(reason)
62+
result = any(EnumConstant c).getAnAccess()
6563
or
66-
result.(ImplicitCastExpr).getExpr() = clearlyNotNullExpr(reason)
64+
result instanceof ImplicitNotNullExpr
6765
or
68-
result instanceof ImplicitNotNullExpr and reason = result
66+
result instanceof ImplicitCoercionToUnitExpr
6967
or
70-
result instanceof ImplicitCoercionToUnitExpr and reason = result
68+
result
69+
.(MethodCall)
70+
.getMethod()
71+
.hasQualifiedName("com.google.common.base", "Strings", "nullToEmpty")
72+
}
73+
74+
/** Gets an expression that is provably not `null`. */
75+
Expr clearlyNotNullExpr(Expr reason) {
76+
result = baseNotNullExpr() and reason = result
77+
or
78+
result.(CastExpr).getExpr() = clearlyNotNullExpr(reason)
79+
or
80+
result.(ImplicitCastExpr).getExpr() = clearlyNotNullExpr(reason)
7181
or
7282
result.(AssignExpr).getSource() = clearlyNotNullExpr(reason)
7383
or
@@ -83,14 +93,14 @@ Expr clearlyNotNullExpr(Expr reason) {
8393
guard.controls(rval.getBasicBlock(), branch) and
8494
reason = guard and
8595
rval = v.getAUse() and
86-
result = rval
96+
result = rval and
97+
not result = baseNotNullExpr()
8798
)
8899
or
89-
exists(SsaVariable v | clearlyNotNull(v, reason) and result = v.getAUse())
90-
or
91-
exists(Method m | m = result.(MethodCall).getMethod() and reason = result |
92-
m.getDeclaringType().hasQualifiedName("com.google.common.base", "Strings") and
93-
m.hasName("nullToEmpty")
100+
exists(SsaVariable v |
101+
clearlyNotNull(v, reason) and
102+
result = v.getAUse() and
103+
not result = baseNotNullExpr()
94104
)
95105
}
96106

0 commit comments

Comments
 (0)