File tree 3 files changed +20
-4
lines changed
3 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -8,8 +8,10 @@ import "unsafe"
8
8
9
9
func checkptrAlignment (p unsafe.Pointer , elem * _type , n uintptr ) {
10
10
// Check that (*[n]elem)(p) is appropriately aligned.
11
+ // Note that we allow unaligned pointers if the types they point to contain
12
+ // no pointers themselves. See issue 37298.
11
13
// TODO(mdempsky): What about fieldAlign?
12
- if uintptr (p )& (uintptr (elem .align )- 1 ) != 0 {
14
+ if elem . ptrdata != 0 && uintptr (p )& (uintptr (elem .align )- 1 ) != 0 {
13
15
throw ("checkptr: unsafe pointer conversion" )
14
16
}
15
17
Original file line number Diff line number Diff line change @@ -24,7 +24,8 @@ func TestCheckPtr(t *testing.T) {
24
24
cmd string
25
25
want string
26
26
}{
27
- {"CheckPtrAlignment" , "fatal error: checkptr: unsafe pointer conversion\n " },
27
+ {"CheckPtrAlignmentPtr" , "fatal error: checkptr: unsafe pointer conversion\n " },
28
+ {"CheckPtrAlignmentNoPtr" , "" },
28
29
{"CheckPtrArithmetic" , "fatal error: checkptr: unsafe pointer arithmetic\n " },
29
30
{"CheckPtrSize" , "fatal error: checkptr: unsafe pointer conversion\n " },
30
31
{"CheckPtrSmall" , "fatal error: checkptr: unsafe pointer arithmetic\n " },
@@ -38,6 +39,12 @@ func TestCheckPtr(t *testing.T) {
38
39
if err != nil {
39
40
t .Log (err )
40
41
}
42
+ if tc .want == "" {
43
+ if len (got ) > 0 {
44
+ t .Errorf ("output:\n %s\n want no output" , got )
45
+ }
46
+ return
47
+ }
41
48
if ! strings .HasPrefix (string (got ), tc .want ) {
42
49
t .Errorf ("output:\n %s\n \n want output starting with: %s" , got , tc .want )
43
50
}
Original file line number Diff line number Diff line change @@ -7,18 +7,25 @@ package main
7
7
import "unsafe"
8
8
9
9
func init () {
10
- register ("CheckPtrAlignment" , CheckPtrAlignment )
10
+ register ("CheckPtrAlignmentNoPtr" , CheckPtrAlignmentNoPtr )
11
+ register ("CheckPtrAlignmentPtr" , CheckPtrAlignmentPtr )
11
12
register ("CheckPtrArithmetic" , CheckPtrArithmetic )
12
13
register ("CheckPtrSize" , CheckPtrSize )
13
14
register ("CheckPtrSmall" , CheckPtrSmall )
14
15
}
15
16
16
- func CheckPtrAlignment () {
17
+ func CheckPtrAlignmentNoPtr () {
17
18
var x [2 ]int64
18
19
p := unsafe .Pointer (& x [0 ])
19
20
sink2 = (* int64 )(unsafe .Pointer (uintptr (p ) + 1 ))
20
21
}
21
22
23
+ func CheckPtrAlignmentPtr () {
24
+ var x [2 ]int64
25
+ p := unsafe .Pointer (& x [0 ])
26
+ sink2 = (* * int64 )(unsafe .Pointer (uintptr (p ) + 1 ))
27
+ }
28
+
22
29
func CheckPtrArithmetic () {
23
30
var x int
24
31
i := uintptr (unsafe .Pointer (& x ))
You can’t perform that action at this time.
0 commit comments