Skip to content

Commit cfd281b

Browse files
authored
Merge pull request #17400 from smowton/smowton/admin/further-golang-aliasing-tests
Go: add tests for dataflow relating to type aliasing
2 parents 0516d75 + 0d0c943 commit cfd281b

File tree

15 files changed

+160
-60
lines changed

15 files changed

+160
-60
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test
2+
3+
import (
4+
"test.com/basename/pkg1"
5+
"test.com/basename/pkg2"
6+
)
7+
8+
// Tests that dataflow behaves as expected when loads and stores from a field traverse embeddings that use different type names.
9+
10+
// pkg2.IntStruct is an alias for pkg1.IntStruct
11+
// Note referring to symbols in different packages is necessary so that Go will assign the fields the same name as well as the same type--
12+
// for example, if we defined two aliases here named 'IntStruct1' and 'IntStruct2' and embedded them into two types,
13+
// the types would be non-identical due to having a field implicitly named IntStruct1 and IntStruct2 respectively,
14+
// even though syntactically it could be addressed without mentioning the field name.
15+
16+
type EmbedsPkg1IntStruct = struct{ pkg1.IntStruct }
17+
type EmbedsPkg2IntStruct = struct{ pkg2.IntStruct }
18+
19+
func FEmbedded() {
20+
21+
x := source()
22+
pkg1Struct := EmbedsPkg1IntStruct{pkg1.IntStruct{x}}
23+
24+
GEmbedded(&pkg1Struct)
25+
26+
}
27+
28+
func GEmbedded(pkg2Struct *EmbedsPkg2IntStruct) {
29+
30+
sink(pkg2Struct.Field) // $ hasValueFlow="selection of Field"
31+
32+
}
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.23.1
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test
2+
3+
// Tests that dataflow behaves as expected when field loads and stores, and pointer accesses, go via
4+
// types which are identical, but which use different aliases.
5+
6+
func source() int { return 0 }
7+
func sink(value int) {}
8+
9+
type IntAlias = int
10+
type IntStruct = struct{ field int }
11+
type IntAliasStruct = struct{ field IntAlias }
12+
13+
func F() {
14+
15+
x := source()
16+
intStruct := IntStruct{x}
17+
18+
G(&intStruct)
19+
20+
}
21+
22+
func G(intAliasStruct *IntAliasStruct) {
23+
24+
sink(intAliasStruct.field) // $ hasValueFlow="selection of field"
25+
26+
}

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:20:70:20:83 | call to sink | test.go:15:1:15:45 | function declaration | sink |
3+
| test.go:25:65:25:78 | call to sink | test.go:15:1:15:45 | function declaration | sink |
4+
| test.go:32:54:32:67 | call to sink | test.go:15:1:15:45 | function declaration | sink |
5+
| test.go:39:60:39:73 | call to sink | test.go:15:1:15:45 | function declaration | sink |
6+
| test.go:46:72:46:85 | call to sink | test.go:15:1:15:45 | function declaration | sink |
7+
| test.go:53:70:53:83 | call to sink | test.go:15:1:15:45 | function declaration | sink |
8+
| test.go:56:14:56:21 | call to source | test.go:14:1:14:57 | function declaration | source |
9+
| test.go:57:2:57:29 | call to ImplementMe | test.go:20:1:20:85 | function declaration | ImplementMe |
10+
| test.go:57:2:57:29 | call to ImplementMe | test.go:25:1:25:80 | function declaration | ImplementMe |
11+
| test.go:57:2:57:29 | call to ImplementMe | test.go:32:1:32:69 | function declaration | ImplementMe |
12+
| test.go:57:2:57:29 | call to ImplementMe | test.go:39:1:39:75 | function declaration | ImplementMe |
13+
| test.go:57:2:57:29 | call to ImplementMe | test.go:46:1:46:87 | function declaration | ImplementMe |
14+
| test.go:57:2:57:29 | call to ImplementMe | test.go:53:1:53: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:18:6:18:10 | Impl1 |
18+
| file://:0:0:0:0 | basic interface type | test.go:23:6:23:10 | Impl2 |
19+
| file://:0:0:0:0 | basic interface type | test.go:28:6:28:10 | Impl3 |
20+
| file://:0:0:0:0 | basic interface type | test.go:35:6:35:10 | Impl4 |
21+
| file://:0:0:0:0 | basic interface type | test.go:42:6:42:10 | Impl5 |
22+
| file://:0:0:0:0 | basic interface type | test.go:49:6:49:10 | Impl6 |

0 commit comments

Comments
 (0)