From 1acbb84444ba40ca106f6bf529a877f39d61ed8a Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Fri, 5 Apr 2024 11:58:43 +0100 Subject: [PATCH] Shared/Java: Make the 'isNull' interface slightly prettier. --- java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll | 7 ++++--- shared/typeflow/codeql/typeflow/TypeFlow.qll | 6 ++++++ shared/typeflow/codeql/typeflow/internal/TypeFlowImpl.qll | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll b/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll index afec2a22ae8..ac781e223f2 100644 --- a/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll +++ b/java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll @@ -133,11 +133,12 @@ private module Input implements TypeFlowInput { not decl.hasImplicitInit() and not exists(decl.getInit()) ) - or - forex(TypeFlowNode mid | joinStep(mid, n) | Make::isNull(mid)) and + } + + predicate isExcludedFromNullAnalysis(TypeFlowNode n) { // Fields that are never assigned a non-null value are probably set by // reflection and are thus not always null. - not exists(n.asField()) + exists(n.asField()) } predicate exactTypeBase(TypeFlowNode n, RefType t) { diff --git a/shared/typeflow/codeql/typeflow/TypeFlow.qll b/shared/typeflow/codeql/typeflow/TypeFlow.qll index d2862b77637..7518805ac56 100644 --- a/shared/typeflow/codeql/typeflow/TypeFlow.qll +++ b/shared/typeflow/codeql/typeflow/TypeFlow.qll @@ -42,6 +42,12 @@ signature module TypeFlowInput { /** Holds if `n` represents a `null` value. */ predicate isNullValue(TypeFlowNode n); + /** + * Holds if `n` should be excluded from the set of null values even if + * the null analysis determines that `n` is always null. + */ + default predicate isExcludedFromNullAnalysis(TypeFlowNode n) { none() } + /** A type. */ class Type { /** Gets a textual representation of this type. */ diff --git a/shared/typeflow/codeql/typeflow/internal/TypeFlowImpl.qll b/shared/typeflow/codeql/typeflow/internal/TypeFlowImpl.qll index d640c4b67e1..c06b372afba 100644 --- a/shared/typeflow/codeql/typeflow/internal/TypeFlowImpl.qll +++ b/shared/typeflow/codeql/typeflow/internal/TypeFlowImpl.qll @@ -10,6 +10,9 @@ module TypeFlow I> { isNullValue(n) or exists(TypeFlowNode mid | isNull(mid) and step(mid, n)) + or + forex(TypeFlowNode mid | I::joinStep(mid, n) | isNull(mid)) and + not isExcludedFromNullAnalysis(n) } /**