Skip to content

Commit f8b8c67

Browse files
committed
Swift: Clean up and autoformat.
1 parent 3c1f755 commit f8b8c67

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

swift/ql/lib/codeql/swift/regex/Regex.qll

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,19 @@ newtype TRegexParseMode =
6868
MkUnicode() // Unicode UAX 29 word boundary mode
6969

7070
class RegexParseMode extends TRegexParseMode {
71-
string toString() {
72-
(this = MkIgnoreCase() and result = "IGNORECASE") or
73-
(this = MkVerbose() and result = "VERBOSE") or
74-
(this = MkDotAll() and result = "DOTALL") or
75-
(this = MkMultiLine() and result = "MULTILINE") or
76-
(this = MkUnicode() and result = "UNICODE")
71+
string getName() {
72+
this = MkIgnoreCase() and result = "IGNORECASE"
73+
or
74+
this = MkVerbose() and result = "VERBOSE"
75+
or
76+
this = MkDotAll() and result = "DOTALL"
77+
or
78+
this = MkMultiLine() and result = "MULTILINE"
79+
or
80+
this = MkUnicode() and result = "UNICODE"
7781
}
82+
83+
string toString() { result = this.getName() }
7884
}
7985

8086
/**
@@ -91,7 +97,9 @@ class RegexAdditionalFlowStep extends Unit {
9197
* Holds if the step from `node1` to `node2` either sets (`isSet` = true)
9298
* or unsets (`isSet` = false) parse mode `mode` for the regular expression.
9399
*/
94-
abstract predicate modifiesParseMode(DataFlow::Node nodeFrom, DataFlow::Node nodeTo, RegexParseMode mode, boolean isSet);
100+
abstract predicate modifiesParseMode(
101+
DataFlow::Node nodeFrom, DataFlow::Node nodeTo, RegexParseMode mode, boolean isSet
102+
);
95103
}
96104

97105
/**
@@ -102,29 +110,27 @@ class StandardRegexAdditionalFlowStep extends RegexAdditionalFlowStep {
102110
this.modifiesParseMode(nodeFrom, nodeTo, _, _)
103111
}
104112

105-
override predicate modifiesParseMode(DataFlow::Node nodeFrom, DataFlow::Node nodeTo, RegexParseMode mode, boolean isSet)
106-
{
113+
override predicate modifiesParseMode(
114+
DataFlow::Node nodeFrom, DataFlow::Node nodeTo, RegexParseMode mode, boolean isSet
115+
) {
107116
exists(CallExpr ce |
108117
nodeFrom.asExpr() = ce.getQualifier() and
109118
nodeTo.asExpr() = ce and
110119
// decode the parse mode being set
111120
(
112-
(
113-
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "ignoresCase(_:)") and
114-
mode = MkIgnoreCase()
115-
) or (
116-
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "dotMatchesNewlines(_:)") and
117-
mode = MkDotAll()
118-
) or (
119-
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "anchorsMatchLineEndings(_:)") and
120-
mode = MkMultiLine()
121-
)
121+
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "ignoresCase(_:)") and
122+
mode = MkIgnoreCase()
123+
or
124+
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "dotMatchesNewlines(_:)") and
125+
mode = MkDotAll()
126+
or
127+
ce.getStaticTarget().(Method).hasQualifiedName("Regex", "anchorsMatchLineEndings(_:)") and
128+
mode = MkMultiLine()
122129
) and
123130
// decode the value being set
124-
if ce.getArgument(0).getExpr().(BooleanLiteralExpr).getValue() = false then
125-
isSet = false // mode is set to false
126-
else
127-
isSet = true // mode is set to true OR mode is set to default (=true) OR mode is set to an unknown value
131+
if ce.getArgument(0).getExpr().(BooleanLiteralExpr).getValue() = false
132+
then isSet = false // mode is set to false
133+
else isSet = true // mode is set to true OR mode is set to default (=true) OR mode is set to an unknown value
128134
)
129135
}
130136
}
@@ -168,9 +174,8 @@ abstract class RegexEval extends CallExpr {
168174
RegexParseMode getAParseMode() {
169175
exists(DataFlow::Node setNode |
170176
// parse mode flag is set
171-
any(RegexAdditionalFlowStep s).modifiesParseMode(_, setNode, result, true)
172-
and
173-
// reaches here
177+
any(RegexAdditionalFlowStep s).modifiesParseMode(_, setNode, result, true) and
178+
// reaches this eval
174179
RegexParseModeFlow::flow(setNode, DataFlow::exprNode(this.getRegexInput()))
175180
)
176181
}

swift/ql/lib/codeql/swift/regex/internal/ParseRegex.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ abstract class RegExp extends Expr {
319319
}
320320

321321
/**
322-
* Gets a mode (if any) of this regular expression. Can be any of:
322+
* Gets a mode (if any) of this regular expression in any evaluation. Can be
323+
* any of:
323324
* IGNORECASE
324325
* VERBOSE
325326
* DOTALL
@@ -333,7 +334,7 @@ abstract class RegExp extends Expr {
333334
// mode flags applied to the regex object before evaluation
334335
exists(RegexEval e |
335336
e.getARegex() = this and
336-
result = e.getAParseMode().toString() // TODO: temp toString()
337+
result = e.getAParseMode().getName()
337338
)
338339
}
339340

swift/ql/lib/codeql/swift/regex/internal/RegexTracking.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,11 @@ private module RegexParseModeConfig implements DataFlow::StateConfigSig {
6666

6767
predicate isSink(DataFlow::Node node, FlowState flowstate) {
6868
// evaluation of the regex
69-
node.asExpr() = any(RegexEval eval).getRegexInput()
70-
and
69+
node.asExpr() = any(RegexEval eval).getRegexInput() and
7170
flowstate = any(FlowState fs)
7271
}
7372

74-
predicate isBarrier(DataFlow::Node node) {
75-
none()
76-
}
73+
predicate isBarrier(DataFlow::Node node) { none() }
7774

7875
predicate isBarrier(DataFlow::Node node, FlowState flowstate) {
7976
// parse mode flag is set or unset

0 commit comments

Comments
 (0)