From 841aeb030309aac1be7008191e4c0a396cc17ce9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 25 Jul 2017 20:25:53 +0200 Subject: dashboard/dashapi: always zero reply json decoding behavior is somewhat surprising (see // https://github.com/golang/go/issues/21092). This behavior is especially easy to hit in tests that reuse reply objects. To avoid any surprises, we zero the reply. --- dashboard/dashapi/dashapi.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'dashboard/dashapi') diff --git a/dashboard/dashapi/dashapi.go b/dashboard/dashapi/dashapi.go index 79b0368b1..8f5856fd9 100644 --- a/dashboard/dashapi/dashapi.go +++ b/dashboard/dashapi/dashapi.go @@ -14,6 +14,7 @@ import ( "io/ioutil" "net/http" "net/url" + "reflect" "strings" ) @@ -157,6 +158,16 @@ type ( ) func Query(client, addr, key, method string, ctor RequestCtor, doer RequestDoer, req, reply interface{}) error { + if reply != nil { + // json decoding behavior is somewhat surprising + // (see // https://github.com/golang/go/issues/21092). + // To avoid any surprises, we zero the reply. + typ := reflect.TypeOf(reply) + if typ.Kind() != reflect.Ptr { + return fmt.Errorf("resp must be a pointer") + } + reflect.ValueOf(reply).Elem().Set(reflect.New(typ.Elem()).Elem()) + } values := make(url.Values) values.Add("client", client) values.Add("key", key) -- cgit mrf-deployment