Skip to content

Commit ad7b5ae

Browse files
committed
Ruby: Add inline barrier guard test
1 parent f1b63c4 commit ad7b5ae

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
WARNING: Type BarrierGuard has been deprecated and may be removed in future (barrier-guards.ql:9,3-15)
1+
WARNING: Type BarrierGuard has been deprecated and may be removed in future (barrier-guards.ql:10,3-15)
2+
failures
23
oldStyleBarrierGuards
34
| barrier-guards.rb:3:4:3:15 | ... == ... | barrier-guards.rb:4:5:4:7 | foo | barrier-guards.rb:3:4:3:6 | foo | true |
45
| barrier-guards.rb:9:4:9:24 | call to include? | barrier-guards.rb:10:5:10:7 | foo | barrier-guards.rb:9:21:9:23 | foo | true |
@@ -82,7 +83,7 @@ controls
8283
| barrier-guards.rb:125:6:125:10 | "foo" | barrier-guards.rb:126:5:126:7 | foo | match |
8384
| barrier-guards.rb:125:6:125:10 | "foo" | barrier-guards.rb:128:5:128:7 | foo | no-match |
8485
| barrier-guards.rb:132:6:132:10 | "foo" | barrier-guards.rb:133:5:133:7 | foo | match |
85-
| barrier-guards.rb:132:6:132:10 | "foo" | barrier-guards.rb:134:1:135:7 | when ... | no-match |
86+
| barrier-guards.rb:132:6:132:10 | "foo" | barrier-guards.rb:134:1:135:19 | when ... | no-match |
8687
| barrier-guards.rb:132:6:132:10 | "foo" | barrier-guards.rb:135:5:135:7 | foo | no-match |
8788
| barrier-guards.rb:134:6:134:10 | "bar" | barrier-guards.rb:135:5:135:7 | foo | match |
8889
| barrier-guards.rb:139:6:139:10 | "foo" | barrier-guards.rb:139:14:139:16 | bar | no-match |

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import codeql.ruby.controlflow.CfgNodes
44
import codeql.ruby.controlflow.ControlFlowGraph
55
import codeql.ruby.controlflow.BasicBlocks
66
import codeql.ruby.DataFlow
7+
import TestUtilities.InlineExpectationsTest
78

89
query predicate oldStyleBarrierGuards(
910
BarrierGuard g, DataFlow::Node guardedNode, ExprCfgNode expr, boolean branch
@@ -22,3 +23,19 @@ query predicate controls(CfgNode condition, BasicBlock bb, SuccessorTypes::Condi
2223
condition = cb.getLastNode()
2324
)
2425
}
26+
27+
class BarrierGuardTest extends InlineExpectationsTest {
28+
BarrierGuardTest() { this = "BarrierGuardTest" }
29+
30+
override string getARelevantTag() { result = "guarded" }
31+
32+
override predicate hasActualResult(Location location, string element, string tag, string value) {
33+
tag = "guarded" and
34+
exists(DataFlow::Node n |
35+
newStyleBarrierGuards(n) and
36+
location = n.getLocation() and
37+
element = n.toString() and
38+
value = ""
39+
)
40+
}
41+
}

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
foo = "foo"
22

33
if foo == "foo"
4-
foo
4+
foo # $ guarded
55
else
66
foo
77
end
88

99
if ["foo"].include?(foo)
10-
foo
10+
foo # $ guarded
1111
else
1212
foo
1313
end
1414

1515
if foo != "foo"
1616
foo
1717
else
18-
foo
18+
foo # $ guarded
1919
end
2020

2121
unless foo == "foo"
2222
foo
2323
else
24-
foo
24+
foo # $ guarded
2525
end
2626

2727
unless foo != "foo"
28-
foo
28+
foo # $ guarded
2929
else
3030
foo
3131
end
@@ -35,14 +35,14 @@
3535
FOO = ["foo"]
3636

3737
if FOO.include?(foo)
38-
foo
38+
foo # $ guarded
3939
else
4040
foo
4141
end
4242

4343
if foo == "foo"
4444
capture {
45-
foo # guarded
45+
foo # $ guarded
4646
}
4747
end
4848

@@ -68,7 +68,7 @@
6868
bars = NotAnArray.new
6969

7070
if foos.include?(foo)
71-
foo
71+
foo # $ guarded
7272
else
7373
foo
7474
end
@@ -80,15 +80,15 @@
8080
end
8181

8282
if foos.index(foo) != nil
83-
foo
83+
foo # $ guarded
8484
else
8585
foo
8686
end
8787

8888
if foos.index(foo) == nil
8989
foo
9090
else
91-
foo
91+
foo # $ guarded
9292
end
9393

9494
bars = ["bar"]
@@ -123,45 +123,45 @@
123123

124124
case foo
125125
when "foo"
126-
foo
126+
foo # $ guarded
127127
else
128128
foo
129129
end
130130

131131
case foo
132132
when "foo"
133-
foo
133+
foo # $ guarded
134134
when "bar"
135-
foo
135+
foo # $ guarded
136136
end
137137

138138
case foo
139-
when "foo", "bar" # not recognised
139+
when "foo", "bar" # $ MISSING: guarded
140140
foo
141-
when "baz", "quux" # not recognised
141+
when "baz", "quux" # $ MISSING: guarded
142142
foo
143143
else
144144
foo
145145
end
146146

147147
case foo
148148
when *["foo", "bar"]
149-
foo
149+
foo # $ guarded
150150
end
151151

152152
case foo
153153
when *%w[foo bar]
154-
foo
154+
foo # $ guarded
155155
end
156156

157157
case foo
158158
when *FOO
159-
foo
159+
foo # $ guarded
160160
end
161161

162162
case foo
163163
when *foos
164-
foo
164+
foo # $ guarded
165165
end
166166

167167
case foo
@@ -188,24 +188,24 @@
188188
foo
189189
end
190190

191-
if foo == "foo" or foo == "bar" # not recognised
192-
foo
191+
if foo == "foo" or foo == "bar"
192+
foo # $ MISSING: guarded
193193
end
194194

195195
case
196196
when foo == "foo"
197-
foo
197+
foo # $ guarded
198198
end
199199

200200
case
201-
when foo == "foo" then foo
201+
when foo == "foo" then foo # $ guarded
202202
when bar == "bar" then foo
203203
when foo == x then foo
204204
end
205205

206206
case foo
207207
in "foo"
208-
foo
208+
foo # $ guarded
209209
in x
210210
foo
211211
end

0 commit comments

Comments
 (0)