Skip to content

Commit c3c5f8b

Browse files
author
Bas van Kervel
committed
rpc: fixed params parsing problem which could lead to a panic
check argument type before parsing params recover from panic in ipc channel
1 parent 56f8699 commit c3c5f8b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

rpc/api/eth_args.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,12 @@ func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
626626

627627
args.IncludeTxs = obj[1].(bool)
628628

629-
return nil
629+
if inclTx, ok := obj[1].(bool); ok {
630+
args.IncludeTxs = inclTx
631+
return nil
632+
}
633+
634+
return shared.NewInvalidTypeError("includeTxs", "not a bool")
630635
}
631636

632637
type GetBlockByNumberArgs struct {
@@ -648,9 +653,12 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
648653
return err
649654
}
650655

651-
args.IncludeTxs = obj[1].(bool)
656+
if inclTx, ok := obj[1].(bool); ok {
657+
args.IncludeTxs = inclTx
658+
return nil
659+
}
652660

653-
return nil
661+
return shared.NewInvalidTypeError("includeTxs", "not a bool")
654662
}
655663

656664
type BlockFilterArgs struct {

rpc/comms/comms.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ type EthereumClient interface {
6262
func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
6363
codec := c.New(conn)
6464

65+
defer func() {
66+
if r := recover(); r != nil {
67+
glog.Errorf("panic: %v\n", r)
68+
}
69+
codec.Close()
70+
}()
71+
6572
for {
6673
requests, isBatch, err := codec.ReadRequest()
6774
if err == io.EOF {
68-
codec.Close()
6975
return
7076
} else if err != nil {
71-
codec.Close()
7277
glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err)
7378
return
7479
}
@@ -87,7 +92,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
8792

8893
err = codec.WriteResponse(responses[:responseCount])
8994
if err != nil {
90-
codec.Close()
9195
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
9296
return
9397
}
@@ -98,7 +102,6 @@ func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
98102
rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err)
99103
err = codec.WriteResponse(rpcResponse)
100104
if err != nil {
101-
codec.Close()
102105
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
103106
return
104107
}

0 commit comments

Comments
 (0)