Merge pull request #51 from lifupan/fixnilresp
ttrpc: fix the issue of marshaling on nil will crash the server
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -95,6 +96,10 @@ func (s *serviceSet) dispatch(ctx context.Context, serviceName, methodName strin
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isNil(resp) {
|
||||||
|
return nil, errors.New("ttrpc: marshal called with nil")
|
||||||
|
}
|
||||||
|
|
||||||
switch v := resp.(type) {
|
switch v := resp.(type) {
|
||||||
case proto.Message:
|
case proto.Message:
|
||||||
r, err := proto.Marshal(v)
|
r, err := proto.Marshal(v)
|
||||||
@@ -154,3 +159,7 @@ func convertCode(err error) codes.Code {
|
|||||||
func fullPath(service, method string) string {
|
func fullPath(service, method string) string {
|
||||||
return "/" + path.Join(service, method)
|
return "/" + path.Join(service, method)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isNil(resp interface{}) bool {
|
||||||
|
return (*[2]uintptr)(unsafe.Pointer(&resp))[1] == 0
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user