Skip to content

Commit 229247d

Browse files
randall77cagedmantis
authored andcommitted
[release-branch.go1.14] runtime: don't report a pointer alignment error for pointer-free base type
Fixes #37905 Change-Id: I8ba9c8b106e16cea7dd25473c7390b0f2ba9a1a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/223781 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/223749
1 parent c512509 commit 229247d

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/runtime/checkptr.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import "unsafe"
88

99
func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
1010
// 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.
1113
// 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 {
1315
throw("checkptr: unsafe pointer conversion")
1416
}
1517

src/runtime/checkptr_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func TestCheckPtr(t *testing.T) {
2424
cmd string
2525
want string
2626
}{
27-
{"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
27+
{"CheckPtrAlignmentPtr", "fatal error: checkptr: unsafe pointer conversion\n"},
28+
{"CheckPtrAlignmentNoPtr", ""},
2829
{"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
2930
{"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
3031
{"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
@@ -38,6 +39,12 @@ func TestCheckPtr(t *testing.T) {
3839
if err != nil {
3940
t.Log(err)
4041
}
42+
if tc.want == "" {
43+
if len(got) > 0 {
44+
t.Errorf("output:\n%s\nwant no output", got)
45+
}
46+
return
47+
}
4148
if !strings.HasPrefix(string(got), tc.want) {
4249
t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
4350
}

src/runtime/testdata/testprog/checkptr.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ package main
77
import "unsafe"
88

99
func init() {
10-
register("CheckPtrAlignment", CheckPtrAlignment)
10+
register("CheckPtrAlignmentNoPtr", CheckPtrAlignmentNoPtr)
11+
register("CheckPtrAlignmentPtr", CheckPtrAlignmentPtr)
1112
register("CheckPtrArithmetic", CheckPtrArithmetic)
1213
register("CheckPtrSize", CheckPtrSize)
1314
register("CheckPtrSmall", CheckPtrSmall)
1415
}
1516

16-
func CheckPtrAlignment() {
17+
func CheckPtrAlignmentNoPtr() {
1718
var x [2]int64
1819
p := unsafe.Pointer(&x[0])
1920
sink2 = (*int64)(unsafe.Pointer(uintptr(p) + 1))
2021
}
2122

23+
func CheckPtrAlignmentPtr() {
24+
var x [2]int64
25+
p := unsafe.Pointer(&x[0])
26+
sink2 = (**int64)(unsafe.Pointer(uintptr(p) + 1))
27+
}
28+
2229
func CheckPtrArithmetic() {
2330
var x int
2431
i := uintptr(unsafe.Pointer(&x))

0 commit comments

Comments
 (0)