Fix nil pointer dereferences from status.FromProto(nil) (#1211)

This commit is contained in:
dfawley 2017-04-28 14:28:53 -07:00 committed by GitHub
parent fc5d355228
commit 84cd50a2f3
2 changed files with 27 additions and 0 deletions

View File

@ -71,16 +71,25 @@ type Status struct {
// Code returns the status code contained in s.
func (s *Status) Code() codes.Code {
if s == nil || s.s == nil {
return codes.OK
}
return codes.Code(s.s.Code)
}
// Message returns the message contained in s.
func (s *Status) Message() string {
if s == nil || s.s == nil {
return ""
}
return s.s.Message
}
// Proto returns s's status as an spb.Status proto message.
func (s *Status) Proto() *spb.Status {
if s == nil {
return nil
}
return proto.Clone(s.s).(*spb.Status)
}

View File

@ -65,6 +65,24 @@ func TestFromToProto(t *testing.T) {
}
}
func TestFromNilProto(t *testing.T) {
tests := []*Status{nil, FromProto(nil)}
for _, s := range tests {
if c := s.Code(); c != codes.OK {
t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
}
if m := s.Message(); m != "" {
t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
}
if p := s.Proto(); p != nil {
t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
}
if e := s.Err(); e != nil {
t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
}
}
}
func TestError(t *testing.T) {
err := Error(codes.Internal, "test description")
if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {