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

add slice size check to unmarshaler.unmarshalList() #5331

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
### SDK Enhancements

### SDK Bugs
* Fix an issue where `jsonutil.UnmarshalJSON()` is called with an insufficient size slice.
7 changes: 5 additions & 2 deletions private/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,12 @@ func (u unmarshaler) unmarshalList(value reflect.Value, data interface{}, tag re
return fmt.Errorf("JSON value is not a list (%#v)", data)
}

if value.IsNil() {
l := len(listData)
if l := len(listData); value.IsNil() {
value.Set(reflect.MakeSlice(value.Type(), l, l))
} else {
if value.Kind() != reflect.Slice || value.Len() < len(listData) {
Copy link
Author

Choose a reason for hiding this comment

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

Perhaps we could simply replace the list with a correctly sized one, rather than emitting an error.​

return fmt.Errorf("existing slice not big enough to hold list data (%#v)", data)
}
}

for i, c := range listData {
Expand Down
14 changes: 14 additions & 0 deletions private/protocol/json/jsonutil/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,17 @@ func TestUnmarshalJSON_JSONNumber(t *testing.T) {
})
}
}

func TestUnmarshalJSON_SliceNotBigEnough(t *testing.T) {
type input struct {
ListField []*int64 `locationName:"listField" type:"list"`
}
JSON := `{"listField": [1]}`
value := input{
ListField: make([]*int64, 0),
}
err := jsonutil.UnmarshalJSON(&value, bytes.NewReader([]byte(JSON)))
if err == nil {
t.Error("expect error, got nil")
}
}