ttrpc: fix the issue of marshaling on nil will crash the server

Since some type's Marshal() on nil will crash runtime, thus
it's should check 'resp' before marshaling on it. If it's
nil, return directly to avoid the server crash.

Signed-off-by: lifupan <lifupan@gmail.com>
This commit is contained in:
lifupan 2019-10-15 15:50:49 +08:00
parent 5046735d25
commit 2ef8878926

View File

@ -21,6 +21,7 @@ import (
"io"
"os"
"path"
"unsafe"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
@ -95,6 +96,10 @@ func (s *serviceSet) dispatch(ctx context.Context, serviceName, methodName strin
return nil, err
}
if isNil(resp) {
return nil, errors.New("ttrpc: marshal called with nil")
}
switch v := resp.(type) {
case proto.Message:
r, err := proto.Marshal(v)
@ -154,3 +159,7 @@ func convertCode(err error) codes.Code {
func fullPath(service, method string) string {
return "/" + path.Join(service, method)
}
func isNil(resp interface{}) bool {
return (*[2]uintptr)(unsafe.Pointer(&resp))[1] == 0
}