Fix JSONGetMethod to prevent panics on bad input.
ok @jcvernaleo
This commit is contained in:
parent
3e465b2012
commit
26802c7ecc
1 changed files with 4 additions and 19 deletions
23
jsonapi.go
23
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
|
// JSONGetMethod takes a message and tries to find the bitcoin command that it
|
||||||
// is in reply to so it can be processed further.
|
// is in reply to so it can be processed further.
|
||||||
func JSONGetMethod(message []byte) (string, error) {
|
func JSONGetMethod(message []byte) (string, error) {
|
||||||
// Need this so we can tell what kind of message we are sending
|
var obj struct {
|
||||||
// so we can unmarshal it properly.
|
Method string `json:"method"`
|
||||||
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
|
|
||||||
}
|
}
|
||||||
m := msg.(map[string]interface{})
|
err := json.Unmarshal(message, &obj)
|
||||||
for k, v := range m {
|
return obj.Method, err
|
||||||
if k == "method" {
|
|
||||||
method = v.(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if method == "" {
|
|
||||||
err := fmt.Errorf("error, no method specified")
|
|
||||||
return method, err
|
|
||||||
}
|
|
||||||
return method, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TlsRpcCommand takes a message generated from one of the routines above
|
// TlsRpcCommand takes a message generated from one of the routines above
|
||||||
|
|
Loading…
Reference in a new issue