]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: emit more specific errors from checkptr
authorJohan Jansson <johan.jansson@iki.fi>
Wed, 11 Mar 2020 19:05:21 +0000 (21:05 +0200)
committerIan Lance Taylor <iant@golang.org>
Thu, 12 Mar 2020 23:45:51 +0000 (23:45 +0000)
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 <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/runtime/checkptr.go
src/runtime/checkptr_test.go

index ddbc8168afc11ab7f8d41fc34b1129f38c95f710..8e401e876351d35e1f15da9da7fea6ee6bf77e6b 100644 (file)
@@ -10,18 +10,18 @@ func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
        // Check that (*[n]elem)(p) is appropriately aligned.
        // TODO(mdempsky): What about fieldAlign?
        if uintptr(p)&(uintptr(elem.align)-1) != 0 {
-               throw("checkptr: unsafe pointer conversion")
+               throw("checkptr: misaligned pointer conversion")
        }
 
        // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
        if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
-               throw("checkptr: unsafe pointer conversion")
+               throw("checkptr: converted pointer straddles multiple allocations")
        }
 }
 
 func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
        if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
-               throw("checkptr: unsafe pointer arithmetic")
+               throw("checkptr: pointer arithmetic computed bad pointer value")
        }
 
        // Check that if the computed pointer p points into a heap
@@ -38,7 +38,7 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
                }
        }
 
-       throw("checkptr: unsafe pointer arithmetic")
+       throw("checkptr: pointer arithmetic result points to invalid allocation")
 }
 
 // checkptrBase returns the base address for the allocation containing
index c5f22cc1015c4e28e31b5aaa8ebe711d0a718800..1a7c253733d6e611c621b2c336cf3a11ea2f7c29 100644 (file)
@@ -24,10 +24,10 @@ func TestCheckPtr(t *testing.T) {
                cmd  string
                want string
        }{
-               {"CheckPtrAlignment", "fatal error: checkptr: unsafe pointer conversion\n"},
-               {"CheckPtrArithmetic", "fatal error: checkptr: unsafe pointer arithmetic\n"},
-               {"CheckPtrSize", "fatal error: checkptr: unsafe pointer conversion\n"},
-               {"CheckPtrSmall", "fatal error: checkptr: unsafe pointer arithmetic\n"},
+               {"CheckPtrAlignment", "fatal error: checkptr: misaligned pointer conversion\n"},
+               {"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
+               {"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
+               {"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
        }
 
        for _, tc := range testCases {