Skip to content

Commit 5a03cbd

Browse files
committed
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 <joetsai@digital-static.net> Trust: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent 4a13f6f commit 5a03cbd

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/encoding/json/encode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
784784
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
785785
// We're a large number of nested ptrEncoder.encode calls deep;
786786
// start checking if we've run into a pointer cycle.
787-
ptr := v.Pointer()
787+
ptr := v.UnsafePointer()
788788
if _, ok := e.ptrSeen[ptr]; ok {
789789
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
790790
}
@@ -877,9 +877,9 @@ func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
877877
// Here we use a struct to memorize the pointer to the first element of the slice
878878
// and its length.
879879
ptr := struct {
880-
ptr uintptr
880+
ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
881881
len int
882-
}{v.Pointer(), v.Len()}
882+
}{v.UnsafePointer(), v.Len()}
883883
if _, ok := e.ptrSeen[ptr]; ok {
884884
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
885885
}

0 commit comments

Comments
 (0)