Skip to content

Commit 24bb11b

Browse files
committed
Improve module/class resolution
1 parent 12ee957 commit 24bb11b

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

ql/src/codeql_ruby/ast/internal/Module.qll

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ private TResolved resolveScopeExpr(ConstantReadAccess r) {
6363
qname =
6464
min(string qn, int p |
6565
isDefinedConstant(qn) and
66-
qn = resolveScopeExpr(r, p)
66+
qn = resolveScopeExpr(r, p) and
67+
// prevent classes/modules that contain/extend themselves
68+
not exists(ConstantWriteAccess w | qn = constantDefinition0(w) |
69+
r = w.getScopeExpr()
70+
or
71+
r = w.(ClassDeclaration).getSuperclassExpr()
72+
)
6773
|
6874
qn order by p
6975
)
@@ -100,18 +106,20 @@ private string resolveScopeExpr(ConstantReadAccess c, int priority) {
100106
or
101107
not exists(c.getScopeExpr()) and
102108
not c.hasGlobalScope() and
103-
exists(Namespace n |
104-
result = qualifiedModuleName(constantDefinition0(n), c.getName()) and
105-
n = enclosing(c.getEnclosingModule(), priority)
109+
(
110+
exists(Namespace n |
111+
result = qualifiedModuleName(constantDefinition0(n), c.getName()) and
112+
n = enclosing(c.getEnclosingModule(), priority)
113+
)
114+
or
115+
result =
116+
qualifiedModuleName(ancestors(qualifiedModuleName(c.getEnclosingModule()),
117+
priority - maxDepth()), c.getName())
118+
or
119+
result = c.getName() and
120+
priority = maxDepth() + 4 and
121+
qualifiedModuleName(c.getEnclosingModule()) != "BasicObject"
106122
)
107-
or
108-
result =
109-
qualifiedModuleName(ancestors(qualifiedModuleName(c.getEnclosingModule()), priority - maxDepth()),
110-
c.getName())
111-
or
112-
result = c.getName() and
113-
priority = maxDepth() + 4 and
114-
qualifiedModuleName(c.getEnclosingModule()) != "BasicObject"
115123
}
116124

117125
bindingset[qualifier, name]

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ moduleTypes
141141
| modules.rb:101:1:105:3 | PrependTest | modules.rb:101:1:105:3 | PrependTest |
142142
| modules.rb:103:3:104:5 | Y | modules.rb:103:3:104:5 | Test::Foo2::Y |
143143
| modules.rb:107:1:110:3 | MM | modules.rb:107:1:110:3 | MM |
144-
| modules.rb:108:3:109:5 | MM | modules.rb:108:3:109:5 | ...::MM |
144+
| modules.rb:108:3:109:5 | MM | modules.rb:108:3:109:5 | MM::MM |
145145
| modules.rb:112:1:113:3 | YY | modules.rb:112:1:113:3 | YY |
146146
| modules.rb:115:1:118:3 | XX | modules.rb:115:1:118:3 | XX |
147147
| modules.rb:116:7:117:9 | YY | modules.rb:116:7:117:9 | XX::YY |
148-
| modules.rb:120:1:121:3 | Baz | modules.rb:120:1:121:3 | Bar::Baz |
149148
| modules.rb:120:1:121:3 | Baz | modules.rb:120:1:121:3 | Test::Foo1::Bar::Baz |
150149
| toplevel.rb:1:1:5:23 | toplevel.rb | file://:0:0:0:0 | Object |

0 commit comments

Comments
 (0)