Skip to content

Commit 9463927

Browse files
committed
Address review comments
1 parent 23447e6 commit 9463927

6 files changed

Lines changed: 45 additions & 2 deletions

File tree

ql/src/codeql_ruby/ast/Constant.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class ConstantReadAccess extends ConstantAccess {
114114
then result.getFile() = this.getFile()
115115
else any()
116116
or
117+
this.hasGlobalScope() and
118+
result = lookupConst(TResolved("Object"), this.getName())
119+
or
117120
result = lookupConst(resolveScopeExpr(this.getScopeExpr()), this.getName())
118121
}
119122

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,16 @@ private module Cached {
135135
Method lookupMethod(Module m, string name) { TMethod(result) = lookupMethodOrConst(m, name) }
136136

137137
cached
138-
Expr lookupConst(Module m, string name) { TExpr(result) = lookupMethodOrConst(m, name) }
138+
Expr lookupConst(Module m, string name) {
139+
TExpr(result) = lookupMethodOrConst(m, name)
140+
or
141+
exists(AssignExpr ae, ConstantWriteAccess w |
142+
w = ae.getLeftOperand() and
143+
w.getName() = name and
144+
m = resolveScopeExpr(w.getScopeExpr()) and
145+
result = ae.getRightOperand()
146+
)
147+
}
139148
}
140149

141150
import Cached

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,17 @@ constants/constants.rb:
867867
# 37| getScopeExpr: [ConstantReadAccess] ModuleB
868868
# 37| getScopeExpr: [ConstantReadAccess] ModuleA
869869
# 37| getAnOperand/getRightOperand: [IntegerLiteral] 1024
870+
# 39| getStmt: [MethodCall] call to puts
871+
# 39| getReceiver: [Self] self
872+
# 39| getArgument: [ConstantReadAccess] MAX_SIZE
873+
# 39| getScopeExpr: [ConstantReadAccess] ModuleB
874+
# 39| getScopeExpr: [ConstantReadAccess] ModuleA
875+
# 41| getStmt: [MethodCall] call to puts
876+
# 41| getReceiver: [Self] self
877+
# 41| getArgument: [ConstantReadAccess] GREETING
878+
# 42| getStmt: [MethodCall] call to puts
879+
# 42| getReceiver: [Self] self
880+
# 42| getArgument: [ConstantReadAccess] GREETING
870881
literals/literals.rb:
871882
# 1| [Toplevel] literals.rb
872883
# 2| getStmt: [NilLiteral] nil

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ constantAccess
2828
| constants.rb:37:1:37:7 | ModuleA | read | ModuleA | ConstantReadAccess |
2929
| constants.rb:37:1:37:16 | ModuleB | read | ModuleB | ConstantReadAccess |
3030
| constants.rb:37:1:37:26 | MAX_SIZE | write | MAX_SIZE | ConstantAssignment |
31+
| constants.rb:39:6:39:12 | ModuleA | read | ModuleA | ConstantReadAccess |
32+
| constants.rb:39:6:39:21 | ModuleB | read | ModuleB | ConstantReadAccess |
33+
| constants.rb:39:6:39:31 | MAX_SIZE | read | MAX_SIZE | ConstantReadAccess |
34+
| constants.rb:41:6:41:13 | GREETING | read | GREETING | ConstantReadAccess |
35+
| constants.rb:42:6:42:15 | GREETING | read | GREETING | ConstantReadAccess |
3136
getConst
3237
| constants.rb:1:1:15:3 | ModuleA | CONST_B | constants.rb:6:15:6:23 | "const_b" |
3338
| constants.rb:2:5:4:7 | ModuleA::ClassA | CONST_A | constants.rb:3:19:3:27 | "const_a" |
@@ -36,8 +41,16 @@ lookupConst
3641
| constants.rb:1:1:15:3 | ModuleA | CONST_B | constants.rb:6:15:6:23 | "const_b" |
3742
| constants.rb:2:5:4:7 | ModuleA::ClassA | CONST_A | constants.rb:3:19:3:27 | "const_a" |
3843
| constants.rb:2:5:4:7 | ModuleA::ClassA | GREETING | constants.rb:17:12:17:64 | ... + ... |
44+
| constants.rb:8:5:14:7 | ModuleA::ModuleB | MAX_SIZE | constants.rb:37:30:37:33 | 1024 |
3945
| constants.rb:9:9:10:11 | ModuleA::ModuleB::ClassB | GREETING | constants.rb:17:12:17:64 | ... + ... |
4046
| constants.rb:12:9:13:11 | ModuleA::ModuleB::ClassC | GREETING | constants.rb:17:12:17:64 | ... + ... |
4147
| constants.rb:31:1:32:3 | ModuleA::ClassD | CONST_A | constants.rb:3:19:3:27 | "const_a" |
4248
| constants.rb:31:1:32:3 | ModuleA::ClassD | GREETING | constants.rb:17:12:17:64 | ... + ... |
4349
| file://:0:0:0:0 | Object | GREETING | constants.rb:17:12:17:64 | ... + ... |
50+
constantValue
51+
| constants.rb:17:22:17:45 | CONST_A | constants.rb:3:19:3:27 | "const_a" |
52+
| constants.rb:17:49:17:64 | CONST_B | constants.rb:6:15:6:23 | "const_b" |
53+
| constants.rb:23:18:23:25 | GREETING | constants.rb:17:12:17:64 | ... + ... |
54+
| constants.rb:39:6:39:31 | MAX_SIZE | constants.rb:37:30:37:33 | 1024 |
55+
| constants.rb:41:6:41:13 | GREETING | constants.rb:17:12:17:64 | ... + ... |
56+
| constants.rb:42:6:42:15 | GREETING | constants.rb:17:12:17:64 | ... + ... |

ql/test/library-tests/ast/constants/constants.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ query predicate constantAccess(ConstantAccess a, string kind, string name, strin
1414
query Expr getConst(Module m, string name) { result = M::ExposedForTestingOnly::getConst(m, name) }
1515

1616
query Expr lookupConst(Module m, string name) { result = M::lookupConst(m, name) }
17+
18+
query predicate constantValue(ConstantReadAccess a, Expr e) { e = a.getValue() }

ql/test/library-tests/ast/constants/constants.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ class ModuleA::ClassD < ModuleA::ClassA
3434
module ModuleA::ModuleC
3535
end
3636

37-
ModuleA::ModuleB::MAX_SIZE = 1024
37+
ModuleA::ModuleB::MAX_SIZE = 1024
38+
39+
puts ModuleA::ModuleB::MAX_SIZE
40+
41+
puts GREETING
42+
puts ::GREETING

0 commit comments

Comments
 (0)