Skip to content

Commit accf4ca

Browse files
committed
Ruby: Recognize custom self.new methods that return self.allocate
1 parent b64083d commit accf4ca

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,12 @@ private DataFlow::LocalSourceNode trackModuleAccess(Module m) {
473473
}
474474

475475
pragma[nomagic]
476-
private predicate hasUserDefinedSelf(Module m) {
477-
// cannot use `lookupSingletonMethod` due to negative recursion
478-
singletonMethodOnModule(_, "new", m.getSuperClass*()) // not `getAnAncestor` because singleton methods cannot be included
476+
private predicate hasUserDefinedNew(Module m) {
477+
exists(DataFlow::MethodNode method |
478+
// not `getAnAncestor` because singleton methods cannot be included
479+
singletonMethodOnModule(method.asCallableAstNode(), "new", m.getSuperClass*()) and
480+
not method.getSelfParameter().getAMethodCall("allocate").flowsTo(method.getAReturningNode())
481+
)
479482
}
480483

481484
/** Holds if `n` is an instance of type `tp`. */
@@ -536,7 +539,7 @@ private predicate isInstance(DataFlow::Node n, Module tp, boolean exact) {
536539
flowsToMethodCallReceiver(call, sourceNode, "new") and
537540
n.asExpr() = call and
538541
// `tp` should not have a user-defined `self.new` method
539-
not hasUserDefinedSelf(tp)
542+
not hasUserDefinedNew(tp)
540543
|
541544
// `C.new`
542545
sourceNode = trackModuleAccess(tp) and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ getTarget
244244
| calls.rb:647:9:647:34 | call to puts | calls.rb:102:5:102:30 | puts |
245245
| calls.rb:651:1:651:14 | call to new | calls.rb:117:5:117:16 | new |
246246
| calls.rb:651:1:651:14 | call to new | calls.rb:642:5:644:7 | new |
247+
| calls.rb:651:1:651:23 | call to instance | calls.rb:646:5:648:7 | instance |
247248
| hello.rb:12:5:12:24 | call to include | calls.rb:108:5:110:7 | include |
248249
| hello.rb:14:16:14:20 | call to hello | hello.rb:2:5:4:7 | hello |
249250
| hello.rb:20:16:20:20 | call to super | hello.rb:13:5:15:7 | message |
@@ -369,7 +370,6 @@ unresolvedCall
369370
| calls.rb:562:1:562:39 | call to each |
370371
| calls.rb:570:5:570:14 | call to singleton2 |
371372
| calls.rb:643:9:643:21 | call to allocate |
372-
| calls.rb:651:1:651:23 | call to instance |
373373
| hello.rb:20:16:20:26 | ... + ... |
374374
| hello.rb:20:16:20:34 | ... + ... |
375375
| hello.rb:20:16:20:40 | ... + ... |

0 commit comments

Comments
 (0)