Skip to content

Commit 99b5c58

Browse files
committed
Ruby: Fix captured reads in lambdas
These were previously identified as method calls. The fix is to recognise lambdas as a scope which can inherit variables from its parent.
1 parent c891e62 commit 99b5c58

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ import Cached
406406

407407
/** Holds if this scope inherits `name` from an outer scope `outer`. */
408408
private predicate inherits(Scope::Range scope, string name, Scope::Range outer) {
409-
(scope instanceof Ruby::Block or scope instanceof Ruby::DoBlock) and
409+
(
410+
scope instanceof Ruby::Block or
411+
scope instanceof Ruby::DoBlock or
412+
scope instanceof Ruby::Lambda
413+
) and
410414
not scopeDefinesParameterVariable(scope, name, _) and
411415
(
412416
outer = scope.getOuterScope() and

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,7 @@ calls/calls.rb:
714714
# 351| getAnOperand/getRightOperand: [Lambda] -> { ... }
715715
# 351| getParameter: [SimpleParameter] x
716716
# 351| getDefiningAccess: [LocalVariableAccess] x
717-
# 351| getStmt: [MethodCall] call to y
718-
# 351| getReceiver: [SelfVariableAccess] self
717+
# 351| getStmt: [LocalVariableAccess] y
719718
# 352| getStmt: [AssignExpr] ... = ...
720719
# 352| getAnOperand/getLeftOperand: [LocalVariableAccess] f
721720
# 352| getAnOperand/getRightOperand: [Lambda] -> { ... }
@@ -737,8 +736,7 @@ calls/calls.rb:
737736
# 354| getParameter: [SimpleParameter] x
738737
# 354| getDefiningAccess: [LocalVariableAccess] x
739738
# 355| getStmt: [LocalVariableAccess] x
740-
# 356| getStmt: [MethodCall] call to y
741-
# 356| getReceiver: [SelfVariableAccess] self
739+
# 356| getStmt: [LocalVariableAccess] y
742740
# 357| getStmt: [MethodCall] call to unknown_call
743741
# 357| getReceiver: [SelfVariableAccess] self
744742
control/cases.rb:

ruby/ql/test/library-tests/ast/calls/calls.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,8 @@ callsWithReceiver
369369
| calls.rb:345:1:345:15 | call to foo | calls.rb:345:1:345:15 | self |
370370
| calls.rb:346:1:346:10 | call to foo | calls.rb:346:1:346:10 | self |
371371
| calls.rb:347:1:347:7 | call to foo | calls.rb:347:1:347:7 | self |
372-
| calls.rb:351:14:351:14 | call to y | calls.rb:351:14:351:14 | self |
373372
| calls.rb:352:13:352:17 | call to foo | calls.rb:352:13:352:17 | self |
374373
| calls.rb:353:13:353:24 | call to unknown_call | calls.rb:353:13:353:24 | self |
375-
| calls.rb:356:3:356:3 | call to y | calls.rb:356:3:356:3 | self |
376374
| calls.rb:357:3:357:14 | call to unknown_call | calls.rb:357:3:357:14 | self |
377375
callsWithBlock
378376
| calls.rb:17:1:17:17 | call to foo | calls.rb:17:5:17:17 | { ... } |

0 commit comments

Comments
 (0)