From 9fec2495903ddf5422c1d555e3d75883e491b93e Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Sat, 25 Jul 2020 08:17:53 +0900 Subject: [PATCH 1/2] Copy the JSON data of EphemeralKey The godoc of json.Unmarshaler says: > https://golang.org/pkg/encoding/json/#Unmarshaler > UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning. This code comes from the implementation of RawMessage.UnmarshalJSON. https://github.com/golang/go/blob/074f2d800f2c7b741a080081cfcc5295b375b23d/src/encoding/json/stream.go#L275 --- ephemeralkey.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ephemeralkey.go b/ephemeralkey.go index 9fc649e81b..d648faee97 100644 --- a/ephemeralkey.go +++ b/ephemeralkey.go @@ -46,7 +46,7 @@ func (e *EphemeralKey) UnmarshalJSON(data []byte) error { *e = EphemeralKey(ee) } - e.RawJSON = data + e.RawJSON = append(e.RawJSON[:0], data...) return nil } From a40214c96ffa36adaf33581519a1f991e3479ddf Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Tue, 28 Jul 2020 06:10:06 +0900 Subject: [PATCH 2/2] add a comment about assigning `RawJSON` --- ephemeralkey.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ephemeralkey.go b/ephemeralkey.go index d648faee97..9bc8a7fdcc 100644 --- a/ephemeralkey.go +++ b/ephemeralkey.go @@ -46,6 +46,9 @@ func (e *EphemeralKey) UnmarshalJSON(data []byte) error { *e = EphemeralKey(ee) } + // Go does guarantee the longevity of `data`, so copy when assigning `RawJSON` + // See https://golang.org/pkg/encoding/json/#Unmarshaler + // and https://github.com/stripe/stripe-go/pull/1142 e.RawJSON = append(e.RawJSON[:0], data...) return nil