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

jsonpb: check for nil in Marshal and return error to avoid panic. #469

Merged
merged 2 commits into from
Dec 11, 2017
Merged

jsonpb: check for nil in Marshal and return error to avoid panic. #469

merged 2 commits into from
Dec 11, 2017

Conversation

cybrcodr
Copy link
Contributor

@cybrcodr cybrcodr commented Dec 9, 2017

Add check for nil in Marshal and return error to avoid panic.

var msg *pb.Simple
m := &Marshaler{}
if _, err := m.MarshalToString(msg); err == nil {
t.Errorf("mashalling nil returned no error")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It seems the convention is usually one l.

s/marshalling/marshaling/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

jsonpb/jsonpb.go Outdated
@@ -118,6 +118,9 @@ type JSONPBUnmarshaler interface {

// Marshal marshals a protocol buffer into JSON.
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
if pb == nil || reflect.ValueOf(pb).IsNil() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that pb is a pointer, which is not always true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes. Switched to checking for pointer only. I'm guessing I shouldn't worry about nil maps, slices, etc.

Fixed check for nil to pointer types only.
Copy link
Contributor Author

@cybrcodr cybrcodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

jsonpb/jsonpb.go Outdated
@@ -118,6 +118,9 @@ type JSONPBUnmarshaler interface {

// Marshal marshals a protocol buffer into JSON.
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
if pb == nil || reflect.ValueOf(pb).IsNil() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes. Switched to checking for pointer only. I'm guessing I shouldn't worry about nil maps, slices, etc.

var msg *pb.Simple
m := &Marshaler{}
if _, err := m.MarshalToString(msg); err == nil {
t.Errorf("mashalling nil returned no error")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@cybrcodr cybrcodr merged commit 49f2ba7 into golang:dev Dec 11, 2017
@cybrcodr cybrcodr deleted the jsonpb branch December 11, 2017 21:09
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants