1
0
mirror of https://github.com/golang/go synced 2024-10-01 11:28:34 -06:00
go/src/reflect
Huan Du 4dc11ae26b reflect: fix panic in DeepEqual when checking a cycle
Before this change, when DeepEqual checks values with cycle, it may
panic due to stack overflow.

Here is a sample to reproduce the issue.

    makeCycleMap := func() interface{} {
        cycleMap := map[string]interface{}{}
        cycleMap["foo"] = cycleMap
        return cycleMap
    }

    m1 := makeCycleMap()
    m2 := makeCycleMap()
    reflect.DeepEqual(m1, m2) // stack overflow

The root cause is that DeepEqual fails to cache interface values
in visited map, which is used to detect cycle. DeepEqual calls
CanAddr to check whether a value should be cached or not. However,
all values referenced by interface don't have flagAddr thus all these
values are not cached.

THe fix is to remove CanAddr calls and use underlying ptr in value
directly. As ptr is only read-only in DeepEqual for caching, it's
safe to do so. We don't use UnsafeAddr this time, because this method
panics when CanAddr returns false.

Fixes #33907

Change-Id: I2aa88cc060a2c2192b1d34c129c0aad4bd5597e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/191940
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-09-11 00:56:01 +00:00
..
all_test.go reflect: fix panic in DeepEqual when checking a cycle 2019-09-11 00:56:01 +00:00
asm_386.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_amd64.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_amd64p32.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_arm64.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_arm.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_mips64x.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_mipsx.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_ppc64x.s reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
asm_s390x.s reflect: fix s390x reflect method calls 2018-09-30 20:30:55 +00:00
asm_wasm.s all: rename WebAssembly instructions according to spec changes 2019-03-03 21:10:01 +00:00
deepequal.go reflect: fix panic in DeepEqual when checking a cycle 2019-09-11 00:56:01 +00:00
example_test.go reflect: add an example for Kind 2018-10-03 22:38:53 +00:00
export_test.go runtime: remove kindNoPointers 2019-03-25 20:46:35 +00:00
makefunc.go reflect: ensure correct scanning of return values 2018-09-29 20:25:24 +00:00
set_test.go reflect: define MyBuffer more locally in TestImplicitMapConversion 2018-04-18 12:47:39 +00:00
swapper.go runtime: remove kindNoPointers 2019-03-25 20:46:35 +00:00
tostring_test.go
type.go cmd/compile,runtime: generate hash functions only for types which are map keys 2019-09-03 20:41:29 +00:00
value.go reflect: enhance docs for IsZero and IsValid 2019-09-11 00:16:10 +00:00