Skip to content

Commit 39fb714

Browse files
committed
Swift: Add test with substring declared differently.
1 parent eebba36 commit 39fb714

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
edges
2+
| StringLengthConflation2.swift:37:34:37:36 | .count : | StringLengthConflation2.swift:37:34:37:44 | ... call to -(_:_:) ... |
23
| StringLengthConflation.swift:60:47:60:50 | .length : | StringLengthConflation.swift:60:47:60:59 | ... call to /(_:_:) ... |
34
| StringLengthConflation.swift:66:33:66:36 | .length : | StringLengthConflation.swift:66:33:66:45 | ... call to /(_:_:) ... |
45
| StringLengthConflation.swift:93:28:93:31 | .length : | StringLengthConflation.swift:93:28:93:40 | ... call to -(_:_:) ... |
@@ -15,6 +16,8 @@ edges
1516
| StringLengthConflation.swift:135:36:135:38 | .count : | StringLengthConflation.swift:135:36:135:46 | ... call to -(_:_:) ... |
1617
| StringLengthConflation.swift:141:28:141:30 | .count : | StringLengthConflation.swift:141:28:141:38 | ... call to -(_:_:) ... |
1718
nodes
19+
| StringLengthConflation2.swift:37:34:37:36 | .count : | semmle.label | .count : |
20+
| StringLengthConflation2.swift:37:34:37:44 | ... call to -(_:_:) ... | semmle.label | ... call to -(_:_:) ... |
1821
| StringLengthConflation.swift:53:43:53:46 | .length | semmle.label | .length |
1922
| StringLengthConflation.swift:60:47:60:50 | .length : | semmle.label | .length : |
2023
| StringLengthConflation.swift:60:47:60:59 | ... call to /(_:_:) ... | semmle.label | ... call to /(_:_:) ... |
@@ -50,6 +53,7 @@ nodes
5053
| StringLengthConflation.swift:141:28:141:38 | ... call to -(_:_:) ... | semmle.label | ... call to -(_:_:) ... |
5154
subpaths
5255
#select
56+
| StringLengthConflation2.swift:37:34:37:44 | ... call to -(_:_:) ... | StringLengthConflation2.swift:37:34:37:36 | .count : | StringLengthConflation2.swift:37:34:37:44 | ... call to -(_:_:) ... | This String length is used in an NSString, but it may not be equivalent. |
5357
| 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. |
5458
| StringLengthConflation.swift:60:47:60:59 | ... call to /(_:_:) ... | StringLengthConflation.swift:60:47:60:50 | .length : | StringLengthConflation.swift:60:47:60:59 | ... call to /(_:_:) ... | This NSString length is used in a String, but it may not be equivalent. |
5559
| StringLengthConflation.swift:66:33:66:45 | ... call to /(_:_:) ... | StringLengthConflation.swift:66:33:66:36 | .length : | StringLengthConflation.swift:66:33:66:45 | ... call to /(_:_:) ... | This NSString length is used in a String, but it may not be equivalent. |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// this test is in a separate file, because we want to test with a slightly different library
2+
// implementation. In this version, some of the functions of `NSString` are in fact implemented
3+
// in a base class `NSStringBase`.
4+
5+
// --- stubs ---
6+
7+
func print(_ items: Any...) {}
8+
9+
typealias unichar = UInt16
10+
11+
class NSObject
12+
{
13+
}
14+
15+
class NSStringBase : NSObject
16+
{
17+
func substring(from: Int) -> String { return "" }
18+
}
19+
20+
class NSString : NSStringBase
21+
{
22+
init(string: String) { length = string.count }
23+
24+
func substring(to: Int) -> String { return "" }
25+
26+
private(set) var length: Int
27+
}
28+
29+
// --- tests ---
30+
31+
func test(s: String) {
32+
let ns = NSString(string: s)
33+
34+
let nstr1 = ns.substring(from: ns.length - 1) // GOOD
35+
let nstr2 = ns.substring(from: s.count - 1) // BAD: String length used in NSString [NOT DETECTED]
36+
let nstr3 = ns.substring(to: ns.length - 1) // GOOD
37+
let nstr4 = ns.substring(to: s.count - 1) // BAD: String length used in NSString
38+
print("substrings '\(nstr1)' '\(nstr2)' / '\(nstr3)' '\(nstr4)'")
39+
}
40+
41+
// `begin :thumbsup: end`, with thumbs up emoji and skin tone modifier
42+
test(s: "begin \u{0001F44D}\u{0001F3FF} end")

0 commit comments

Comments
 (0)