ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.83k stars 294 forks source link

Msgpack deserialization with codecgen produces wrong result #389

Closed suzuki-safie closed 1 year ago

suzuki-safie commented 1 year ago

with github.com/ugorji/go/codec and github.com/ugorji/go/codec/codecgen v1.2.9, Msgpack deserialization with codecgen seems to produce wrong result. Prior to v1.2.8, same code worked as expected, and without codecgen it worked too.

Struct:

type S struct {
    Int  uint32 `codec:"int"`
    Flag int8   `codec:"flag"`
    Data []byte `codec:"data"`
}

Test:

package s

import (
    "encoding/hex"
    "testing"

    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
    "github.com/ugorji/go/codec"
)

func TestMsgpack(t *testing.T) {
    data, err := hex.DecodeString(
        "93" + // fixarray(3)
            "CF" + // uint64
            "0000000000000000" +
            "D0" + // int8
            "80" +
            "DA" + // str16(4)
            "0004" +
            "00000001")
    require.NoError(t, err)

    var s S
    err = codec.NewDecoderBytes(data, &codec.MsgpackHandle{}).Decode(&s)
    require.NoError(t, err)

    t.Logf("S: %#v\n", s)
    assert.Equal(t, s, S{0, -128, []byte{0x00, 0x00, 0x00, 0x01}})
}

Result without codecgen (expected):

    s_test.go:26: S: s.S{Int:0x0, Flag:-128, Data:[]uint8{0x0, 0x0, 0x0, 0x1}}

Result with codecgen (actual):

    s_test.go:26: S: s.S{Int:0x0, Flag:-128, Data:[]uint8(nil)}
ugorji commented 1 year ago

will take a look later on today.

ugorji commented 1 year ago

verified bug. will fix within next few days.

suzuki-safie commented 1 year ago

Thank you!