Skip to content

Commit 1f231d7

Browse files
jupjianlancetaylor
authored andcommitted
runtime: emit more specific errors from checkptr
Update error messages for pointer alignment checks and pointer arithmetic checks so that each type of error has a unique error message. Fixes #37488 Change-Id: Ida2c2fa3f041a3307d665879a463f9e8f2c1fd03 Reviewed-on: https://go-review.googlesource.com/c/go/+/223037 Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 5d70cb0 commit 1f231d7

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/runtime/checkptr.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
1010
// Check that (*[n]elem)(p) is appropriately aligned.
1111
// TODO(mdempsky): What about fieldAlign?
1212
if uintptr(p)&(uintptr(elem.align)-1) != 0 {
13-
throw("checkptr: unsafe pointer conversion")
13+
throw("checkptr: misaligned pointer conversion")
1414
}
1515

1616
// Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
1717
if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
18-
throw("checkptr: unsafe pointer conversion")
18+
throw("checkptr: converted pointer straddles multiple allocations")
1919
}
2020
}
2121

2222
func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
2323
if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
24-
throw("checkptr: unsafe pointer arithmetic")
24+
throw("checkptr: pointer arithmetic computed bad pointer value")
2525
}
2626

2727
// Check that if the computed pointer p points into a heap
@@ -38,7 +38,7 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
3838
}
3939
}
4040

41-
throw("checkptr: unsafe pointer arithmetic")
41+
throw("checkptr: pointer arithmetic result points to invalid allocation")
4242
}
4343

4444
// checkptrBase returns the base address for the allocation containing

src/runtime/checkptr_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func TestCheckPtr(t *testing.T) {
2424
cmd string
2525
want string
2626
}{
27-
{"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
28-
{"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
29-
{"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
30-
{"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
27+
{"CheckPtrAlignment", "fatal error: checkptr: misaligned pointer conversion\n"},
28+
{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
29+
{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
30+
{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
3131
}
3232

3333
for _, tc := range testCases {

0 commit comments

Comments
 (0)