Skip to content

Commit 0312dce

Browse files
committed
PS: Fix the scope of parameters.
1 parent a6a157a commit 0312dce

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

powershell/ql/lib/semmle/code/powershell/Function.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import powershell
2+
import semmle.code.powershell.controlflow.BasicBlocks
23

34
abstract private class AbstractFunction extends Ast {
45
abstract string getName();
@@ -20,6 +21,8 @@ abstract private class AbstractFunction extends Ast {
2021
}
2122

2223
final Parameter getAParameter() { result = this.getParameter(_) }
24+
25+
EntryBasicBlock getEntryBasicBlock() { result.getScope() = this.getBody() }
2326
}
2427

2528
class NonMemberFunction extends @function_definition, Stmt, AbstractFunction {

powershell/ql/lib/semmle/code/powershell/Variable.qll

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@ private import powershell
22
private import semmle.code.powershell.controlflow.internal.Scope
33
private import internal.Internal as Internal
44

5+
private predicate isFunctionParameterImpl(Internal::Parameter p, Function f, int i) {
6+
function_definition_parameter(f, i, p)
7+
or
8+
function_member_parameter(f, i, p)
9+
}
10+
11+
private predicate hasParameterBlockImpl(Internal::Parameter p, ParamBlock block, int i) {
12+
param_block_parameter(block, i, p)
13+
}
14+
15+
/**
16+
* Gets the enclosing scope of `p`.
17+
*
18+
* For a function parameter, this is the function body. For a parameter from a
19+
* parameter block, this is the enclosing scope of the parameter block.
20+
*
21+
* In both of the above cases, the enclosing scope is the function body.
22+
*/
23+
private Scope getEnclosingScopeImpl(Internal::Parameter p) {
24+
exists(Function f |
25+
isFunctionParameterImpl(p, f, _) and
26+
result = f.getBody()
27+
)
28+
or
29+
exists(ParamBlock b |
30+
hasParameterBlockImpl(p, b, _) and
31+
result = b.getEnclosingScope()
32+
)
33+
}
34+
535
bindingset[scope]
636
pragma[inline_late]
737
private predicate isParameterImpl(string name, Scope scope) {
8-
exists(Internal::Parameter p | p.getName() = name and p.getEnclosingScope() = scope)
38+
exists(Internal::Parameter p | p.getName() = name and getEnclosingScopeImpl(p) = scope)
939
or
1040
name = "_"
1141
}
@@ -47,17 +77,13 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
4777

4878
override string getName() { result = p.getName() }
4979

50-
final override Scope getEnclosingScope() { result = p.getEnclosingScope() }
80+
final override Scope getEnclosingScope() { result = getEnclosingScopeImpl(p) }
5181

5282
override predicate hasParameterBlock(ParamBlock block, int i) {
53-
param_block_parameter(block, i, p)
83+
hasParameterBlockImpl(p, block, i)
5484
}
5585

56-
override predicate isFunctionParameter(Function f, int i) {
57-
function_definition_parameter(f, i, p)
58-
or
59-
function_member_parameter(f, i, p)
60-
}
86+
override predicate isFunctionParameter(Function f, int i) { isFunctionParameterImpl(p, f, i) }
6187

6288
override Expr getDefaultValue() { result = p.getDefaultValue() }
6389
}
@@ -161,4 +187,6 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
161187
predicate hasDefaultValue() { exists(this.getDefaultValue()) }
162188

163189
int getIndex() { this.isFunctionParameter(_, result) }
190+
191+
Function getFunction() { result.getBody() = this.getDeclaringScope() }
164192
}

0 commit comments

Comments
 (0)