Skip to content

Commit a8758c0

Browse files
committed
Ruby: Handle element references with blocks
1 parent d7ce2be commit a8758c0

File tree

9 files changed

+100
-1
lines changed

9 files changed

+100
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class ElementReferenceImpl extends MethodCallImpl, TElementReference {
112112

113113
final override string getMethodNameImpl() { result = "[]" }
114114

115-
final override Block getBlockImpl() { none() }
115+
final override Block getBlockImpl() { toGenerated(result) = g.getBlock() }
116116
}
117117

118118
abstract class SuperCallImpl extends MethodCallImpl, TSuperCall { }

ruby/ql/test/library-tests/dataflow/global/Flow.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
testFailures
22
edges
33
| blocks.rb:14:12:14:20 | call to source | blocks.rb:8:10:8:14 | yield ... | provenance | |
4+
| blocks.rb:17:10:17:10 | x | blocks.rb:18:11:18:11 | x | provenance | |
5+
| blocks.rb:18:11:18:11 | x | blocks.rb:24:18:24:18 | x | provenance | |
6+
| blocks.rb:24:3:24:11 | call to source | blocks.rb:17:10:17:10 | x | provenance | |
7+
| blocks.rb:24:18:24:18 | x | blocks.rb:25:8:25:8 | x | provenance | |
48
| captured_variables.rb:9:24:9:24 | x | captured_variables.rb:11:5:11:6 | fn [captured x] | provenance | |
59
| captured_variables.rb:11:5:11:6 | fn [captured x] | captured_variables.rb:10:20:10:20 | x | provenance | |
610
| captured_variables.rb:13:20:13:29 | call to taint | captured_variables.rb:9:24:9:24 | x | provenance | |
@@ -254,6 +258,11 @@ edges
254258
nodes
255259
| blocks.rb:8:10:8:14 | yield ... | semmle.label | yield ... |
256260
| blocks.rb:14:12:14:20 | call to source | semmle.label | call to source |
261+
| blocks.rb:17:10:17:10 | x | semmle.label | x |
262+
| blocks.rb:18:11:18:11 | x | semmle.label | x |
263+
| blocks.rb:24:3:24:11 | call to source | semmle.label | call to source |
264+
| blocks.rb:24:18:24:18 | x | semmle.label | x |
265+
| blocks.rb:25:8:25:8 | x | semmle.label | x |
257266
| captured_variables.rb:9:24:9:24 | x | semmle.label | x |
258267
| captured_variables.rb:10:20:10:20 | x | semmle.label | x |
259268
| captured_variables.rb:11:5:11:6 | fn [captured x] | semmle.label | fn [captured x] |
@@ -554,6 +563,7 @@ subpaths
554563
| instance_variables.rb:120:6:120:10 | foo16 [@field] | instance_variables.rb:13:5:15:7 | self in get_field [@field] | instance_variables.rb:14:9:14:21 | return | instance_variables.rb:120:6:120:20 | call to get_field |
555564
#select
556565
| blocks.rb:8:10:8:14 | yield ... | blocks.rb:14:12:14:20 | call to source | blocks.rb:8:10:8:14 | yield ... | $@ | blocks.rb:14:12:14:20 | call to source | call to source |
566+
| blocks.rb:25:8:25:8 | x | blocks.rb:24:3:24:11 | call to source | blocks.rb:25:8:25:8 | x | $@ | blocks.rb:24:3:24:11 | call to source | call to source |
557567
| captured_variables.rb:10:20:10:20 | x | captured_variables.rb:13:20:13:29 | call to taint | captured_variables.rb:10:20:10:20 | x | $@ | captured_variables.rb:13:20:13:29 | call to taint | call to taint |
558568
| captured_variables.rb:17:14:17:14 | x | captured_variables.rb:20:25:20:34 | call to taint | captured_variables.rb:17:14:17:14 | x | $@ | captured_variables.rb:20:25:20:34 | call to taint | call to taint |
559569
| captured_variables.rb:24:14:24:14 | x | captured_variables.rb:27:48:27:57 | call to taint | captured_variables.rb:24:14:24:14 | x | $@ | captured_variables.rb:27:48:27:57 | call to taint | call to taint |

