Skip to content

Commit c065085

Browse files
committed
Ruby: Cache ConstantReadAccess::getValue
1 parent 99ddfb4 commit c065085

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,7 @@ class ConstantReadAccess extends ConstantAccess {
224224
*
225225
* the value being read at `M::CONST` is `"const"`.
226226
*/
227-
Expr getValue() {
228-
not exists(this.getScopeExpr()) and
229-
result = lookupConst(this.getEnclosingModule+().getModule(), this.getName()) and
230-
// For now, we restrict the scope of top-level declarations to their file.
231-
// This may remove some plausible targets, but also removes a lot of
232-
// implausible targets
233-
if result.getEnclosingModule() instanceof Toplevel
234-
then result.getFile() = this.getFile()
235-
else any()
236-
or
237-
this.hasGlobalScope() and
238-
result = lookupConst(TResolved("Object"), this.getName())
239-
or
240-
result = lookupConst(resolveConstantReadAccess(this.getScopeExpr()), this.getName())
241-
}
227+
Expr getValue() { result = getConstantReadAccessValue(this) }
242228

243229
final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
244230
}

ruby/ql/lib/codeql/ruby/ast/internal/Constant.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import codeql.ruby.AST
22
private import codeql.ruby.ast.internal.Literal
3+
private import codeql.ruby.ast.internal.Module
34
private import codeql.ruby.controlflow.CfgNodes
45
private import codeql.ruby.dataflow.SSA
56
private import ExprNodes
@@ -441,6 +442,23 @@ private module Cached {
441442
result.isNil() and
442443
isNilExpr(e)
443444
}
445+
446+
cached
447+
Expr getConstantReadAccessValue(ConstantReadAccess read) {
448+
not exists(read.getScopeExpr()) and
449+
result = lookupConst(read.getEnclosingModule+().getModule(), read.getName()) and
450+
// For now, we restrict the scope of top-level declarations to their file.
451+
// This may remove some plausible targets, but also removes a lot of
452+
// implausible targets
453+
if result.getEnclosingModule() instanceof Toplevel
454+
then result.getFile() = read.getFile()
455+
else any()
456+
or
457+
read.hasGlobalScope() and
458+
result = lookupConst(TResolved("Object"), read.getName())
459+
or
460+
result = lookupConst(resolveConstantReadAccess(read.getScopeExpr()), read.getName())
461+
}
444462
}
445463

446464
import Cached

0 commit comments

Comments
 (0)