Skip to content

Commit 021ed88

Browse files
committed
Swift: Add a test revealing unevaluated regexs.
1 parent e225ea6 commit 021ed88

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

swift/ql/test/library-tests/regex/parse.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6663,3 +6663,8 @@ regex.swift:
66636663

66646664
# 254| [RegExpStar] .*
66656665
#-----| 0 -> [RegExpDot] .
6666+
6667+
# 257| [RegExpDot] .
6668+
6669+
# 257| [RegExpStar] .*
6670+
#-----| 0 -> [RegExpDot] .

swift/ql/test/library-tests/regex/regex.ql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else
1010

1111
module RegexTest implements TestSig {
1212
string getARelevantTag() {
13-
result = ["regex", "input", "redos-vulnerable", "hasParseFailure", "modes"]
13+
result = ["regex", "unevaluated-regex", "input", "redos-vulnerable", "hasParseFailure", "modes"]
1414
}
1515

1616
predicate hasActualResult(Location location, string element, string tag, string value) {
@@ -47,6 +47,15 @@ module RegexTest implements TestSig {
4747
tag = "regex" and
4848
value = quote(regex.toString().replaceAll("\n", "NEWLINE"))
4949
)
50+
or
51+
exists(RegExp regex |
52+
// unevaluated regex
53+
not exists(RegexEval eval | eval.getARegex() = regex) and
54+
location = regex.getLocation() and
55+
element = regex.toString() and
56+
tag = "unevaluated-regex" and
57+
value = quote(regex.toString().replaceAll("\n", "NEWLINE"))
58+
)
5059
}
5160

5261
predicate hasOptionalResult(Location location, string element, string tag, string value) {

swift/ql/test/library-tests/regex/regex.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ func myRegexpMethodsTests(b: Bool, str_unknown: String) throws {
127127
// --- StringProtocol ---
128128

129129
_ = input.range(of: ".*", options: .regularExpression, range: nil, locale: nil) // $ regex=.* input=input
130-
_ = input.range(of: ".*", options: .literal, range: nil, locale: nil) // (not a regular expression)
130+
_ = input.range(of: ".*", options: .literal, range: nil, locale: nil) // $ SPURIOUS: unevaluated-regex=.* (not a regular expression)
131131
_ = input.replacingOccurrences(of: ".*", with: "", options: .regularExpression) // $ regex=.* input=input
132-
_ = input.replacingOccurrences(of: ".*", with: "", options: .literal) // (not a regular expression)
132+
_ = input.replacingOccurrences(of: ".*", with: "", options: .literal) // $ SPURIOUS: unevaluated-regex=.* (not a regular expression)
133133

134134
// --- NSRegularExpression ---
135135

@@ -151,9 +151,9 @@ func myRegexpMethodsTests(b: Bool, str_unknown: String) throws {
151151
_ = inputNS.range(of: ".*", options: [.regularExpression]) // $ regex=.* input=inputNS
152152
_ = inputNS.range(of: ".*", options: regexOptions) // $ regex=.* input=inputNS
153153
_ = inputNS.range(of: ".*", options: regexOptions2) // $ regex=.* input=inputNS modes=IGNORECASE
154-
_ = inputNS.range(of: ".*", options: .literal) // (not a regular expression)
154+
_ = inputNS.range(of: ".*", options: .literal) // $ SPURIOUS: unevaluated-regex=.* (not a regular expression)
155155
_ = inputNS.replacingOccurrences(of: ".*", with: "", options: .regularExpression, range: NSMakeRange(0, inputNS.length)) // $ regex=.* input=inputNS
156-
_ = inputNS.replacingOccurrences(of: ".*", with: "", options: .literal, range: NSMakeRange(0, inputNS.length)) // (not a regular expression)
156+
_ = inputNS.replacingOccurrences(of: ".*", with: "", options: .literal, range: NSMakeRange(0, inputNS.length)) // $ SPURIOUS: unevaluated-regex=.* (not a regular expression)
157157

158158
// --- flow ---
159159

@@ -252,4 +252,7 @@ func myRegexpMethodsTests(b: Bool, str_unknown: String) throws {
252252
_ = input.replacingOccurrences(of: ".*", with: "", options: myOptions2) // $ regex=.* input=input modes=IGNORECASE
253253
_ = NSString(string: "abc").replacingOccurrences(of: ".*", with: "", options: [.regularExpression, .caseInsensitive], range: NSMakeRange(0, inputNS.length)) // $ regex=.* input="call to NSString.init(string:)" modes=IGNORECASE
254254
_ = NSString(string: "abc").replacingOccurrences(of: ".*", with: "", options: myOptions2, range: NSMakeRange(0, inputNS.length)) // $ regex=.* input="call to NSString.init(string:)" modes=IGNORECASE
255+
256+
// Regex created but never evaluated
257+
_ = try Regex(".*") // $ unevaluated-regex=.*
255258
}

0 commit comments

Comments
 (0)