Skip to content

Commit daeb5ef

Browse files
committed
runtime: somewhat better checkptr error messages
They're still lacking in details, but at least better than being printed as raw interface values. Updates #22218. Change-Id: I4fd813253afdd6455c0c9b5a05c61659805abad1 Reviewed-on: https://go-review.googlesource.com/c/go/+/202677 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 9093b1d commit daeb5ef

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/runtime/checkptr.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,45 @@ package runtime
66

77
import "unsafe"
88

9-
type ptrAlign struct {
9+
type ptrAlignError struct {
1010
ptr unsafe.Pointer
1111
elem *_type
1212
n uintptr
1313
}
1414

15+
func (e ptrAlignError) RuntimeError() {}
16+
17+
func (e ptrAlignError) Error() string {
18+
return "runtime error: unsafe pointer conversion"
19+
}
20+
1521
func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
1622
// Check that (*[n]elem)(p) is appropriately aligned.
1723
// TODO(mdempsky): What about fieldAlign?
1824
if uintptr(p)&(uintptr(elem.align)-1) != 0 {
19-
panic(ptrAlign{p, elem, n})
25+
panic(ptrAlignError{p, elem, n})
2026
}
2127

2228
// Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
2329
if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
24-
panic(ptrAlign{p, elem, n})
30+
panic(ptrAlignError{p, elem, n})
2531
}
2632
}
2733

28-
type ptrArith struct {
34+
type ptrArithError struct {
2935
ptr unsafe.Pointer
3036
originals []unsafe.Pointer
3137
}
3238

39+
func (e ptrArithError) RuntimeError() {}
40+
41+
func (e ptrArithError) Error() string {
42+
return "runtime error: unsafe pointer arithmetic"
43+
}
44+
3345
func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
3446
if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
35-
panic(ptrArith{p, originals})
47+
panic(ptrArithError{p, originals})
3648
}
3749

3850
// Check that if the computed pointer p points into a heap
@@ -49,7 +61,7 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
4961
}
5062
}
5163

52-
panic(ptrArith{p, originals})
64+
panic(ptrArithError{p, originals})
5365
}
5466

5567
func checkptrBase(p unsafe.Pointer) uintptr {

0 commit comments

Comments
 (0)