Skip to content

Commit a06cc30

Browse files
committed
Ruby: fix some more spurious call edges
1 parent b1dadc2 commit a06cc30

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,17 @@ private module Cached {
413413
// end
414414
// end
415415
// ```
416-
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), any(SingletonMethod sm),
417-
m.getSuperClass*())
416+
exists(Module target |
417+
target = m.getSuperClass*() and
418+
selfInMethod(sourceNode.(SsaSelfDefinitionNode).getVariable(), any(SingletonMethod sm),
419+
target) and
420+
// Singleton methods declared in a block in the top-level may spuriously end up being seen as singleton
421+
// methods on Object, if the block is actually evaluated in the context of another class.
422+
// The 'self' inside such a singleton method could then be any class, leading to self-calls
423+
// being resolved to arbitrary singleton methods.
424+
// To remedy this, we do not allow following super-classes all the way to Object.
425+
not (m != target and target = TResolved("Object"))
426+
)
418427
)
419428
)
420429
or

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ getTarget
265265
| private.rb:104:1:104:20 | call to new | calls.rb:117:5:117:16 | new |
266266
| private.rb:104:1:104:28 | call to call_m1 | private.rb:91:3:93:5 | call_m1 |
267267
| private.rb:105:1:105:20 | call to new | calls.rb:117:5:117:16 | new |
268-
| toplevel_self_singleton.rb:13:9:13:27 | call to ab_singleton_method | toplevel_self_singleton.rb:3:9:4:11 | ab_singleton_method |
269-
| toplevel_self_singleton.rb:19:9:19:27 | call to ab_singleton_method | toplevel_self_singleton.rb:3:9:4:11 | ab_singleton_method |
270268
unresolvedCall
271269
| calls.rb:23:9:23:19 | call to singleton_m |
272270
| calls.rb:26:9:26:18 | call to instance_m |
@@ -355,7 +353,9 @@ unresolvedCall
355353
| private.rb:105:1:105:23 | call to m1 |
356354
| toplevel_self_singleton.rb:8:1:15:3 | call to do_something |
357355
| toplevel_self_singleton.rb:10:9:10:27 | call to ab_singleton_method |
356+
| toplevel_self_singleton.rb:13:9:13:27 | call to ab_singleton_method |
358357
| toplevel_self_singleton.rb:17:12:21:1 | call to new |
358+
| toplevel_self_singleton.rb:19:9:19:27 | call to ab_singleton_method |
359359
privateMethod
360360
| calls.rb:1:1:3:3 | foo |
361361
| calls.rb:39:1:41:3 | call_instance_m |

0 commit comments

Comments
 (0)