-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: fix handling of illegal and negative nanoseconds #492
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ package jsonpb | |
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"math" | ||
"reflect" | ||
|
@@ -407,6 +408,8 @@ var marshalingTests = []struct { | |
{"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON}, | ||
{"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON}, | ||
{"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`}, | ||
{"Duration beyond float64 precision", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 100000000, Nanos: 1}}, `{"dur":"100000000.000000001s"}`}, | ||
{"negative Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: -123, Nanos: -456}}, `{"dur":"-123.000000456s"}`}, | ||
{"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{ | ||
Fields: map[string]*stpb.Value{ | ||
"one": {Kind: &stpb.Value_StringValue{"loneliest number"}}, | ||
|
@@ -473,6 +476,33 @@ func TestMarshalingNil(t *testing.T) { | |
} | ||
} | ||
|
||
func TestMarshalIllegalTime(t *testing.T) { | ||
tests := []struct { | ||
pb proto.Message | ||
errWant error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we care that much about the exact error message. Let's just change this to a "fail bool" that checks whether it passes or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
}{ | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 0}}, nil}, | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 0}}, nil}, | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: -1}}, errors.New("signs of seconds and nanos do not match")}, | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 1}}, errors.New("signs of seconds and nanos do not match")}, | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 1000000000}}, errors.New("ns out of range")}, | ||
{&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: -1000000000}}, errors.New("ns out of range")}, | ||
{&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1}}, nil}, | ||
{&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: -1}}, errors.New("ns out of range")}, | ||
{&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1000000000}}, errors.New("ns out of range")}, | ||
} | ||
for _, tt := range tests { | ||
_, err := marshaler.MarshalToString(tt.pb) | ||
if (err == nil) != (tt.errWant == nil) { | ||
t.Errorf("marshaler.MarshalToString(%v) = _, %v; want _, %v", tt.pb, err, tt.errWant) | ||
continue | ||
} | ||
if err != nil && !strings.Contains(err.Error(), tt.errWant.Error()) { | ||
t.Errorf("marshaler.MarshalToString(%v) = _, %v; want _, %v", tt.pb, err, tt.errWant) | ||
} | ||
} | ||
} | ||
|
||
func TestMarshalJSONPBMarshaler(t *testing.T) { | ||
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }` | ||
msg := dynamicMessage{rawJson: rawJson} | ||
|
@@ -920,7 +950,7 @@ func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) { | |
} | ||
|
||
if !proto.Equal(&got, &want) { | ||
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", got, want) | ||
t.Errorf("message contents not set correctly after unmarshalling JSON: got %v, wanted %v", got, want) | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check seems wrong. The documentation for
Timestamp.nanos
says:Which seems to indicate a negative value for
ns
is invalid regardless ofs
value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh. The spec here says the opposite:
https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Duration
“For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field. Must be from -999,999,999 to +999,999,999 inclusive.”
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait. This is
Duration
, notTimestamp
....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, my mistake. I'm looking at
Timestamp
.Also, to what degree should we validate durations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave that up to you. If you want this CL to be consistent with the rest of the code, I can remove the validation and testing of illegal inputs here. If you want validation elsewhere, I would rather change the other places in a different commit, as this commit fixes a very real regression and it would be best to get a fix in ASAP.