Skip to content

Commit 0aa6670

Browse files
committed
PS: Model underscore parameters as pipeline parameters.
1 parent a0e17ee commit 0aa6670

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ abstract private class AbstractFunction extends Ast {
4343
* Gets the i'th parameter of this function, if any.
4444
*
4545
* This does not include the `this` parameter.
46+
*
47+
* The implicit underscore parameter (if any) is included at index `-1`.
4648
*/
4749
final Parameter getParameter(int i) {
48-
result = this.getFunctionParameter(i)
49-
or
50-
result = this.getBody().getParamBlock().getParameter(i)
50+
result.getFunction() = this and
51+
result.getIndex() = i
5152
}
5253

5354
final Parameter getParameterExcludingPipline(int i) {

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ private class ParameterImpl extends TParameterImpl {
8282
result.getUserPath() = this.getName() and
8383
result.getEnclosingScope() = this.getEnclosingScope()
8484
}
85+
86+
abstract predicate isPipeline();
8587
}
8688

8789
private class InternalParameter extends ParameterImpl, TInternalParameter {
@@ -108,6 +110,10 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
108110
override Expr getDefaultValue() { result = p.getDefaultValue() }
109111

110112
override Attribute getAnAttribute() { result = p.getAnAttribute() }
113+
114+
override predicate isPipeline() {
115+
this.getAnAttribute().getANamedArgument() instanceof ValueFromPipelineAttribute
116+
}
111117
}
112118

113119
/**
@@ -138,6 +144,10 @@ private class Underscore extends ParameterImpl, TUnderscore {
138144
final override Scope getEnclosingScope() { result = scope }
139145

140146
final override Attribute getAnAttribute() { none() }
147+
148+
final override predicate isPipeline() { any() }
149+
150+
final override predicate isFunctionParameter(Function f, int i) { f.getBody() = scope and i = -1 }
141151
}
142152

143153
private class ThisParameter extends ParameterImpl, TThisParameter {
@@ -152,6 +162,8 @@ private class ThisParameter extends ParameterImpl, TThisParameter {
152162
final override Scope getEnclosingScope() { result = scope }
153163

154164
final override Attribute getAnAttribute() { none() }
165+
166+
final override predicate isPipeline() { none() }
155167
}
156168

157169
private newtype TVariable =
@@ -265,9 +277,7 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
265277

266278
Attribute getAnAttribute() { result = p.getAnAttribute() }
267279

268-
predicate isPipeline() {
269-
this.getAnAttribute().getANamedArgument() instanceof ValueFromPipelineAttribute
270-
}
280+
predicate isPipeline() { p.isPipeline() }
271281
}
272282

273283
class PipelineParameter extends Parameter {
@@ -276,7 +286,7 @@ class PipelineParameter extends Parameter {
276286

277287
/**
278288
* The variable that represents the value of a pipeline during a process block.
279-
*
289+
*
280290
* That is, it is _not_ the pipeline variable, but the value that is obtained by reading
281291
* from the pipeline.
282292
*/

0 commit comments

Comments
 (0)