Java/Shared: Share more 'isNull' computations.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-03-20 14:36:12 +00:00
parent 90fbacc7bf
commit 6a65c46b2e
3 changed files with 11 additions and 8 deletions

View File

@@ -125,7 +125,7 @@ private module Input implements TypeFlowInput<J::Location> {
/**
* Holds if `null` is the only value that flows to `n`.
*/
predicate isNull(TypeFlowNode n) {
predicate isNullValue(TypeFlowNode n) {
n.asExpr() instanceof NullLiteral
or
exists(LocalVariableDeclExpr decl |
@@ -134,9 +134,7 @@ private module Input implements TypeFlowInput<J::Location> {
not exists(decl.getInit())
)
or
exists(TypeFlowNode mid | isNull(mid) and step(mid, n))
or
forex(TypeFlowNode mid | joinStep0(mid, n) | isNull(mid)) and
forex(TypeFlowNode mid | joinStep0(mid, n) | Make<J::Location, Input>::isNull(mid)) and
// Fields that are never assigned a non-null value are probably set by
// reflection and are thus not always null.
not exists(n.asField())

View File

@@ -39,10 +39,8 @@ signature module TypeFlowInput<LocationSig Location> {
*/
predicate step(TypeFlowNode n1, TypeFlowNode n2);
/**
* Holds if `null` is the only value that flows to `n`.
*/
predicate isNull(TypeFlowNode n);
/** Holds if `n` represents a `null` value. */
predicate isNullValue(TypeFlowNode n);
/** A type. */
class Type {

View File

@@ -5,6 +5,13 @@ private import codeql.util.Unit
module TypeFlow<LocationSig Location, TypeFlowInput<Location> I> {
private import I
/** Holds if `null` is the only value that flows to `n`. */
predicate isNull(TypeFlowNode n) {
isNullValue(n)
or
exists(TypeFlowNode mid | isNull(mid) and step(mid, n))
}
/**
* Holds if data can flow from `n1` to `n2` in one step, `n1` is not necessarily
* functionally determined by `n2`, and `n1` might take a non-null value.