Skip to content

Commit b5a3d3c

Browse files
committed
Ruby: Extract isArrayConstant
This predicate might be useful elsewhere.
1 parent 301914d commit b5a3d3c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/Constant.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,23 @@ private module Cached {
509509
}
510510

511511
import Cached
512+
513+
/**
514+
* Holds if `e` is an array constructed from an array literal.
515+
* Example:
516+
* ```rb
517+
* [1, 2, 3]
518+
* C = [1, 2, 3]; C
519+
* x = [1, 2, 3]; x
520+
* ```
521+
*/
522+
predicate isArrayConstant(ExprCfgNode e, ArrayLiteralCfgNode arr) {
523+
// [...]
524+
e = arr
525+
or
526+
// C = [...]; C
527+
e.(ExprNodes::ConstantReadAccessCfgNode).getExpr().getValue().getDesugared() = arr.getExpr()
528+
or
529+
// x = [...]; x
530+
exists(Ssa::WriteDefinition def | def.getARead() = e and def.assigns(arr))
531+
}

ruby/ql/lib/codeql/ruby/dataflow/BarrierGuards.qll

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ private import codeql.ruby.DataFlow
55
private import codeql.ruby.CFG
66
private import codeql.ruby.controlflow.CfgNodes
77
private import codeql.ruby.dataflow.SSA
8+
private import codeql.ruby.ast.internal.Constant
89

910
private predicate stringConstCompare(CfgNodes::ExprCfgNode g, CfgNode e, boolean branch) {
1011
exists(CfgNodes::ExprNodes::ComparisonOperationCfgNode c |
@@ -137,19 +138,7 @@ deprecated class StringConstArrayInclusionCall extends DataFlow::BarrierGuard,
137138
StringConstArrayInclusionCall() {
138139
this.getMethodName() = "include?" and
139140
this.getArgument(0) = checkedNode and
140-
exists(ExprNodes::ArrayLiteralCfgNode arr |
141-
// [...].include?
142-
this.getReceiver() = arr
143-
or
144-
// C = [...]
145-
// C.include?
146-
this.getReceiver().(ExprNodes::ConstantReadAccessCfgNode).getExpr().getValue().getDesugared() =
147-
arr.getExpr()
148-
or
149-
// x = [...]
150-
// x.include?
151-
exists(Ssa::WriteDefinition def | def.getARead() = this.getReceiver() and def.assigns(arr))
152-
|
141+
exists(ExprNodes::ArrayLiteralCfgNode arr | isArrayConstant(this.getReceiver(), arr) |
153142
forall(ExprCfgNode elem | elem = arr.getAnArgument() |
154143
elem instanceof ExprNodes::StringLiteralCfgNode
155144
)

0 commit comments

Comments
 (0)