Skip to content
Open
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
10 changes: 10 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
disallowUnknownFieldsFlag
usePreallocateValues
disableAllocLimitFlag
ignoreStructLength
)

type bufReader interface {
Expand Down Expand Up @@ -261,6 +262,15 @@ func (d *Decoder) DisableAllocLimit(on bool) {
}
}

// IgnoreStructLength disables field count verification during parsing
func (d *Decoder) IgnoreStructLength(on bool) {
if on {
d.flags |= ignoreStructLength
} else {
d.flags &= ^ignoreStructLength
}
}

// Buffered returns a reader of the data remaining in the Decoder's buffer.
// The reader is valid until the next call to Decode.
func (d *Decoder) Buffered() io.Reader {
Expand Down
9 changes: 8 additions & 1 deletion decode_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,22 @@ func decodeStructValue(d *Decoder, v reflect.Value) error {
}

fields := structs.Fields(v.Type(), d.structTag)
if n != len(fields.List) {
if d.flags&ignoreStructLength == 0 && n != len(fields.List) {
return errArrayStruct
}

for _, f := range fields.List {
n--
if n < 0 {
break
}
if err := f.DecodeValue(d, v); err != nil {
return err
}
}
if n > 0 {
d.skipNext(n)
}

return nil
}
Expand Down
25 changes: 25 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (o *Object) UnmarshalMsgpack(b []byte) error {

//------------------------------------------------------------------------------

const (
ignoreStructLength uint32 = 1 << iota
)

//------------------------------------------------------------------------------

type CustomTime time.Time

func (t CustomTime) EncodeMsgpack(enc *msgpack.Encoder) error {
Expand Down Expand Up @@ -451,6 +457,7 @@ type typeTest struct {
out interface{}
encErr string
decErr string
decFlags uint32
wantnil bool
wantzero bool
wanted interface{}
Expand Down Expand Up @@ -631,6 +638,18 @@ var (
out: new(unexported),
decErr: "msgpack: number of fields in array-encoded struct has changed",
},
{
in: OmitEmptyTest{"foo", "bar"},
out: new(unexported),
wanted: unexported{Foo: "foo"},
decFlags: ignoreStructLength,
},
{
in: unexported{Foo: "foo"},
out: new(OmitEmptyTest),
wanted: OmitEmptyTest{"foo", ""},
decFlags: ignoreStructLength,
},

{in: (*EventTime)(nil), out: new(*EventTime)},
{in: &EventTime{time.Unix(0, 0)}, out: new(*EventTime)},
Expand Down Expand Up @@ -690,6 +709,9 @@ func TestTypes(t *testing.T) {
}

dec := msgpack.NewDecoder(&buf)
if test.decFlags&ignoreStructLength != 0 {
dec.IgnoreStructLength(true)
}
err = dec.Decode(test.out)
if test.decErr != "" {
test.requireErr(err, test.decErr)
Expand Down Expand Up @@ -737,6 +759,9 @@ func TestTypes(t *testing.T) {

var dst interface{}
dec := msgpack.NewDecoder(bytes.NewReader(b))
if test.decFlags&ignoreStructLength != 0 {
dec.IgnoreStructLength(true)
}
dec.SetMapDecoder(func(dec *msgpack.Decoder) (interface{}, error) {
return dec.DecodeUntypedMap()
})
Expand Down