Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0dd891

Browse files
committed
Handle parameters with overlapping names
1 parent 58baa33 commit c0dd891

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ private VariableScope enclosingScope(Generated::AstNode node) {
1313
result.getScopeElement() = parent*(node.getParent())
1414
}
1515

16+
private predicate parameterAssignment(CallableScope scope, string name, Generated::Identifier i) {
17+
assignment(i, true) and
18+
scope = enclosingScope(i) and
19+
name = i.getValue()
20+
}
21+
1622
/** Holds if `scope` defines `name` in its parameter declaration at `i`. */
1723
private predicate scopeDefinesParameterVariable(
1824
CallableScope scope, string name, Generated::Identifier i
1925
) {
20-
assignment(i, true) and
21-
scope = enclosingScope(i) and
22-
name = i.getValue()
26+
parameterAssignment(scope, name, i) and
27+
// In case of overlapping parameter names (e.g. `_`), only the first
28+
// parameter will give rise to a variable
29+
i =
30+
min(Generated::Identifier other |
31+
parameterAssignment(scope, name, other)
32+
|
33+
other order by other.getLocation().getStartLine(), other.getLocation().getStartColumn()
34+
)
2335
or
2436
exists(Parameter p |
2537
p = scope.getScopeElement().getAParameter() and

ql/test/library-tests/variables/parameter.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ parameter
1818
| parameters.rb:35:11:35:21 | a | parameters.rb:35:11:35:11 | a |
1919
| parameters.rb:40:12:40:19 | d | parameters.rb:40:12:40:12 | d |
2020
| parameters.rb:45:20:45:20 | _ | parameters.rb:45:20:45:20 | _ |
21-
| parameters.rb:45:20:45:20 | _ | parameters.rb:45:22:45:22 | _ |
22-
| parameters.rb:45:22:45:22 | _ | parameters.rb:45:20:45:20 | _ |
23-
| parameters.rb:45:22:45:22 | _ | parameters.rb:45:22:45:22 | _ |
2421
| parameters.rb:54:14:54:24 | y | parameters.rb:54:14:54:14 | y |
2522
parameterNoAcess
2623
| nested_scopes.rb:16:26:16:26 | x |
2724
| nested_scopes.rb:18:26:18:26 | x |
25+
| parameters.rb:45:22:45:22 | _ |
2826
| scopes.rb:2:14:2:14 | x |
2927
| scopes.rb:9:14:9:14 | x |

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
| parameters.rb:42:16:42:16 | e | parameters.rb:40:15:40:15 | e | parameters.rb:40:1:43:3 | method scope |
3535
| parameters.rb:45:22:45:22 | _ | parameters.rb:45:20:45:20 | _ | parameters.rb:45:1:47:3 | method scope |
3636
| parameters.rb:46:8:46:8 | _ | parameters.rb:45:20:45:20 | _ | parameters.rb:45:1:47:3 | method scope |
37-
| parameters.rb:46:8:46:8 | _ | parameters.rb:45:22:45:22 | _ | parameters.rb:45:1:47:3 | method scope |
3837
| parameters.rb:50:11:50:11 | a | parameters.rb:49:13:49:13 | a | parameters.rb:49:1:51:3 | method scope |
3938
| parameters.rb:50:16:50:16 | b | parameters.rb:49:15:49:15 | b | parameters.rb:49:1:51:3 | method scope |
4039
| parameters.rb:54:19:54:19 | x | parameters.rb:53:1:53:1 | x | parameters.rb:1:1:58:1 | top-level scope |

ql/test/library-tests/variables/variable.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
| parameters.rb:40:12:40:12 | d |
3030
| parameters.rb:40:15:40:15 | e |
3131
| parameters.rb:45:20:45:20 | _ |
32-
| parameters.rb:45:22:45:22 | _ |
3332
| parameters.rb:49:13:49:13 | a |
3433
| parameters.rb:49:15:49:15 | b |
3534
| parameters.rb:53:1:53:1 | x |

0 commit comments

Comments
 (0)