Skip to content

Commit 992b3c7

Browse files
committed
Add data-flow tests for aliasing
1 parent c7e3682 commit 992b3c7

File tree

11 files changed

+101
-19
lines changed

11 files changed

+101
-19
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package test
2+
3+
import (
4+
"test.com/basename/pkg1"
5+
"test.com/basename/pkg2"
6+
)
7+
8+
// pkg2.IntStruct is an alias for pkg1.IntStruct
9+
// Note trickery with packages is necessary so that Go will assign the fields the same name as well as the same type.
10+
11+
type EmbedsPkg1IntStruct = struct{ pkg1.IntStruct }
12+
type EmbedsPkg2IntStruct = struct{ pkg2.IntStruct }
13+
14+
func FEmbedded() {
15+
16+
x := source()
17+
pkg1Struct := EmbedsPkg1IntStruct{pkg1.IntStruct{x}}
18+
19+
GEmbedded(&pkg1Struct)
20+
21+
}
22+
23+
func GEmbedded(pkg2Struct *EmbedsPkg2IntStruct) {
24+
25+
sink(pkg2Struct.Field) // $ hasValueFlow="selection of Field"
26+
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module test.com/basename
2+
3+
go 1.22.5
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pkg1
2+
3+
type IntStruct struct {
4+
Field int
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg2
2+
3+
import (
4+
"test.com/basename/pkg1"
5+
)
6+
7+
type IntStruct = pkg1.IntStruct
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test
2+
3+
func source() int { return 0 }
4+
func sink(value int) { }
5+
6+
type IntAlias = int
7+
type IntStruct = struct{ field int }
8+
type IntAliasStruct = struct{ field IntAlias }
9+
10+
func F() {
11+
12+
x := source()
13+
intStruct := IntStruct{x}
14+
15+
G(&intStruct)
16+
17+
}
18+
19+
func G(intAliasStruct *IntAliasStruct) {
20+
21+
sink(intAliasStruct.field) // $ hasValueFlow="selection of field"
22+
23+
}

go/ql/test/library-tests/semmle/go/aliases/DataflowFields/test.expected

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import go
2+
import TestUtilities.InlineFlowTest
3+
import DefaultFlowTest

go/ql/test/library-tests/semmle/go/aliases/InterfaceImpls/flow.expected

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import go
2+
import TestUtilities.InlineFlowTest
3+
import DefaultFlowTest
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
callTargets
2-
| test.go:48:2:48:24 | call to ImplementMe | test.go:12:1:12:69 | function declaration | ImplementMe |
3-
| test.go:48:2:48:24 | call to ImplementMe | test.go:17:1:17:64 | function declaration | ImplementMe |
4-
| test.go:48:2:48:24 | call to ImplementMe | test.go:24:1:24:53 | function declaration | ImplementMe |
5-
| test.go:48:2:48:24 | call to ImplementMe | test.go:31:1:31:59 | function declaration | ImplementMe |
6-
| test.go:48:2:48:24 | call to ImplementMe | test.go:38:1:38:71 | function declaration | ImplementMe |
7-
| test.go:48:2:48:24 | call to ImplementMe | test.go:45:1:45:69 | function declaration | ImplementMe |
2+
| test.go:15:70:15:83 | call to sink | test.go:10:1:10:46 | function declaration | sink |
3+
| test.go:20:65:20:78 | call to sink | test.go:10:1:10:46 | function declaration | sink |
4+
| test.go:27:54:27:67 | call to sink | test.go:10:1:10:46 | function declaration | sink |
5+
| test.go:34:60:34:73 | call to sink | test.go:10:1:10:46 | function declaration | sink |
6+
| test.go:41:72:41:85 | call to sink | test.go:10:1:10:46 | function declaration | sink |
7+
| test.go:48:70:48:83 | call to sink | test.go:10:1:10:46 | function declaration | sink |
8+
| test.go:51:14:51:21 | call to source | test.go:9:1:9:55 | function declaration | source |
9+
| test.go:52:2:52:29 | call to ImplementMe | test.go:15:1:15:85 | function declaration | ImplementMe |
10+
| test.go:52:2:52:29 | call to ImplementMe | test.go:20:1:20:80 | function declaration | ImplementMe |
11+
| test.go:52:2:52:29 | call to ImplementMe | test.go:27:1:27:69 | function declaration | ImplementMe |
12+
| test.go:52:2:52:29 | call to ImplementMe | test.go:34:1:34:75 | function declaration | ImplementMe |
13+
| test.go:52:2:52:29 | call to ImplementMe | test.go:41:1:41:87 | function declaration | ImplementMe |
14+
| test.go:52:2:52:29 | call to ImplementMe | test.go:48:1:48:85 | function declaration | ImplementMe |
815
#select
916
| file://:0:0:0:0 | basic interface type | file://:0:0:0:0 | basic interface type |
10-
| file://:0:0:0:0 | basic interface type | test.go:10:6:10:10 | Impl1 |
11-
| file://:0:0:0:0 | basic interface type | test.go:15:6:15:10 | Impl2 |
12-
| file://:0:0:0:0 | basic interface type | test.go:20:6:20:10 | Impl3 |
13-
| file://:0:0:0:0 | basic interface type | test.go:27:6:27:10 | Impl4 |
14-
| file://:0:0:0:0 | basic interface type | test.go:34:6:34:10 | Impl5 |
15-
| file://:0:0:0:0 | basic interface type | test.go:41:6:41:10 | Impl6 |
17+
| file://:0:0:0:0 | basic interface type | test.go:13:6:13:10 | Impl1 |
18+
| file://:0:0:0:0 | basic interface type | test.go:18:6:18:10 | Impl2 |
19+
| file://:0:0:0:0 | basic interface type | test.go:23:6:23:10 | Impl3 |
20+
| file://:0:0:0:0 | basic interface type | test.go:30:6:30:10 | Impl4 |
21+
| file://:0:0:0:0 | basic interface type | test.go:37:6:37:10 | Impl5 |
22+
| file://:0:0:0:0 | basic interface type | test.go:44:6:44:10 | Impl6 |

0 commit comments

Comments
 (0)