Skip to content

Commit 198ece9

Browse files
authored
Merge pull request #91 from microsoft/powershell-cfg-for-if-and-match
PS: CFG for `if`, `match`, exceptions
2 parents e133b5c + dbbb9b3 commit 198ece9

14 files changed

+340
-30
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import powershell
33
class ArrayExpr extends @array_expression, Expr {
44
override SourceLocation getLocation() { array_expression_location(this, result) }
55

6-
StmtBlock getStatementBlock() { array_expression(this, result) }
6+
StmtBlock getStmtBlock() { array_expression(this, result) }
77

8-
override string toString() { result = "ArrayExpression at: " + this.getLocation().toString() }
8+
override string toString() { result = "@(...)" }
99
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class Attribute extends @attribute, AttributeBase {
1515

1616
NamedAttributeArgument getANamedArgument() { result = this.getNamedArgument(_) }
1717

18+
int getNumberOfArguments() { result = count(this.getAPositionalArgument()) }
19+
1820
Expr getPositionalArgument(int i) { attribute_positional_argument(this, i, result) }
1921

2022
Expr getAPositionalArgument() { result = this.getPositionalArgument(_) }
23+
24+
int getNumberOfPositionalArguments() { result = count(this.getAPositionalArgument()) }
2125
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,24 @@ class CatchClause extends @catch_clause, Ast {
1616
predicate isCatchAll() { not exists(this.getACatchType()) }
1717

1818
TryStmt getTryStmt() { result.getACatchClause() = this }
19+
20+
predicate isLast() {
21+
exists(TryStmt ts, int last |
22+
ts = this.getTryStmt() and
23+
last = max(int i | exists(ts.getCatchClause(i))) and
24+
this = ts.getCatchClause(last)
25+
)
26+
}
27+
}
28+
29+
class GeneralCatchClause extends CatchClause {
30+
GeneralCatchClause() { this.isCatchAll() }
31+
32+
override string toString() { result = "catch {...}" }
33+
}
34+
35+
class SpecificCatchClause extends CatchClause {
36+
SpecificCatchClause() { not this.isCatchAll() }
37+
38+
override string toString() { result = "catch[...] {...}" }
1939
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,28 @@ class Cmd extends @command, CmdBase {
1515

1616
CmdElement getElement(int i) { command_command_element(this, i, result) }
1717

18-
Redirection getRedirection(int i) { command_redirection(this, i, result) }
18+
StringConstExpr getCmdName() { result = this.getElement(0) }
19+
20+
Expr getArgument(int i) {
21+
result =
22+
rank[i + 1](CmdElement e, int j |
23+
e = this.getElement(j) and
24+
not e instanceof CmdParameter and
25+
j > 0 // 0'th element is the command name itself
26+
|
27+
e order by j
28+
)
29+
}
30+
31+
Expr getNamedArgument(string name) {
32+
exists(int i, CmdParameter p |
33+
this.getElement(i) = p and
34+
p.getName() = name and
35+
result = p.getArgument()
36+
)
37+
}
1938

20-
CmdElement getAnElement() { result = this.getElement(_) }
39+
Redirection getRedirection(int i) { command_redirection(this, i, result) }
2140

2241
Redirection getARedirection() { result = this.getRedirection(_) }
2342
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ class CmdParameter extends @command_parameter, CmdElement {
55

66
string getName() { command_parameter(this, result) }
77

8-
Expr getArgument() { command_parameter_argument(this, result) }
8+
Expr getArgument() {
9+
// When an argumnt is of the form -Name:$var
10+
command_parameter_argument(this, result)
11+
or
12+
// When an argument is of the form -Name $var
13+
exists(int i, Cmd cmd |
14+
cmd = this.getCmd() and
15+
cmd.getElement(i) = this and
16+
result = cmd.getElement(i + 1)
17+
)
18+
}
19+
20+
Cmd getCmd() { result.getElement(_) = this }
921

1022
override string toString() { command_parameter(this, result) }
1123
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class IfStmt extends @if_statement, Stmt {
1313

1414
StmtBlock getThen(int i) { if_statement_clause(this, i, _, result) }
1515

16+
int getNumberOfConditions() { result = count(this.getACondition()) }
17+
18+
StmtBlock getAThen() { result = this.getThen(_) }
19+
1620
/** ..., if any. */
1721
StmtBlock getElse() { if_statement_else(this, result) }
1822

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import powershell
22

3-
class InvokeMemberExpression extends @invoke_member_expression, MemberExprBase {
3+
class InvokeMemberExpr extends @invoke_member_expression, MemberExprBase {
44
override SourceLocation getLocation() { invoke_member_expression_location(this, result) }
55

6-
Expr getExpression() { invoke_member_expression(this, result, _) }
6+
Expr getBase() { invoke_member_expression(this, result, _) }
77

88
CmdElement getMember() { invoke_member_expression(this, _, result) }
99

1010
Expr getArgument(int i) { invoke_member_expression_argument(this, i, result) }
1111

1212
Expr getAnArgument() { invoke_member_expression_argument(this, _, result) }
1313

14-
override string toString() { result = "ArrayExpression at: " + this.getLocation().toString() }
14+
override string toString() { result = "call to " + this.getMember() }
1515
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import powershell
22

3-
class NamedAttributeArgument extends @named_attribute_argument {
4-
string toString() { result = this.getValue().toString() }
3+
class NamedAttributeArgument extends @named_attribute_argument, Ast {
4+
final override string toString() { result = this.getValue().toString() }
55

6-
SourceLocation getLocation() { named_attribute_argument_location(this, result) }
6+
final override SourceLocation getLocation() { named_attribute_argument_location(this, result) }
77

88
string getName() { named_attribute_argument(this, result, _) }
99

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import powershell
22

3-
class StringConstExpression extends @string_constant_expression, BaseConstExpr {
3+
class StringConstExpr extends @string_constant_expression, BaseConstExpr {
44
StringLiteral getValue() { string_constant_expression(this, result) }
55

66
/** Get the full string literal with all its parts concatenated */
7-
override string toString() { result = getValue().toString() }
7+
override string toString() { result = this.getValue().toString() }
88

99
override SourceLocation getLocation() { string_constant_expression_location(this, result) }
1010
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ class StringLiteral extends @string_literal {
77

88
/** Get the full string literal with all its parts concatenated */
99
string toString() {
10-
result = concat(int i | i = [0 .. getNumContinuations()] | getContinuation(i), "\n")
10+
result = this.getValue()
11+
}
12+
13+
string getValue() {
14+
result = concat(int i | i = [0 .. this.getNumContinuations()] | this.getContinuation(i), "\n")
1115
}
1216

1317
SourceLocation getLocation() { string_literal_location(this, result) }

0 commit comments

Comments
 (0)