ugorji / go

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

failed to decode long []byte with BincHandle #36

Closed hillbig closed 10 years ago

hillbig commented 10 years ago

Hi, I often use your great library, thanks.

I encountered a failure in decoding long []byte with BincHandle. The following code is an example to reproduce the failure. I expect that the both lengths should be 65536, but the length of decoded one is 256.

package main

import (
    "fmt"
    "github.com/ugorji/go/codec"
    "os"
)

func main() {
    b := make([]byte, 0)
    var h codec.BincHandle
    enc := codec.NewEncoderBytes(&b, &h)
    sample := make([]byte, 65536)
    err := enc.Encode(sample)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    dec := codec.NewDecoderBytes(b, &h)
    sampleDecoded := make([]byte, 0)
    err = dec.Decode(&sample_test)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Printf("len=%d\n", len(sample))
    fmt.Printf("len=%d\n", len(sampleDecoded)
}
hillbig commented 10 years ago

I found that, encLen calls encLenNumber at encoding. https://github.com/ugorji/go/blob/master/codec/binc.go#L288 however, decLen calls decUint at decoding, in which different decoding is applied. https://github.com/ugorji/go/blob/master/codec/binc.go#L571

ugorji commented 10 years ago

Thanks so much. Fix coming pronto.