Skip to content

Commit ea95e2e

Browse files
committed
Ruby: Use InclusionTests library in barrier guards
1 parent b9fc82a commit ea95e2e

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import codeql.ruby.CFG
66
private import codeql.ruby.controlflow.CfgNodes
77
private import codeql.ruby.dataflow.SSA
88
private import codeql.ruby.ast.internal.Constant
9+
private import codeql.ruby.InclusionTests
910

1011
private predicate stringConstCompare(CfgNodes::ExprCfgNode g, CfgNode e, boolean branch) {
1112
exists(CfgNodes::ExprNodes::ComparisonOperationCfgNode c |
@@ -72,18 +73,19 @@ deprecated class StringConstCompare extends DataFlow::BarrierGuard,
7273
}
7374

7475
private predicate stringConstArrayInclusionCall(CfgNodes::ExprCfgNode g, CfgNode e, boolean branch) {
75-
exists(CfgNodes::ExprNodes::MethodCallCfgNode mc |
76-
mc = g and
77-
mc.getExpr().getMethodName() = "include?" and
78-
mc.getArgument(0) = e
76+
exists(InclusionTest t |
77+
t.asExpr() = g and
78+
e = t.getContainedNode().asExpr() and
79+
branch = t.getPolarity()
7980
|
80-
exists(ExprNodes::ArrayLiteralCfgNode arr | isArrayConstant(mc.getReceiver(), arr) |
81+
exists(ExprNodes::ArrayLiteralCfgNode arr |
82+
isArrayConstant(t.getContainerNode().asExpr(), arr)
83+
|
8184
forall(ExprCfgNode elem | elem = arr.getAnArgument() |
8285
elem instanceof ExprNodes::StringLiteralCfgNode
8386
)
8487
)
85-
) and
86-
branch = true
88+
)
8789
}
8890

8991
/**
@@ -126,7 +128,7 @@ deprecated class StringConstArrayInclusionCall extends DataFlow::BarrierGuard,
126128
CfgNodes::ExprNodes::MethodCallCfgNode {
127129
private CfgNode checkedNode;
128130

129-
StringConstArrayInclusionCall() { stringConstArrayInclusionCall(this, checkedNode, _) }
131+
StringConstArrayInclusionCall() { stringConstArrayInclusionCall(this, checkedNode, true) }
130132

131133
override predicate checks(CfgNode expr, boolean branch) { expr = checkedNode and branch = true }
132134
}

ruby/ql/test/library-tests/dataflow/barrier-guards/barrier-guards.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ oldStyleBarrierGuards
88
| barrier-guards.rb:37:4:37:20 | call to include? | barrier-guards.rb:38:5:38:7 | foo | barrier-guards.rb:37:17:37:19 | foo | true |
99
| barrier-guards.rb:43:4:43:15 | ... == ... | barrier-guards.rb:45:9:45:11 | foo | barrier-guards.rb:43:4:43:6 | foo | true |
1010
| barrier-guards.rb:70:4:70:21 | call to include? | barrier-guards.rb:71:5:71:7 | foo | barrier-guards.rb:70:18:70:20 | foo | true |
11+
| barrier-guards.rb:82:4:82:25 | ... != ... | barrier-guards.rb:83:5:83:7 | foo | barrier-guards.rb:82:15:82:17 | foo | true |
1112
newStyleBarrierGuards
1213
| barrier-guards.rb:4:5:4:7 | foo |
1314
| barrier-guards.rb:10:5:10:7 | foo |
@@ -17,3 +18,5 @@ newStyleBarrierGuards
1718
| barrier-guards.rb:38:5:38:7 | foo |
1819
| barrier-guards.rb:45:9:45:11 | foo |
1920
| barrier-guards.rb:71:5:71:7 | foo |
21+
| barrier-guards.rb:83:5:83:7 | foo |
22+
| barrier-guards.rb:91:5:91:7 | foo |

ruby/ql/test/library-tests/dataflow/barrier-guards/barrier-guards.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@
7979
foo
8080
end
8181

82+
if foos.index(foo) != nil
83+
foo
84+
else
85+
foo
86+
end
87+
88+
if foos.index(foo)r == nil
89+
foo
90+
else
91+
foo
92+
end
93+
8294
bars = ["bar"]
8395

8496
if condition

0 commit comments

Comments
 (0)