Skip to content

Commit

Permalink
fix fatal error: do not handle nil package
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks committed Apr 12, 2022
1 parent bbe817c commit 18a6f94
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
24 changes: 14 additions & 10 deletions protocol/dubbo/dubbo_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,30 @@ func (c *DubboCodec) EncodeResponse(response *remoting.Response) (*bytes.Buffer,
}

// Decode data, including request and response.
func (c *DubboCodec) Decode(data []byte) (remoting.DecodeResult, int, error) {
var res remoting.DecodeResult

func (c *DubboCodec) Decode(data []byte) (*remoting.DecodeResult, int, error) {
dataLen := len(data)
if dataLen < impl.HEADER_LENGTH { // check whether header bytes is enough or not
return res, 0, nil
return nil, 0, nil
}
if c.isRequest(data) {
req, length, err := c.decodeRequest(data)
if err != nil {
return remoting.DecodeResult{}, length, perrors.WithStack(err)
return nil, length, perrors.WithStack(err)
}
if req == ((*remoting.Request)(nil)) {
return nil, length, err
}
return remoting.DecodeResult{IsRequest: true, Result: req}, length, perrors.WithStack(err)
return &remoting.DecodeResult{IsRequest: true, Result: req}, length, perrors.WithStack(err)
}

resp, length, err := c.decodeResponse(data)
rsp, length, err := c.decodeResponse(data)
if err != nil {
return remoting.DecodeResult{}, length, perrors.WithStack(err)
return nil, length, perrors.WithStack(err)
}
return remoting.DecodeResult{IsRequest: false, Result: resp}, length, perrors.WithStack(err)
if rsp == ((*remoting.Response)(nil)) {
return nil, length, err
}
return &remoting.DecodeResult{IsRequest: false, Result: rsp}, length, perrors.WithStack(err)
}

func (c *DubboCodec) isRequest(data []byte) bool {
Expand Down Expand Up @@ -247,14 +251,14 @@ func (c *DubboCodec) decodeResponse(data []byte) (*remoting.Response, int, error
if err != nil {
originErr := perrors.Cause(err)
// if the data is very big, so the receive need much times.
logger.Errorf("pkg.Unmarshal(len(@data):%d) = error:%+v", buf.Len(), err)
if originErr == hessian.ErrHeaderNotEnough { // this is impossible, as dubbo_codec.go:DubboCodec::Decode() line 167
return nil, 0, nil
}
if originErr == hessian.ErrBodyNotEnough {
return nil, hessian.HEADER_LENGTH + pkg.GetBodyLen(), nil
}

logger.Warnf("pkg.Unmarshal(len(@data):%d) = error:%+v", buf.Len(), err)
return nil, 0, perrors.WithStack(err)
}
response := &remoting.Response{
Expand Down
2 changes: 1 addition & 1 deletion remoting/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
type Codec interface {
EncodeRequest(request *Request) (*bytes.Buffer, error)
EncodeResponse(response *Response) (*bytes.Buffer, error)
Decode(data []byte) (DecodeResult, int, error)
Decode(data []byte) (*DecodeResult, int, error)
}

type DecodeResult struct {
Expand Down
8 changes: 4 additions & 4 deletions remoting/getty/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (h *RpcClientHandler) OnClose(session getty.Session) {

// OnMessage get response from getty server, and update the session to the getty client session list
func (h *RpcClientHandler) OnMessage(session getty.Session, pkg interface{}) {
result, ok := pkg.(remoting.DecodeResult)
if !ok {
result, ok := pkg.(*remoting.DecodeResult)
if !ok || result == ((*remoting.DecodeResult)(nil)) {
logger.Errorf("[RpcClientHandler.OnMessage] getty client gets an unexpected rpc result: %#v", result)
return
}
Expand Down Expand Up @@ -232,8 +232,8 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
}
h.rwlock.Unlock()

decodeResult, drOK := pkg.(remoting.DecodeResult)
if !drOK {
decodeResult, drOK := pkg.(*remoting.DecodeResult)
if !drOK || decodeResult == ((*remoting.DecodeResult)(nil)) {
logger.Errorf("illegal package{%#v}", pkg)
return
}
Expand Down
10 changes: 8 additions & 2 deletions remoting/getty/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (p *RpcClientPackageHandler) Read(ss getty.Session, data []byte) (interface
if err != nil {
err = perrors.WithStack(err)
}
if rsp.Result == nil {
if rsp == ((*remoting.DecodeResult)(nil)) {
return nil, length, err
}
if rsp.Result == ((*remoting.Response)(nil)) || rsp.Result == ((*remoting.Request)(nil)) {
return nil, length, err
}
return rsp, length, err
Expand Down Expand Up @@ -97,7 +100,10 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface
if err != nil {
err = perrors.WithStack(err)
}
if req.Result == nil {
if req == ((*remoting.DecodeResult)(nil)) {
return nil, length, err
}
if req.Result == ((*remoting.Request)(nil)) || req.Result == ((*remoting.Response)(nil)) {
return nil, length, err // as getty rule
}
return req, length, err
Expand Down

0 comments on commit 18a6f94

Please sign in to comment.