Skip to content

Commit b688aab

Browse files
committed
Java: Improve customNullGuard performance.
1 parent ae56b82 commit b688aab

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,20 @@ Guard nullGuard(SsaVariable v, boolean branch, boolean isnull) {
242242
}
243243

244244
/**
245-
* A return statement that on a return value of `retval` allows the conclusion that the
246-
* parameter `p` either is null or non-null as specified by `isnull`.
245+
* A return statement in a non-overridable method that on a return value of
246+
* `retval` allows the conclusion that the parameter `p` either is null or
247+
* non-null as specified by `isnull`.
247248
*/
248249
private predicate validReturnInCustomNullGuard(
249250
ReturnStmt ret, Parameter p, boolean retval, boolean isnull
250251
) {
251252
exists(Method m |
252253
ret.getEnclosingCallable() = m and
253254
p.getCallable() = m and
254-
m.getReturnType().(PrimitiveType).hasName("boolean")
255+
m.getReturnType().(PrimitiveType).hasName("boolean") and
256+
not p.isVarargs() and
257+
p.getType() instanceof RefType and
258+
not m.isOverridable()
255259
) and
256260
exists(SsaImplicitInit ssa | ssa.isParameterDefinition(p) |
257261
nullGuardedReturn(ret, ssa, isnull) and
@@ -267,21 +271,22 @@ private predicate nullGuardedReturn(ReturnStmt ret, SsaImplicitInit ssa, boolean
267271
)
268272
}
269273

274+
pragma[nomagic]
275+
private Method returnStmtGetEnclosingCallable(ReturnStmt ret) {
276+
ret.getEnclosingCallable() = result
277+
}
278+
270279
/**
271280
* Gets a non-overridable method with a boolean return value that performs a null-check
272281
* on the `index`th parameter. A return value equal to `retval` allows us to conclude
273282
* that the argument either is null or non-null as specified by `isnull`.
274283
*/
275284
private Method customNullGuard(int index, boolean retval, boolean isnull) {
276285
exists(Parameter p |
277-
result.getReturnType().(PrimitiveType).hasName("boolean") and
278-
not result.isOverridable() and
279286
p.getCallable() = result and
280-
not p.isVarargs() and
281-
p.getType() instanceof RefType and
282287
p.getPosition() = index and
283288
forex(ReturnStmt ret |
284-
ret.getEnclosingCallable() = result and
289+
returnStmtGetEnclosingCallable(ret) = result and
285290
exists(Expr res | res = ret.getResult() |
286291
not res.(BooleanLiteral).getBooleanValue() = retval.booleanNot()
287292
)

0 commit comments

Comments
 (0)