ruby/ql/test/library-tests/dataflow/global/blocks.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ def m2
1212
A.new.m1 { source(1) }
1313

1414
A.new.m2 { source(2) }
15+
16+
class B
17+
def [](x)
18+
yield x
19+
end
20+
end
21+
22+
b = B.new
23+
24+
b[source(3)] do |x|
25+
sink x # $ hasValueFlow=3
26+
end

ruby/ql/test/library-tests/modules/ancestors.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ calls.rb:
154154
# 649| CustomNew2
155155
#-----| super -> Object
156156

157+
element_reference.rb:
158+
# 1| ClassWithElementRef
159+
#-----| super -> Object
160+
157161
hello.rb:
158162
# 1| EnglishWords
159163

ruby/ql/test/library-tests/modules/callgraph.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ getTarget
257257
| calls.rb:659:1:659:23 | call to instance | calls.rb:654:5:656:7 | instance |
258258
| calls.rb:667:2:667:25 | call to capture_parameter | calls.rb:661:1:665:3 | capture_parameter |
259259
| calls.rb:667:20:667:25 | call to new | calls.rb:117:5:117:16 | new |
260+
| element_reference.rb:3:9:3:19 | yield ... | element_reference.rb:9:6:9:19 | { ... } |
261+
| element_reference.rb:3:9:3:19 | yield ... | element_reference.rb:11:6:13:3 | do ... end |
262+
| element_reference.rb:7:5:7:27 | call to new | calls.rb:117:5:117:16 | new |
263+
| element_reference.rb:9:1:9:19 | ...[...] | element_reference.rb:2:5:4:7 | [] |
264+
| element_reference.rb:9:12:9:17 | call to puts | calls.rb:102:5:102:30 | puts |
265+
| element_reference.rb:11:1:13:3 | ...[...] | element_reference.rb:2:5:4:7 | [] |
266+
| element_reference.rb:12:5:12:10 | call to puts | calls.rb:102:5:102:30 | puts |
260267
| hello.rb:12:5:12:24 | call to include | calls.rb:108:5:110:7 | include |
261268
| hello.rb:14:16:14:20 | call to hello | hello.rb:2:5:4:7 | hello |
262269
| hello.rb:20:16:20:20 | super call to message | hello.rb:13:5:15:7 | message |
@@ -385,6 +392,7 @@ unresolvedCall
385392
| calls.rb:662:5:662:11 | call to [] |
386393
| calls.rb:662:5:664:7 | call to each |
387394
| calls.rb:667:1:667:35 | call to instance |
395+
| element_reference.rb:3:15:3:19 | ... + ... |
388396
| hello.rb:20:16:20:26 | ... + ... |
389397
| hello.rb:20:16:20:34 | ... + ... |
390398
| hello.rb:20:16:20:40 | ... + ... |
@@ -512,6 +520,7 @@ publicMethod
512520
| calls.rb:642:5:644:7 | new |
513521
| calls.rb:650:5:652:7 | new |
514522
| calls.rb:654:5:656:7 | instance |
523+
| element_reference.rb:2:5:4:7 | [] |
515524
| hello.rb:2:5:4:7 | hello |
516525
| hello.rb:5:5:7:7 | world |
517526
| hello.rb:13:5:15:7 | message |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class ClassWithElementRef
2+
def [](x)
3+
yield x + 1
4+
end
5+
end
6+
7+
c = ClassWithElementRef.new
8+
9+
c[1] { |x| puts x }
10+
11+
c[1] do |x|
12+
puts x
13+
end

