Skip to content

Commit

Permalink
Fix nil pointer dereferences from status.FromProto(nil) (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley committed Apr 28, 2017
1 parent fc5d355 commit 84cd50a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 18 additions & 0 deletions status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 84cd50a

Please sign in to comment.