Skip to content

Commit bb08e6f

Browse files
committed
Ruby: Three call graph fixes for singleton methods
1 parent d13332c commit bb08e6f

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowDispatch.qll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private module Cached {
354354
// end
355355
// end
356356
// ```
357-
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), _, m)
357+
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), _, m.getSuperClass*())
358358
)
359359
)
360360
or
@@ -509,16 +509,17 @@ private DataFlow::Node trackInstance(Module tp, boolean exact, TypeTracker t) {
509509
// `self.new` inside a module
510510
selfInModule(sourceNode.(SsaSelfDefinitionNode).getVariable(), tp)
511511
or
512-
// `self.new` inside a (singleton) method
513-
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), _, tp)
512+
// `self.new` inside a singleton method
513+
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), any(SingletonMethod sm), tp)
514514
)
515515
or
516-
// `self` reference in method or top-level (but not in module, where instance
517-
// methods cannot be called; only singleton methods)
516+
// `self` reference in method or top-level (but not in module or singleton method,
517+
// where instance methods cannot be called; only singleton methods)
518518
result =
519519
any(SsaSelfDefinitionNode self |
520520
exists(MethodBase m |
521521
selfInMethod(self.getVariable(), m, tp) and
522+
not m instanceof SingletonMethod and
522523
if m.getEnclosingModule() instanceof Toplevel then exact = true else exact = false
523524
)
524525
or

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ getTarget
99
| calls.rb:19:1:19:8 | call to foo | calls.rb:1:1:3:3 | foo |
1010
| calls.rb:19:1:19:8 | call to foo | calls.rb:85:1:89:3 | foo |
1111
| calls.rb:23:9:23:19 | call to singleton_m | calls.rb:25:5:27:7 | singleton_m |
12-
| calls.rb:26:9:26:18 | call to instance_m | calls.rb:22:5:24:7 | instance_m |
1312
| calls.rb:32:5:32:15 | call to singleton_m | calls.rb:25:5:27:7 | singleton_m |
1413
| calls.rb:33:5:33:20 | call to singleton_m | calls.rb:25:5:27:7 | singleton_m |
1514
| calls.rb:37:1:37:13 | call to singleton_m | calls.rb:25:5:27:7 | singleton_m |
@@ -114,7 +113,6 @@ getTarget
114113
| calls.rb:305:1:305:22 | call to singleton_j | calls.rb:300:5:302:7 | singleton_j |
115114
| calls.rb:309:9:309:31 | call to puts | calls.rb:102:5:102:30 | puts |
116115
| calls.rb:310:9:310:11 | call to new | calls.rb:114:5:114:16 | new |
117-
| calls.rb:310:9:310:20 | call to instance | calls.rb:308:5:311:7 | instance |
118116
| calls.rb:314:9:314:11 | call to new | calls.rb:114:5:114:16 | new |
119117
| calls.rb:314:9:314:20 | call to instance | calls.rb:308:5:311:7 | instance |
120118
| calls.rb:317:5:317:7 | call to new | calls.rb:114:5:114:16 | new |
@@ -150,8 +148,10 @@ getTarget
150148
| calls.rb:372:1:372:11 | call to instance | calls.rb:365:5:367:7 | instance |
151149
| calls.rb:377:13:377:48 | call to puts | calls.rb:102:5:102:30 | puts |
152150
| calls.rb:381:13:381:22 | call to singleton1 | calls.rb:376:9:378:11 | singleton1 |
151+
| calls.rb:381:13:381:22 | call to singleton1 | calls.rb:401:9:403:11 | singleton1 |
153152
| calls.rb:386:9:386:44 | call to puts | calls.rb:102:5:102:30 | puts |
154153
| calls.rb:390:9:390:18 | call to singleton2 | calls.rb:385:5:387:7 | singleton2 |
154+
| calls.rb:390:9:390:18 | call to singleton2 | calls.rb:406:5:408:7 | singleton2 |
155155
| calls.rb:393:5:393:14 | call to singleton2 | calls.rb:385:5:387:7 | singleton2 |
156156
| calls.rb:396:1:396:34 | call to call_singleton1 | calls.rb:380:9:382:11 | call_singleton1 |
157157
| calls.rb:397:1:397:34 | call to call_singleton2 | calls.rb:389:5:391:7 | call_singleton2 |
@@ -187,6 +187,7 @@ getTarget
187187
| private.rb:51:3:51:19 | call to private | calls.rb:109:5:109:20 | private |
188188
| private.rb:53:3:53:9 | call to private | calls.rb:109:5:109:20 | private |
189189
unresolvedCall
190+
| calls.rb:26:9:26:18 | call to instance_m |
190191
| calls.rb:29:5:29:14 | call to instance_m |
191192
| calls.rb:30:5:30:19 | call to instance_m |
192193
| calls.rb:36:1:36:12 | call to instance_m |
@@ -216,6 +217,7 @@ unresolvedCall
216217
| calls.rb:258:1:258:14 | call to singleton_g |
217218
| calls.rb:271:1:271:14 | call to singleton_g |
218219
| calls.rb:273:1:273:14 | call to singleton_g |
220+
| calls.rb:310:9:310:20 | call to instance |
219221
| calls.rb:411:1:411:34 | call to call_singleton1 |
220222
| calls.rb:412:1:412:34 | call to call_singleton2 |
221223
| hello.rb:20:16:20:26 | ... + ... |

0 commit comments

Comments
 (0)