ruby/ql/test/library-tests/modules/methods.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ getMethod
5151
| calls.rb:626:1:632:3 | Included | foo | calls.rb:627:5:629:7 | foo |
5252
| calls.rb:634:1:639:3 | IncludesIncluded | bar | calls.rb:636:5:638:7 | bar |
5353
| calls.rb:649:1:657:3 | CustomNew2 | instance | calls.rb:654:5:656:7 | instance |
54+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | [] | element_reference.rb:2:5:4:7 | [] |
5455
| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | hello |
5556
| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | world |
5657
| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | message |
@@ -561,6 +562,10 @@ lookupMethod
561562
| calls.rb:649:1:657:3 | CustomNew2 | private_on_main | calls.rb:185:1:186:3 | private_on_main |
562563
| calls.rb:649:1:657:3 | CustomNew2 | puts | calls.rb:102:5:102:30 | puts |
563564
| calls.rb:649:1:657:3 | CustomNew2 | to_s | calls.rb:172:5:173:7 | to_s |
565+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | [] | element_reference.rb:2:5:4:7 | [] |
566+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | new | calls.rb:117:5:117:16 | new |
567+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | puts | calls.rb:102:5:102:30 | puts |
568+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | to_s | calls.rb:172:5:173:7 | to_s |
564569
| file://:0:0:0:0 | Class | include | calls.rb:108:5:110:7 | include |
565570
| file://:0:0:0:0 | Class | module_eval | calls.rb:107:5:107:24 | module_eval |
566571
| file://:0:0:0:0 | Class | new | calls.rb:117:5:117:16 | new |
@@ -1080,6 +1085,12 @@ enclosingMethod
10801085
| calls.rb:662:10:662:10 | 2 | calls.rb:661:1:665:3 | capture_parameter |
10811086
| calls.rb:662:18:664:7 | do ... end | calls.rb:661:1:665:3 | capture_parameter |
10821087
| calls.rb:663:9:663:9 | x | calls.rb:661:1:665:3 | capture_parameter |
1088+
| element_reference.rb:2:12:2:12 | x | element_reference.rb:2:5:4:7 | [] |
1089+
| element_reference.rb:2:12:2:12 | x | element_reference.rb:2:5:4:7 | [] |
1090+
| element_reference.rb:3:9:3:19 | yield ... | element_reference.rb:2:5:4:7 | [] |
1091+
| element_reference.rb:3:15:3:15 | x | element_reference.rb:2:5:4:7 | [] |
1092+
| element_reference.rb:3:15:3:19 | ... + ... | element_reference.rb:2:5:4:7 | [] |
1093+
| element_reference.rb:3:19:3:19 | 1 | element_reference.rb:2:5:4:7 | [] |
10831094
| hello.rb:3:9:3:22 | return | hello.rb:2:5:4:7 | hello |
10841095
| hello.rb:3:16:3:22 | "hello" | hello.rb:2:5:4:7 | hello |
10851096
| hello.rb:3:17:3:21 | hello | hello.rb:2:5:4:7 | hello |

