Skip to content

Commit e811ba1

Browse files
committed
Ruby: handle private module methods
`private` can be used in both classes and modules.
1 parent b49ca6a commit e811ba1

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

ruby/ql/lib/codeql/ruby/ast/Method.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private class Private extends MethodCall {
4949
* method named `name`.
5050
*/
5151
pragma[noinline]
52-
predicate isRef(ClassDeclaration c, string name) {
52+
predicate isRef(Namespace c, string name) {
5353
this = c.getAStmt() and
5454
name = this.getMethod().(SymbolLiteral).getValueText()
5555
}
@@ -59,7 +59,7 @@ private class Private extends MethodCall {
5959
* and the call has no arguments.
6060
*/
6161
pragma[noinline]
62-
predicate hasNoArg(ClassDeclaration c, int i) {
62+
predicate hasNoArg(Namespace c, int i) {
6363
this = c.getStmt(i) and
6464
not exists(this.getMethod())
6565
}
@@ -91,7 +91,7 @@ class Method extends MethodBase, TMethod {
9191
final predicate isSetter() { g.getName() instanceof Ruby::Setter }
9292

9393
pragma[noinline]
94-
private predicate isDeclaredIn(ClassDeclaration c, string name) {
94+
private predicate isDeclaredIn(Namespace c, string name) {
9595
this = c.getAStmt() and
9696
name = this.getName()
9797
}
@@ -125,12 +125,12 @@ class Method extends MethodBase, TMethod {
125125
predicate isPrivate() {
126126
this = any(Private p).getMethod()
127127
or
128-
exists(ClassDeclaration c, Private p, string name |
128+
exists(Namespace c, Private p, string name |
129129
this.isDeclaredIn(c, name) and
130130
p.isRef(c, name)
131131
)
132132
or
133-
exists(ClassDeclaration c, Private p, int i, int j |
133+
exists(Namespace c, Private p, int i, int j |
134134
p.hasNoArg(c, i) and
135135
this = c.getStmt(j) and
136136
j > i

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ getTarget
8585
| private.rb:28:1:28:5 | call to new | calls.rb:99:5:99:16 | new |
8686
| private.rb:28:1:28:12 | call to public | private.rb:5:3:6:5 | public |
8787
| private.rb:30:1:30:15 | call to private_on_main | private.rb:21:1:22:3 | private_on_main |
88+
| private.rb:33:3:34:5 | call to private | calls.rb:94:5:94:20 | private |
89+
| private.rb:41:3:41:19 | call to private | calls.rb:94:5:94:20 | private |
90+
| private.rb:43:3:43:9 | call to private | calls.rb:94:5:94:20 | private |
8891
unresolvedCall
8992
| calls.rb:19:5:19:14 | call to instance_m |
9093
| calls.rb:20:5:20:19 | call to instance_m |
@@ -127,3 +130,7 @@ privateMethod
127130
| private.rb:14:3:15:5 | private3 |
128131
| private.rb:17:3:18:5 | private4 |
129132
| private.rb:21:1:22:3 | private_on_main |
133+
| private.rb:33:11:34:5 | private1 |
134+
| private.rb:39:3:40:5 | private2 |
135+
| private.rb:45:3:46:5 | private3 |
136+
| private.rb:48:3:49:5 | private4 |

ruby/ql/test/library-tests/modules/private.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,24 @@ def private_on_main
2727
C.new.private4
2828
C.new.public
2929

30-
private_on_main
30+
private_on_main
31+
32+
module D
33+
private def private1
34+
end
35+
36+
def public
37+
end
38+
39+
def private2
40+
end
41+
private :private2
42+
43+
private
44+
45+
def private3
46+
end
47+
48+
def private4
49+
end
50+
end

0 commit comments

Comments
 (0)