JS: Fix flow through &&

This is a long-standing bug we've been unable to fix due to noise from type inference.
This commit is contained in:
Asger F
2024-02-13 14:34:24 +01:00
parent 057ee85cd0
commit 7122a7502a
2 changed files with 25 additions and 1 deletions

View File

@@ -1693,7 +1693,11 @@ module DataFlow {
exists(Expr predExpr, Expr succExpr |
pred = valueNode(predExpr) and succ = valueNode(succExpr)
|
predExpr = succExpr.(LogicalBinaryExpr).getAnOperand()
predExpr = succExpr.(LogicalOrExpr).getAnOperand()
or
predExpr = succExpr.(NullishCoalescingExpr).getAnOperand()
or
predExpr = succExpr.(LogicalAndExpr).getRightOperand()
or
predExpr = succExpr.(ConditionalExpr).getABranch()
or

View File

@@ -238,6 +238,26 @@ private class AnalyzedBinaryExpr extends DataFlow::AnalyzedValueNode {
}
}
pragma[nomagic]
private predicate falsyValue(AbstractValue value) { value.getBooleanValue() = false }
/**
* Flow analysis for `&&` operators.
*/
private class AnalyzedLogicalAndExpr extends DataFlow::AnalyzedValueNode {
override LogicalAndExpr astNode;
pragma[nomagic]
private AnalyzedValueNode leftOperand() { result = astNode.getLeftOperand().analyze() }
override AbstractValue getALocalValue() {
result = super.getALocalValue()
or
result = this.leftOperand().getALocalValue() and
falsyValue(result)
}
}
/**
* Gets the `n`th operand of the given `+` or `+=` expression.
*/