Skip to content

Commit 409d916

Browse files
committed
Use a struct to record slice
1 parent b69943d commit 409d916

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/encoding/json/encode.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,11 @@ func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
871871
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
872872
// We're a large number of nested ptrEncoder.encode calls deep;
873873
// start checking if we've run into a pointer cycle.
874-
p := reflect.New(reflect.TypeOf(v))
875-
p.Elem().Set(reflect.ValueOf(v))
876-
ptr := p.Pointer()
874+
// Here we use a struct to record the pointer of the first element and the length.
875+
ptr := struct {
876+
ptr uintptr
877+
len int
878+
}{v.Pointer(), v.Len()}
877879
if _, ok := e.ptrSeen[ptr]; ok {
878880
e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
879881
}

0 commit comments

Comments
 (0)