Skip to content

Commit 4e954c2

Browse files
authored
Merge pull request #13936 from hvitved/ruby/captured-access-fix
Ruby: Fix bug in `isCapturedAccess`
2 parents b99b6b8 + e40f0a7 commit 4e954c2

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import TreeSitter
22
private import codeql.ruby.AST
3+
private import codeql.ruby.CFG
34
private import codeql.ruby.ast.internal.AST
45
private import codeql.ruby.ast.internal.Parameter
56
private import codeql.ruby.ast.internal.Pattern
@@ -364,7 +365,7 @@ private module Cached {
364365

365366
cached
366367
predicate isCapturedAccess(LocalVariableAccess access) {
367-
exists(Scope scope1, Scope scope2 |
368+
exists(Scope scope1, CfgScope scope2 |
368369
scope1 = access.getVariable().getDeclaringScope() and
369370
scope2 = access.getCfgScope() and
370371
scope1 != scope2
@@ -375,10 +376,11 @@ private module Cached {
375376
// class C
376377
// def self.m // not a captured access
377378
// end
379+
//
380+
// self.foo // not a captured access
378381
// end
379382
// ```
380-
not scope2 instanceof Toplevel or
381-
not access = any(SingletonMethod m).getObject()
383+
not scope2 instanceof Toplevel
382384
else any()
383385
)
384386
}

ruby/ql/test/library-tests/variables/varaccess.expected

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,62 @@ readAccess
616616
| ssa.rb:100:10:100:11 | b4 |
617617
| ssa.rb:101:5:101:10 | self |
618618
| ssa.rb:101:10:101:10 | x |
619+
captureAccess
620+
| instance_variables.rb:28:3:28:4 | self |
621+
| instance_variables.rb:32:12:32:13 | self |
622+
| nested_scopes.rb:5:3:5:3 | a |
623+
| nested_scopes.rb:7:5:7:5 | a |
624+
| nested_scopes.rb:9:7:9:7 | a |
625+
| nested_scopes.rb:11:9:11:9 | a |
626+
| nested_scopes.rb:18:29:18:34 | self |
627+
| nested_scopes.rb:18:34:18:34 | a |
628+
| nested_scopes.rb:25:14:25:14 | a |
629+
| nested_scopes.rb:31:11:31:11 | a |
630+
| nested_scopes.rb:32:16:32:16 | a |
631+
| nested_scopes.rb:34:12:34:12 | a |
632+
| nested_scopes.rb:36:10:36:10 | a |
633+
| nested_scopes.rb:38:8:38:8 | a |
634+
| parameters.rb:3:4:3:9 | self |
635+
| parameters.rb:4:4:4:9 | self |
636+
| parameters.rb:17:5:17:28 | self |
637+
| parameters.rb:54:19:54:19 | x |
638+
| parameters.rb:55:4:55:9 | self |
639+
| parameters.rb:55:9:55:9 | x |
640+
| parameters.rb:56:4:56:9 | self |
641+
| scopes.rb:3:4:3:9 | self |
642+
| scopes.rb:3:9:3:9 | self |
643+
| scopes.rb:5:4:5:9 | self |
644+
| scopes.rb:10:4:10:9 | self |
645+
| scopes.rb:10:9:10:9 | a |
646+
| scopes.rb:11:4:11:4 | a |
647+
| scopes.rb:11:4:11:4 | a |
648+
| scopes.rb:12:4:12:9 | self |
649+
| scopes.rb:12:9:12:9 | a |
650+
| scopes.rb:13:4:13:4 | a |
651+
| scopes.rb:14:4:14:9 | self |
652+
| scopes.rb:14:9:14:9 | a |
653+
| scopes.rb:15:4:15:9 | self |
654+
| scopes.rb:16:4:16:9 | self |
655+
| scopes.rb:17:4:17:9 | self |
656+
| scopes.rb:29:3:29:3 | x |
657+
| scopes.rb:32:3:32:3 | x |
658+
| scopes.rb:35:3:35:3 | x |
659+
| scopes.rb:42:2:42:4 | var |
660+
| scopes.rb:43:2:43:4 | foo |
661+
| scopes.rb:44:5:44:7 | var |
662+
| scopes.rb:46:5:46:8 | var2 |
663+
| scopes.rb:47:5:47:8 | var2 |
664+
| ssa.rb:26:7:26:10 | elem |
665+
| ssa.rb:27:5:27:13 | self |
666+
| ssa.rb:27:10:27:13 | elem |
667+
| ssa.rb:34:5:34:10 | self |
668+
| ssa.rb:67:5:67:10 | self |
669+
| ssa.rb:68:5:68:17 | self |
670+
| ssa.rb:68:10:68:17 | captured |
671+
| ssa.rb:69:5:69:12 | captured |
672+
| ssa.rb:69:5:69:12 | captured |
673+
| ssa.rb:77:6:77:23 | self |
674+
| ssa.rb:77:15:77:22 | captured |
675+
| ssa.rb:84:6:86:8 | self |
676+
| ssa.rb:85:10:85:22 | self |
677+
| ssa.rb:85:15:85:22 | captured |

ruby/ql/test/library-tests/variables/varaccess.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ query predicate explicitWrite(VariableWriteAccess write, AstNode assignment) {
1212
query predicate implicitWrite(VariableWriteAccess write) { write.isImplicitWrite() }
1313

1414
query predicate readAccess(VariableReadAccess read) { any() }
15+
16+
query predicate captureAccess(LocalVariableAccess access) { access.isCapturedAccess() }

0 commit comments

Comments
 (0)