From 26802c7eccfb484141a1488c9ef40e5b5805d96c Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 16 Sep 2014 10:10:24 -0500 Subject: [PATCH] Fix JSONGetMethod to prevent panics on bad input. ok @jcvernaleo --- jsonapi.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/jsonapi.go b/jsonapi.go index 3720ce10..0e37bd23 100644 --- a/jsonapi.go +++ b/jsonapi.go @@ -618,26 +618,11 @@ func CreateMessageWithId(message string, id interface{}, args ...interface{}) ([ // JSONGetMethod takes a message and tries to find the bitcoin command that it // is in reply to so it can be processed further. func JSONGetMethod(message []byte) (string, error) { - // Need this so we can tell what kind of message we are sending - // so we can unmarshal it properly. - var method string - var msg interface{} - err := json.Unmarshal(message, &msg) - if err != nil { - err := fmt.Errorf("error, message does not appear to be valid json: %v", err) - return method, err + var obj struct { + Method string `json:"method"` } - m := msg.(map[string]interface{}) - for k, v := range m { - if k == "method" { - method = v.(string) - } - } - if method == "" { - err := fmt.Errorf("error, no method specified") - return method, err - } - return method, err + err := json.Unmarshal(message, &obj) + return obj.Method, err } // TlsRpcCommand takes a message generated from one of the routines above