From: Joe Tsai Date: Tue, 2 Nov 2021 19:01:24 +0000 (-0700) Subject: encoding/json: use reflect.Value.UnsafePointer over Pointer X-Git-Tag: go1.19beta1~1196 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5a03cbd12a2fcaf85482f1f4d9570c064510da9b;p=gostls13.git encoding/json: use reflect.Value.UnsafePointer over Pointer The latter returns a uintptr, while the former returns a unsafe.Pointer. A uintptr is unsafe if Go ever switches to a moving GC, while a unsafe.Pointer will be properly tracked by the GC. We do not use unsafe.Pointer for any unsafe type conversions, and only use it for comparability purposes, which is relatively safe. Updates #40592 Change-Id: I813e218668704b63a3043acda4331205a3835a66 Reviewed-on: https://go-review.googlesource.com/c/go/+/360855 Trust: Joseph Tsai Trust: Daniel Martí Reviewed-by: Daniel Martí Reviewed-by: Keith Randall Run-TryBot: Joseph Tsai TryBot-Result: Gopher Robot --- diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index 1f5e3e446a..571ac094e2 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -784,7 +784,7 @@ func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter { // We're a large number of nested ptrEncoder.encode calls deep; // start checking if we've run into a pointer cycle. - ptr := v.Pointer() + ptr := v.UnsafePointer() if _, ok := e.ptrSeen[ptr]; ok { e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) } @@ -877,9 +877,9 @@ func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { // Here we use a struct to memorize the pointer to the first element of the slice // and its length. ptr := struct { - ptr uintptr + ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe len int - }{v.Pointer(), v.Len()} + }{v.UnsafePointer(), v.Len()} if _, ok := e.ptrSeen[ptr]; ok { e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())}) }