From 3812b3e941f39d9b7f99f1421a496e6b99b92c5f Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sun, 25 Apr 2021 04:02:43 +0200 Subject: [PATCH] Java: Improve NullGuards.clearlyNotNullExpr() --- .../semmle/code/java/dataflow/NullGuards.qll | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/java/ql/src/semmle/code/java/dataflow/NullGuards.qll b/java/ql/src/semmle/code/java/dataflow/NullGuards.qll index 589b1f7c49f5f..7f23a04378f1c 100644 --- a/java/ql/src/semmle/code/java/dataflow/NullGuards.qll +++ b/java/ql/src/semmle/code/java/dataflow/NullGuards.qll @@ -44,20 +44,22 @@ Expr clearlyNotNullExpr(Expr reason) { or result instanceof ArrayCreationExpr and reason = result or + result instanceof FunctionalExpr and reason = result + or result instanceof TypeLiteral and reason = result or result instanceof ThisAccess and reason = result or result instanceof StringLiteral and reason = result or + // Add and AssignAdd performing String concatenation never have null result result instanceof AddExpr and result.getType() instanceof TypeString and reason = result or - exists(Field f | - result = f.getAnAccess() and - (f.hasName("TRUE") or f.hasName("FALSE")) and - f.getDeclaringType().hasQualifiedName("java.lang", "Boolean") and - reason = result - ) + result instanceof AssignAddExpr and result.getType() instanceof TypeString and reason = result + or + result.getType() instanceof PrimitiveType and reason = result + or + result instanceof CompileTimeConstantExpr and reason = result or result.(CastExpr).getExpr() = clearlyNotNullExpr(reason) or @@ -70,6 +72,11 @@ Expr clearlyNotNullExpr(Expr reason) { (reason = r1 or reason = r2) ) or + exists(SwitchExpr s | + s = result and + forall(Expr resultExpr | resultExpr = s.getAResult() | resultExpr = clearlyNotNullExpr(reason)) + ) + or exists(SsaVariable v, boolean branch, RValue rval, Guard guard | guard = directNullGuard(v, branch, false) and guard.controls(rval.getBasicBlock(), branch) and