Swift: add and accept a few new simple test cases

This commit is contained in:
Nora Dimitrijević
2023-09-14 16:46:23 +02:00
committed by Mathias Vorreiter Pedersen
parent af49a3aa64
commit 5418d39a0d
2 changed files with 26 additions and 7 deletions

View File

@@ -42,12 +42,12 @@ import codeql.swift.dataflow.TaintTracking
import TestUtilities.InlineExpectationsTest
private predicate defaultSource(DataFlow::Node source) {
source.asExpr().(MethodCallExpr).getStaticTarget().getShortName() = ["source", "taint"]
source.asExpr().(CallExpr).getStaticTarget().(Function).getShortName() = ["source", "taint"]
}
private predicate defaultSink(DataFlow::Node sink) {
exists(MethodCallExpr ma | ma.getStaticTarget().getShortName() = "sink" |
sink.asExpr() = ma.getAnArgument().getExpr()
exists(CallExpr ca | ca.getStaticTarget().(Function).getShortName() = "sink" |
sink.asExpr() = ca.getAnArgument().getExpr()
)
}
@@ -67,7 +67,7 @@ private module NoFlowConfig implements DataFlow::ConfigSig {
private string getSourceArgString(DataFlow::Node src) {
defaultSource(src) and
src.asExpr().(MethodCallExpr).getAnArgument().getExpr().(StringLiteralExpr).getValue() = result
src.asExpr().(CallExpr).getAnArgument().getExpr().(StringLiteralExpr).getValue() = result
}
module FlowTest<DataFlow::ConfigSig ValueFlowConfig, DataFlow::ConfigSig TaintFlowConfig> {

View File

@@ -18,7 +18,7 @@ func captureList() {
var escape: (() -> Int)? = nil
func setEscape() {
var x = source("setEscape", 0)
let x = source("setEscape", 0)
escape = {
sink(x) // $ MISSING: hasValueFlow=setEscape
return x + 1
@@ -31,11 +31,15 @@ func callEscape() {
}
func logical() -> Bool {
let f: ((Int) -> Int)? = { x in x + 1 }
let f: ((Int) -> Int)? = { x in
sink(x) // $ hasValueFlow=logical
return x + 1
}
let x: Int? = source("logical", 42)
return f != nil
&& (x != nil
&& f!(x!) == 43) // $ MISSING: hasValueFlow=logical
&& f!(x!) == 43)
}
func asyncTest() {
@@ -121,6 +125,21 @@ func sharedCaptureMultipleWriters() {
callSink2()
}
func taintCollections(array: inout Array<Int>) {
array[0] = source("array", 0)
sink(array)
sink(array[0]) // $ hasValueFlow=array
array.withContiguousStorageIfAvailable({
buffer in
sink(array)
sink(array[0]) // $ hasValueFlow=array
})
}
func simplestTest() {
let x = source("simplestTest", 0)
sink(x) // $ hasValueFlow=simplestTest
}
func main() {
print("captureList():")