Commit c3c5f8b6 authored by Bas van Kervel's avatar Bas van Kervel

rpc: fixed params parsing problem which could lead to a panic

 check argument type before parsing params
 recover from panic in ipc channel
parent 56f8699a
...@@ -626,7 +626,12 @@ func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) { ...@@ -626,7 +626,12 @@ func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
args.IncludeTxs = obj[1].(bool) args.IncludeTxs = obj[1].(bool)
return nil if inclTx, ok := obj[1].(bool); ok {
args.IncludeTxs = inclTx
return nil
}
return shared.NewInvalidTypeError("includeTxs", "not a bool")
} }
type GetBlockByNumberArgs struct { type GetBlockByNumberArgs struct {
...@@ -648,9 +653,12 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { ...@@ -648,9 +653,12 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
return err return err
} }
args.IncludeTxs = obj[1].(bool) if inclTx, ok := obj[1].(bool); ok {
args.IncludeTxs = inclTx
return nil
}
return nil return shared.NewInvalidTypeError("includeTxs", "not a bool")
} }
type BlockFilterArgs struct { type BlockFilterArgs struct {
......
...@@ -62,13 +62,18 @@ type EthereumClient interface { ...@@ -62,13 +62,18 @@ type EthereumClient interface {
func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) { func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
codec := c.New(conn) codec := c.New(conn)
defer func() {
if r := recover(); r != nil {
glog.Errorf("panic: %v\n", r)
}
codec.Close()
}()
for { for {
requests, isBatch, err := codec.ReadRequest() requests, isBatch, err := codec.ReadRequest()
if err == io.EOF { if err == io.EOF {
codec.Close()
return return
} else if err != nil { } else if err != nil {
codec.Close()
glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err) glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err)
return return
} }
...@@ -87,7 +92,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) { ...@@ -87,7 +92,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
err = codec.WriteResponse(responses[:responseCount]) err = codec.WriteResponse(responses[:responseCount])
if err != nil { if err != nil {
codec.Close()
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err) glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
return return
} }
...@@ -98,7 +102,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) { ...@@ -98,7 +102,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err) rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err)
err = codec.WriteResponse(rpcResponse) err = codec.WriteResponse(rpcResponse)
if err != nil { if err != nil {
codec.Close()
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err) glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
return return
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment