Skip to content

Commit f503456

Browse files
committed
Swift: Fix parse failures due to mode flags.
1 parent a7a609d commit f503456

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ abstract class RegExp extends Expr {
708708
this.commentGroupStart(start, end)
709709
or
710710
this.simpleGroupStart(start, end)
711+
or
712+
this.flagGroupStart(start, end, _)
711713
}
712714

713715
/** Matches the start of a non-capturing group, e.g. `(?:` */

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,12 @@ redos_variants.swift:
16181618

16191619
# 142| [RegExpConstant, RegExpNormalChar] !
16201620

1621-
# 146| [RegExpConstant, RegExpNormalChar] s
1621+
# 146| [RegExpZeroWidthMatch] (?s)
1622+
1623+
# 146| [RegExpSequence] (?s)(.|\n)*!
1624+
#-----| 0 -> [RegExpZeroWidthMatch] (?s)
1625+
#-----| 1 -> [RegExpStar] (.|\n)*
1626+
#-----| 2 -> [RegExpConstant, RegExpNormalChar] !
16221627

16231628
# 146| [RegExpGroup] (.|\n)
16241629
#-----| 0 -> [RegExpAlt] .|\n
@@ -6484,15 +6489,30 @@ regex.swift:
64846489

64856490
# 205| [RegExpNamedCharacterProperty] [:aaaaa:]
64866491

6487-
# 209| [RegExpConstant, RegExpNormalChar] i
6492+
# 209| [RegExpZeroWidthMatch] (?i)
6493+
6494+
# 209| [RegExpSequence] (?i)abc
6495+
#-----| 0 -> [RegExpZeroWidthMatch] (?i)
6496+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] abc
64886497

64896498
# 209| [RegExpConstant, RegExpNormalChar] abc
64906499

6491-
# 210| [RegExpConstant, RegExpNormalChar] s
6500+
# 210| [RegExpZeroWidthMatch] (?s)
6501+
6502+
# 210| [RegExpSequence] (?s)abc
6503+
#-----| 0 -> [RegExpZeroWidthMatch] (?s)
6504+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] abc
64926505

64936506
# 210| [RegExpConstant, RegExpNormalChar] abc
64946507

6495-
# 211| [RegExpConstant, RegExpNormalChar] is
6508+
# 211| [RegExpGroup] (?is)
6509+
#-----| 0 -> [RegExpConstant, RegExpNormalChar] s
6510+
6511+
# 211| [RegExpSequence] (?is)abc
6512+
#-----| 0 -> [RegExpGroup] (?is)
6513+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] abc
6514+
6515+
# 211| [RegExpConstant, RegExpNormalChar] s
64966516

64976517
# 211| [RegExpConstant, RegExpNormalChar] abc
64986518

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func myRegexpVariantsTests(myUrl: URL) throws {
143143

144144
// BAD
145145
// attack string: "\n".repeat(100) + "."
146-
_ = try Regex(#"(?s)(.|\n)*!"#).firstMatch(in: tainted) // $ hasParseFailure modes=DOTALL MISSING: redos-vulnerable=
146+
_ = try Regex(#"(?s)(.|\n)*!"#).firstMatch(in: tainted) // $ modes=DOTALL redos-vulnerable=
147147

148148
// GOOD
149149
_ = try Regex(#"([\w.]+)*"#).firstMatch(in: tainted)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ func myRegexpMethodsTests(b: Bool, str_unknown: String) throws {
206206

207207
// --- parse modes ---
208208

209-
_ = try Regex("(?i)abc").firstMatch(in: input) // $ input=input modes=IGNORECASE regex=(?i)abc SPURIOUS: hasParseFailure
210-
_ = try Regex("(?s)abc").firstMatch(in: input) // $ input=input modes=DOTALL regex=(?s)abc SPURIOUS: $hasParseFailure
211-
_ = try Regex("(?is)abc").firstMatch(in: input) // $ input=input regex=(?is)abc MISSING: modes="DOTALL | IGNORECASE" SPURIOUS: modes=IGNORECASE $hasParseFailure
209+
_ = try Regex("(?i)abc").firstMatch(in: input) // $ input=input modes=IGNORECASE regex=(?i)abc
210+
_ = try Regex("(?s)abc").firstMatch(in: input) // $ input=input modes=DOTALL regex=(?s)abc
211+
_ = try Regex("(?is)abc").firstMatch(in: input) // $ input=input regex=(?is)abc MISSING: modes="DOTALL | IGNORECASE" SPURIOUS: modes=IGNORECASE
212212

213213
_ = try Regex("abc").dotMatchesNewlines(true).firstMatch(in: input) // $ input=input regex=abc MISSING: modes=DOTALL
214214
_ = try Regex("abc").dotMatchesNewlines(false).firstMatch(in: input) // $ input=input regex=abc

0 commit comments

Comments
 (0)