Skip to content

Commit

Permalink
Ensure that end of __ref map is consumed by JSON decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
thallgren committed Mar 5, 2020
1 parent 0505576 commit a7812d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions streamer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func (j *jsonDecoder) decodeCollection(delim json.Delim) {
if n, ok := j.nextToken().(json.Number); ok {
if ri, err := n.Int64(); err == nil {
j.consumer.AddRef(int(ri))
if j.nextToken() != json.Delim('}') {
panic(fmt.Errorf(`expected end of object after "%s": %d`, j.refKey, ri))
}
return
}
}
Expand Down
10 changes: 8 additions & 2 deletions streamer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestJSON_ComplexKeys(t *testing.T) {

func TestUnmarshalJSON_ref(t *testing.T) {
v := streamer.UnmarshalJSON(
[]byte(`{"x": {"z": ["a","b"]}, "y": {"__ref":6}}`),
[]byte(`[{"x":"xxxxxxxxxxxxxxxxxxxxx","y":{"__ref":3}}]`),
streamer.DgoDialect())
require.Equal(t, vf.Map(`x`, vf.Map(`z`, vf.Values("a", `b`)), `y`, `b`), v)
require.Equal(t, vf.Values(vf.Map(`x`, `xxxxxxxxxxxxxxxxxxxxx`, `y`, `xxxxxxxxxxxxxxxxxxxxx`)), v)
}

func TestUnmarshalJSON_refBad(t *testing.T) {
Expand All @@ -81,6 +81,12 @@ func TestUnmarshalJSON_refBad(t *testing.T) {
}, `expected integer after key "__ref"`)
}

func TestUnmarshalJSON_refSuperfluous(t *testing.T) {
require.Panic(t, func() {
streamer.UnmarshalJSON([]byte(`{"x": {"z": ["a","b"]}, "y": {"__ref":6, "x": 1}}`), streamer.DgoDialect())
}, `expected end of object after "__ref": 6`)
}

func TestUnmarshalJSON_badDelim(t *testing.T) {
require.Panic(t, func() {
streamer.UnmarshalJSON([]byte(``), streamer.DgoDialect())
Expand Down

0 comments on commit a7812d7

Please sign in to comment.