Skip to content

Commit 3bf3876

Browse files
committed
Swift: fix FPs with inout params in inits
1 parent 94d80b0 commit 3bf3876

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,10 @@ private module ReturnNodes {
404404
* implicitly returned as the newly-constructed object
405405
* */
406406
class SelfReturnNode extends InoutReturnNodeImpl {
407-
SelfReturnNode() { exit.getScope() instanceof ConstructorDecl }
407+
SelfReturnNode() {
408+
exit.getScope() instanceof ConstructorDecl and
409+
param instanceof SelfParamDecl
410+
}
408411

409412
override ReturnKind getKind() { result instanceof NormalReturnKind }
410413
}

swift/ql/test/library-tests/dataflow/dataflow/DataFlow.expected

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,9 @@ edges
158158
| test.swift:479:14:479:21 | call to source() : | test.swift:479:13:479:21 | call to +(_:) |
159159
| test.swift:488:9:488:9 | self [str] : | file://:0:0:0:0 | self [str] : |
160160
| test.swift:489:10:489:13 | s : | test.swift:490:13:490:13 | s : |
161-
| test.swift:490:7:490:7 | [post] self [str] : | test.swift:489:5:491:5 | self[constructor return] [str] : |
162161
| test.swift:490:7:490:7 | [post] self [str] : | test.swift:489:5:491:5 | self[return] [str] : |
163162
| test.swift:490:13:490:13 | s : | test.swift:490:7:490:7 | [post] self [str] : |
164-
| test.swift:495:17:498:5 | self[constructor return] [str] : | test.swift:503:13:503:41 | call to Self.init(contentsOfFile:) [str] : |
165163
| test.swift:495:17:498:5 | self[return] [str] : | test.swift:503:13:503:41 | call to Self.init(contentsOfFile:) [str] : |
166-
| test.swift:496:7:496:7 | [post] self [str] : | test.swift:495:17:498:5 | self[constructor return] [str] : |
167164
| test.swift:496:7:496:7 | [post] self [str] : | test.swift:495:17:498:5 | self[return] [str] : |
168165
| test.swift:496:7:496:7 | [post] self [str] : | test.swift:497:17:497:17 | self [str] : |
169166
| test.swift:496:20:496:28 | call to source3() : | test.swift:489:10:489:13 | s : |
@@ -351,16 +348,10 @@ nodes
351348
| test.swift:479:14:479:21 | call to source() : | semmle.label | call to source() : |
352349
| test.swift:480:14:480:21 | call to source() | semmle.label | call to source() |
353350
| test.swift:488:9:488:9 | self [str] : | semmle.label | self [str] : |
354-
| test.swift:489:5:491:5 | self[constructor return] [str] : | semmle.label | self[constructor return] [str] : |
355-
| test.swift:489:5:491:5 | self[constructor return] [str] : | semmle.label | self[return] [str] : |
356-
| test.swift:489:5:491:5 | self[return] [str] : | semmle.label | self[constructor return] [str] : |
357351
| test.swift:489:5:491:5 | self[return] [str] : | semmle.label | self[return] [str] : |
358352
| test.swift:489:10:489:13 | s : | semmle.label | s : |
359353
| test.swift:490:7:490:7 | [post] self [str] : | semmle.label | [post] self [str] : |
360354
| test.swift:490:13:490:13 | s : | semmle.label | s : |
361-
| test.swift:495:17:498:5 | self[constructor return] [str] : | semmle.label | self[constructor return] [str] : |
362-
| test.swift:495:17:498:5 | self[constructor return] [str] : | semmle.label | self[return] [str] : |
363-
| test.swift:495:17:498:5 | self[return] [str] : | semmle.label | self[constructor return] [str] : |
364355
| test.swift:495:17:498:5 | self[return] [str] : | semmle.label | self[return] [str] : |
365356
| test.swift:496:7:496:7 | [post] self [str] : | semmle.label | [post] self [str] : |
366357
| test.swift:496:20:496:28 | call to source3() : | semmle.label | call to source3() : |
@@ -403,11 +394,8 @@ subpaths
403394
| test.swift:303:15:303:16 | ...! : | file://:0:0:0:0 | [summary param] this in signum() : | file://:0:0:0:0 | [summary] to write: return (return) in signum() : | test.swift:303:15:303:25 | call to signum() |
404395
| test.swift:468:12:468:12 | x : | test.swift:462:9:462:9 | value : | file://:0:0:0:0 | [post] self [x] : | test.swift:468:5:468:5 | [post] cx [x] : |
405396
| test.swift:472:20:472:20 | cx [x] : | test.swift:462:9:462:9 | self [x] : | file://:0:0:0:0 | .x : | test.swift:472:20:472:23 | .x : |
406-
| test.swift:496:20:496:28 | call to source3() : | test.swift:489:10:489:13 | s : | test.swift:489:5:491:5 | self[constructor return] [str] : | test.swift:496:7:496:7 | [post] self [str] : |
407-
| test.swift:496:20:496:28 | call to source3() : | test.swift:489:10:489:13 | s : | test.swift:489:5:491:5 | self[return] [str] : | test.swift:496:7:496:7 | [post] self [str] : |
408397
| test.swift:496:20:496:28 | call to source3() : | test.swift:489:10:489:13 | s : | test.swift:490:7:490:7 | [post] self [str] : | test.swift:496:7:496:7 | [post] self [str] : |
409398
| test.swift:502:13:502:33 | call to MyClass.init(s:) [str] : | test.swift:488:9:488:9 | self [str] : | file://:0:0:0:0 | .str : | test.swift:502:13:502:35 | .str |
410-
| test.swift:502:24:502:32 | call to source3() : | test.swift:489:10:489:13 | s : | test.swift:489:5:491:5 | self[constructor return] [str] : | test.swift:502:13:502:33 | call to MyClass.init(s:) [str] : |
411399
| test.swift:502:24:502:32 | call to source3() : | test.swift:489:10:489:13 | s : | test.swift:489:5:491:5 | self[return] [str] : | test.swift:502:13:502:33 | call to MyClass.init(s:) [str] : |
412400
| test.swift:503:13:503:41 | call to Self.init(contentsOfFile:) [str] : | test.swift:488:9:488:9 | self [str] : | file://:0:0:0:0 | .str : | test.swift:503:13:503:43 | .str |
413401
#select

swift/ql/test/library-tests/dataflow/dataflow/LocalFlow.expected

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@
115115
| test.swift:163:7:163:7 | value | test.swift:163:7:163:7 | SSA def(value) |
116116
| test.swift:165:3:165:3 | SSA def(self) | test.swift:166:5:166:5 | self |
117117
| test.swift:165:3:165:3 | self | test.swift:165:3:165:3 | SSA def(self) |
118-
| test.swift:166:5:166:5 | [post] self | test.swift:165:3:167:3 | self[constructor return] |
119118
| test.swift:166:5:166:5 | [post] self | test.swift:165:3:167:3 | self[return] |
120-
| test.swift:166:5:166:5 | self | test.swift:165:3:167:3 | self[constructor return] |
121119
| test.swift:166:5:166:5 | self | test.swift:165:3:167:3 | self[return] |
122120
| test.swift:169:8:169:8 | SSA def(self) | test.swift:170:5:170:5 | self |
123121
| test.swift:169:8:169:8 | self | test.swift:169:8:169:8 | SSA def(self) |
@@ -141,9 +139,7 @@
141139
| test.swift:185:7:185:7 | value | test.swift:185:7:185:7 | SSA def(value) |
142140
| test.swift:187:3:187:3 | SSA def(self) | test.swift:188:5:188:5 | self |
143141
| test.swift:187:3:187:3 | self | test.swift:187:3:187:3 | SSA def(self) |
144-
| test.swift:188:5:188:5 | [post] self | test.swift:187:3:189:3 | self[constructor return] |
145142
| test.swift:188:5:188:5 | [post] self | test.swift:187:3:189:3 | self[return] |
146-
| test.swift:188:5:188:5 | self | test.swift:187:3:189:3 | self[constructor return] |
147143
| test.swift:188:5:188:5 | self | test.swift:187:3:189:3 | self[return] |
148144
| test.swift:193:7:193:7 | SSA def(b) | test.swift:194:3:194:3 | b |
149145
| test.swift:193:11:193:13 | call to B.init() | test.swift:193:7:193:7 | SSA def(b) |
@@ -165,7 +161,6 @@
165161
| test.swift:217:11:217:13 | call to B.init() | test.swift:217:7:217:7 | SSA def(b) |
166162
| test.swift:218:3:218:3 | [post] b | test.swift:219:13:219:13 | b |
167163
| test.swift:218:3:218:3 | b | test.swift:219:13:219:13 | b |
168-
| test.swift:222:7:222:7 | SSA def(self) | test.swift:222:7:222:7 | self[constructor return] |
169164
| test.swift:222:7:222:7 | SSA def(self) | test.swift:222:7:222:7 | self[return] |
170165
| test.swift:222:7:222:7 | SSA def(self) | test.swift:222:7:222:7 | self[return] |
171166
| test.swift:222:7:222:7 | self | test.swift:222:7:222:7 | SSA def(self) |
@@ -191,9 +186,7 @@
191186
| test.swift:243:18:243:18 | self | test.swift:243:9:243:42 | self[return] |
192187
| test.swift:246:5:246:5 | SSA def(self) | test.swift:247:9:247:9 | self |
193188
| test.swift:246:5:246:5 | self | test.swift:246:5:246:5 | SSA def(self) |
194-
| test.swift:247:9:247:9 | [post] self | test.swift:246:5:248:5 | self[constructor return] |
195189
| test.swift:247:9:247:9 | [post] self | test.swift:246:5:248:5 | self[return] |
196-
| test.swift:247:9:247:9 | self | test.swift:246:5:248:5 | self[constructor return] |
197190
| test.swift:247:9:247:9 | self | test.swift:246:5:248:5 | self[return] |
198191
| test.swift:252:23:252:23 | value | test.swift:252:23:252:23 | SSA def(value) |
199192
| test.swift:262:21:262:27 | SSA def(y) | test.swift:266:15:266:15 | y |
@@ -373,7 +366,6 @@
373366
| test.swift:448:10:448:37 | SSA def(b) | test.swift:450:19:450:19 | b |
374367
| test.swift:455:8:455:17 | SSA def(x) | test.swift:456:19:456:19 | x |
375368
| test.swift:455:8:455:17 | SSA def(y) | test.swift:457:19:457:19 | y |
376-
| test.swift:461:7:461:7 | SSA def(self) | test.swift:461:7:461:7 | self[constructor return] |
377369
| test.swift:461:7:461:7 | SSA def(self) | test.swift:461:7:461:7 | self[return] |
378370
| test.swift:461:7:461:7 | SSA def(self) | test.swift:461:7:461:7 | self[return] |
379371
| test.swift:461:7:461:7 | self | test.swift:461:7:461:7 | SSA def(self) |
@@ -409,17 +401,22 @@
409401
| test.swift:489:5:489:5 | self | test.swift:489:5:489:5 | SSA def(self) |
410402
| test.swift:489:10:489:13 | SSA def(s) | test.swift:490:13:490:13 | s |
411403
| test.swift:489:10:489:13 | s | test.swift:489:10:489:13 | SSA def(s) |
412-
| test.swift:490:7:490:7 | [post] self | test.swift:489:5:491:5 | self[constructor return] |
413404
| test.swift:490:7:490:7 | [post] self | test.swift:489:5:491:5 | self[return] |
414-
| test.swift:490:7:490:7 | self | test.swift:489:5:491:5 | self[constructor return] |
415405
| test.swift:490:7:490:7 | self | test.swift:489:5:491:5 | self[return] |
416406
| test.swift:495:17:495:17 | SSA def(self) | test.swift:496:7:496:7 | self |
417407
| test.swift:495:17:495:17 | self | test.swift:495:17:495:17 | SSA def(self) |
418408
| test.swift:496:7:496:7 | [post] self | test.swift:497:17:497:17 | self |
419409
| test.swift:496:7:496:7 | self | test.swift:497:17:497:17 | self |
420-
| test.swift:497:17:497:17 | [post] self | test.swift:495:17:498:5 | self[constructor return] |
421410
| test.swift:497:17:497:17 | [post] self | test.swift:495:17:498:5 | self[return] |
422-
| test.swift:497:17:497:17 | self | test.swift:495:17:498:5 | self[constructor return] |
423411
| test.swift:497:17:497:17 | self | test.swift:495:17:498:5 | self[return] |
424412
| test.swift:501:21:501:27 | SSA def(path) | test.swift:503:37:503:37 | path |
425413
| test.swift:501:21:501:27 | path | test.swift:501:21:501:27 | SSA def(path) |
414+
| test.swift:506:7:506:7 | SSA def(self) | test.swift:506:7:506:7 | self[return] |
415+
| test.swift:506:7:506:7 | self | test.swift:506:7:506:7 | SSA def(self) |
416+
| test.swift:507:3:507:3 | SSA def(self) | test.swift:507:3:507:40 | self[return] |
417+
| test.swift:507:3:507:3 | self | test.swift:507:3:507:3 | SSA def(self) |
418+
| test.swift:507:27:507:38 | SSA def(n) | test.swift:507:3:507:40 | n[return] |
419+
| test.swift:507:31:507:38 | call to source() | test.swift:507:27:507:38 | SSA def(n) |
420+
| test.swift:513:7:513:7 | SSA def(n) | test.swift:514:36:514:36 | n |
421+
| test.swift:513:11:513:11 | 0 | test.swift:513:7:513:7 | SSA def(n) |
422+
| test.swift:514:36:514:36 | n | test.swift:514:35:514:36 | &... |

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,14 @@ func extensionInits(path: String) {
502502
sink(str: MyClass(s: source3()).str) // $ flow=502
503503
sink(str: MyClass(contentsOfFile: path).str) // $ flow=496
504504
}
505+
506+
class InoutConstructorClass {
507+
init(_ n : inout Int) { n = source() }
508+
}
509+
510+
func sink(arg: InoutConstructorClass) {}
511+
512+
func inoutConstructor() {
513+
var n = 0
514+
sink(arg: InoutConstructorClass(&n))
515+
}

0 commit comments

Comments
 (0)