Skip to content

Commit d60d245

Browse files
committed
Swift: Add String.Index.init as a source as as well.
1 parent bc03f69 commit d60d245

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

swift/ql/src/queries/Security/CWE-135/StringLengthConflation.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class StringLengthConflationConfiguration extends DataFlow::Configuration {
102102
// `prefix`, `suffix`
103103
funcName = ["prefix(_:)", "suffix(_:)"] and
104104
paramName = "maxLength"
105+
or
106+
// `String.Index.init`
107+
funcName = "init(encodedOffset:)" and
108+
paramName = "offset"
105109
) and
106110
call.getFunction().(ApplyExpr).getStaticTarget().getName() = funcName and
107111
call.getFunction()

swift/ql/test/query-tests/Security/CWE-135/StringLengthConflation.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ edges
1313
| StringLengthConflation.swift:135:36:135:38 | .count : | StringLengthConflation.swift:135:36:135:46 | ... call to -(_:_:) ... |
1414
| StringLengthConflation.swift:141:28:141:30 | .count : | StringLengthConflation.swift:141:28:141:38 | ... call to -(_:_:) ... |
1515
nodes
16+
| StringLengthConflation.swift:53:43:53:46 | .length | semmle.label | .length |
1617
| StringLengthConflation.swift:72:33:72:35 | .count | semmle.label | .count |
1718
| StringLengthConflation.swift:78:47:78:49 | .count | semmle.label | .count |
1819
| StringLengthConflation.swift:93:28:93:31 | .length : | semmle.label | .length : |
@@ -43,6 +44,7 @@ nodes
4344
| StringLengthConflation.swift:141:28:141:38 | ... call to -(_:_:) ... | semmle.label | ... call to -(_:_:) ... |
4445
subpaths
4546
#select
47+
| StringLengthConflation.swift:53:43:53:46 | .length | StringLengthConflation.swift:53:43:53:46 | .length | StringLengthConflation.swift:53:43:53:46 | .length | This NSString length is used in a String, but it may not be equivalent. |
4648
| StringLengthConflation.swift:72:33:72:35 | .count | StringLengthConflation.swift:72:33:72:35 | .count | StringLengthConflation.swift:72:33:72:35 | .count | This String length is used in an NSString, but it may not be equivalent. |
4749
| StringLengthConflation.swift:78:47:78:49 | .count | StringLengthConflation.swift:78:47:78:49 | .count | StringLengthConflation.swift:78:47:78:49 | .count | This String length is used in an NSString, but it may not be equivalent. |
4850
| StringLengthConflation.swift:93:28:93:40 | ... call to -(_:_:) ... | StringLengthConflation.swift:93:28:93:31 | .length : | StringLengthConflation.swift:93:28:93:40 | ... call to -(_:_:) ... | This NSString length is used in a String, but it may not be equivalent. |

swift/ql/test/query-tests/Security/CWE-135/StringLengthConflation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func test(s: String) {
5050
// --- constructing a String.Index from integer ---
5151

5252
let ix1 = String.Index(encodedOffset: s.count) // GOOD
53-
let ix2 = String.Index(encodedOffset: ns.length) // BAD: NSString length used in String.Index [NOT DETECTED]
53+
let ix2 = String.Index(encodedOffset: ns.length) // BAD: NSString length used in String.Index
5454
let ix3 = String.Index(encodedOffset: s.utf8.count) // BAD: String.utf8 length used in String.Index [NOT DETECTED]
5555
let ix4 = String.Index(encodedOffset: s.utf16.count) // BAD: String.utf16 length used in String.Index [NOT DETECTED]
5656
let ix5 = String.Index(encodedOffset: s.unicodeScalars.count) // BAD: string.unicodeScalars length used in String.Index [NOT DETECTED]

0 commit comments

Comments
 (0)