Skip to content

Commit 7122a75

Browse files
committed
JS: Fix flow through &&
This is a long-standing bug we've been unable to fix due to noise from type inference.
1 parent 057ee85 commit 7122a75

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

javascript/ql/lib/semmle/javascript/dataflow/DataFlow.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,11 @@ module DataFlow {
16931693
exists(Expr predExpr, Expr succExpr |
16941694
pred = valueNode(predExpr) and succ = valueNode(succExpr)
16951695
|
1696-
predExpr = succExpr.(LogicalBinaryExpr).getAnOperand()
1696+
predExpr = succExpr.(LogicalOrExpr).getAnOperand()
1697+
or
1698+
predExpr = succExpr.(NullishCoalescingExpr).getAnOperand()
1699+
or
1700+
predExpr = succExpr.(LogicalAndExpr).getRightOperand()
16971701
or
16981702
predExpr = succExpr.(ConditionalExpr).getABranch()
16991703
or

javascript/ql/lib/semmle/javascript/dataflow/internal/BasicExprTypeInference.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ private class AnalyzedBinaryExpr extends DataFlow::AnalyzedValueNode {
238238
}
239239
}
240240

241+
pragma[nomagic]
242+
private predicate falsyValue(AbstractValue value) { value.getBooleanValue() = false }
243+
244+
/**
245+
* Flow analysis for `&&` operators.
246+
*/
247+
private class AnalyzedLogicalAndExpr extends DataFlow::AnalyzedValueNode {
248+
override LogicalAndExpr astNode;
249+
250+
pragma[nomagic]
251+
private AnalyzedValueNode leftOperand() { result = astNode.getLeftOperand().analyze() }
252+
253+
override AbstractValue getALocalValue() {
254+
result = super.getALocalValue()
255+
or
256+
result = this.leftOperand().getALocalValue() and
257+
falsyValue(result)
258+
}
259+
}
260+
241261
/**
242262
* Gets the `n`th operand of the given `+` or `+=` expression.
243263
*/

0 commit comments

Comments
 (0)