-
Notifications
You must be signed in to change notification settings - Fork 0
fix: clamp DecodeUntypedMap allocation at maxMapSize #76
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
base: v6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -75,6 +75,26 @@ func (t *MsgpackTest) TestLargeString() { | |||||
| t.Equal(dst, src) | ||||||
| } | ||||||
|
|
||||||
| func (t *MsgpackTest) TestDecodeUntypedMapHugeDeclaredLen() { | ||||||
| // A map32 header declaring ~4G entries with no payload: the map size | ||||||
| // hint must be clamped at maxMapSize before allocation (with the old | ||||||
| // code this allocated a multi-GB map upfront), then fail decoding the | ||||||
| // first key. | ||||||
| data := []byte{0xdf, 0xff, 0xff, 0xff, 0xff} | ||||||
|
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. On 32-bit architectures, Using
Suggested change
|
||||||
| dec := msgpack.NewDecoder(bytes.NewReader(data)) | ||||||
| _, err := dec.DecodeUntypedMap() | ||||||
| t.NotNil(err) | ||||||
| } | ||||||
|
|
||||||
| func (t *MsgpackTest) TestDecodeUntypedMap() { | ||||||
| in := map[interface{}]interface{}{int8(1): "one", "two": int8(2)} | ||||||
| t.Nil(t.enc.Encode(in)) | ||||||
|
|
||||||
| out, err := t.dec.DecodeUntypedMap() | ||||||
| t.Nil(err) | ||||||
| t.Equal(in, out) | ||||||
| } | ||||||
|
|
||||||
| func (t *MsgpackTest) TestSliceOfStructs() { | ||||||
| in := []*nameStruct{{"hello"}} | ||||||
| var out []*nameStruct | ||||||
|
|
||||||
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.
On 32-bit architectures, an extremely large declared map size (e.g.,
0x80000000/ 2147483648) can overflow the signed 32-bitintand result in a negative value other than-1(e.g.,-2147483648). This bypasses then == -1check, leading to a negativelnvalue and causingmake(map[interface{}]interface{}, ln)to panic withpanic: size out of range.To prevent this panic, we should check if
n < -1and return an error.Note: The other map decoding paths (
decodeMapValue,decodeMapStringStringPtr, anddecodeTypedMapN) also suffer from this same overflow panic on 32-bit systems and should be updated similarly.