Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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