ruby/ql/test/library-tests/modules/modules.expected

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ getModule
3636
| calls.rb:634:1:639:3 | IncludesIncluded |
3737
| calls.rb:641:1:645:3 | CustomNew1 |
3838
| calls.rb:649:1:657:3 | CustomNew2 |
39+
| element_reference.rb:1:1:5:3 | ClassWithElementRef |
3940
| file://:0:0:0:0 | BasicObject |
4041
| file://:0:0:0:0 | Class |
4142
| file://:0:0:0:0 | Complex |
@@ -113,6 +114,7 @@ getADeclaration
113114
| calls.rb:105:1:113:3 | Module | calls.rb:105:1:113:3 | Module |
114115
| calls.rb:115:1:118:3 | Object | calls.rb:1:1:667:52 | calls.rb |
115116
| calls.rb:115:1:118:3 | Object | calls.rb:115:1:118:3 | Object |
117+
| calls.rb:115:1:118:3 | Object | element_reference.rb:1:1:13:4 | element_reference.rb |
116118
| calls.rb:115:1:118:3 | Object | hello.rb:1:1:22:3 | hello.rb |
117119
| calls.rb:115:1:118:3 | Object | instance_fields.rb:1:1:29:4 | instance_fields.rb |
118120
| calls.rb:115:1:118:3 | Object | modules.rb:1:1:129:4 | modules.rb |
@@ -153,6 +155,7 @@ getADeclaration
153155
| calls.rb:634:1:639:3 | IncludesIncluded | calls.rb:634:1:639:3 | IncludesIncluded |
154156
| calls.rb:641:1:645:3 | CustomNew1 | calls.rb:641:1:645:3 | CustomNew1 |
155157
| calls.rb:649:1:657:3 | CustomNew2 | calls.rb:649:1:657:3 | CustomNew2 |
158+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | element_reference.rb:1:1:5:3 | ClassWithElementRef |
156159
| hello.rb:1:1:8:3 | EnglishWords | hello.rb:1:1:8:3 | EnglishWords |
157160
| hello.rb:11:1:16:3 | Greeting | hello.rb:11:1:16:3 | Greeting |
158161
| hello.rb:18:1:22:3 | HelloWorld | hello.rb:18:1:22:3 | HelloWorld |
@@ -238,6 +241,7 @@ getSuperClass
238241
| calls.rb:634:1:639:3 | IncludesIncluded | calls.rb:115:1:118:3 | Object |
239242
| calls.rb:641:1:645:3 | CustomNew1 | calls.rb:115:1:118:3 | Object |
240243
| calls.rb:649:1:657:3 | CustomNew2 | calls.rb:115:1:118:3 | Object |
244+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | calls.rb:115:1:118:3 | Object |
241245
| file://:0:0:0:0 | Class | calls.rb:105:1:113:3 | Module |
242246
| file://:0:0:0:0 | Complex | file://:0:0:0:0 | Numeric |
243247
| file://:0:0:0:0 | FalseClass | calls.rb:115:1:118:3 | Object |
@@ -389,6 +393,7 @@ resolveConstantReadAccess
389393
| calls.rb:659:1:659:10 | CustomNew2 | CustomNew2 |
390394
| calls.rb:662:5:662:11 | Array | Array |
391395
| calls.rb:667:20:667:21 | C1 | C1 |
396+
| element_reference.rb:7:5:7:23 | ClassWithElementRef | ClassWithElementRef |
392397
| hello.rb:12:13:12:24 | EnglishWords | EnglishWords |
393398
| hello.rb:18:20:18:27 | Greeting | Greeting |
394399
| instance_fields.rb:4:22:4:31 | A_target | A_target |
@@ -495,6 +500,7 @@ resolveConstantWriteAccess
495500
| calls.rb:634:1:639:3 | IncludesIncluded | IncludesIncluded |
496501
| calls.rb:641:1:645:3 | CustomNew1 | CustomNew1 |
497502
| calls.rb:649:1:657:3 | CustomNew2 | CustomNew2 |
503+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | ClassWithElementRef |
498504
| hello.rb:1:1:8:3 | EnglishWords | EnglishWords |
499505
| hello.rb:11:1:16:3 | Greeting | Greeting |
500506
| hello.rb:18:1:22:3 | HelloWorld | HelloWorld |
@@ -1611,6 +1617,36 @@ enclosingModule
16111617
| calls.rb:667:2:667:25 | self | calls.rb:1:1:667:52 | calls.rb |
16121618
| calls.rb:667:20:667:21 | C1 | calls.rb:1:1:667:52 | calls.rb |
16131619
| calls.rb:667:20:667:25 | call to new | calls.rb:1:1:667:52 | calls.rb |
1620+
| element_reference.rb:1:1:5:3 | ClassWithElementRef | element_reference.rb:1:1:13:4 | element_reference.rb |
1621+
| element_reference.rb:2:5:4:7 | [] | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1622+
| element_reference.rb:2:12:2:12 | x | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1623+
| element_reference.rb:2:12:2:12 | x | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1624+
| element_reference.rb:3:9:3:19 | yield ... | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1625+
| element_reference.rb:3:15:3:15 | x | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1626+
| element_reference.rb:3:15:3:19 | ... + ... | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1627+
| element_reference.rb:3:19:3:19 | 1 | element_reference.rb:1:1:5:3 | ClassWithElementRef |
1628+
| element_reference.rb:7:1:7:1 | c | element_reference.rb:1:1:13:4 | element_reference.rb |
1629+
| element_reference.rb:7:1:7:27 | ... = ... | element_reference.rb:1:1:13:4 | element_reference.rb |
1630+
| element_reference.rb:7:5:7:23 | ClassWithElementRef | element_reference.rb:1:1:13:4 | element_reference.rb |
1631+
| element_reference.rb:7:5:7:27 | call to new | element_reference.rb:1:1:13:4 | element_reference.rb |
1632+
| element_reference.rb:9:1:9:1 | c | element_reference.rb:1:1:13:4 | element_reference.rb |
1633+
| element_reference.rb:9:1:9:19 | ...[...] | element_reference.rb:1:1:13:4 | element_reference.rb |
1634+
| element_reference.rb:9:3:9:3 | 1 | element_reference.rb:1:1:13:4 | element_reference.rb |
1635+
| element_reference.rb:9:6:9:19 | { ... } | element_reference.rb:1:1:13:4 | element_reference.rb |
1636+
| element_reference.rb:9:9:9:9 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
1637+
| element_reference.rb:9:9:9:9 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
1638+
| element_reference.rb:9:12:9:17 | call to puts | element_reference.rb:1:1:13:4 | element_reference.rb |
1639+
| element_reference.rb:9:12:9:17 | self | element_reference.rb:1:1:13:4 | element_reference.rb |
1640+
| element_reference.rb:9:17:9:17 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
1641+
| element_reference.rb:11:1:11:1 | c | element_reference.rb:1:1:13:4 | element_reference.rb |
1642+
| element_reference.rb:11:1:13:3 | ...[...] | element_reference.rb:1:1:13:4 | element_reference.rb |
1643+
| element_reference.rb:11:3:11:3 | 1 | element_reference.rb:1:1:13:4 | element_reference.rb |
1644+
| element_reference.rb:11:6:13:3 | do ... end | element_reference.rb:1:1:13:4 | element_reference.rb |
1645+
| element_reference.rb:11:10:11:10 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
1646+
| element_reference.rb:11:10:11:10 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
1647+
| element_reference.rb:12:5:12:10 | call to puts | element_reference.rb:1:1:13:4 | element_reference.rb |
1648+
| element_reference.rb:12:5:12:10 | self | element_reference.rb:1:1:13:4 | element_reference.rb |
1649+
| element_reference.rb:12:10:12:10 | x | element_reference.rb:1:1:13:4 | element_reference.rb |
16141650
| hello.rb:1:1:8:3 | EnglishWords | hello.rb:1:1:22:3 | hello.rb |
16151651
| hello.rb:2:5:4:7 | hello | hello.rb:1:1:8:3 | EnglishWords |
16161652
| hello.rb:3:9:3:22 | return | hello.rb:1:1:8:3 | EnglishWords |

ruby/ql/test/library-tests/modules/superclasses.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ calls.rb:
148148
# 649| CustomNew2
149149
#-----| -> Object
150150

151+
element_reference.rb:
152+
# 1| ClassWithElementRef
153+
#-----| -> Object
154+
151155
hello.rb:
152156
# 1| EnglishWords
153157

0 commit comments

Comments
 (0)