ugorji / go

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

decoding into slices with length mismatch #272

Closed ruz closed 6 years ago

ruz commented 6 years ago
package main

import (
    "fmt"

    "github.com/ugorji/go/codec"
)

func main() {
    var ch codec.CborHandle
    b := []byte{}
    codec.NewEncoderBytes(&b, &ch).Encode([]string{"a", "b"})

    target := ""
    res := []interface{}{&target}
    err := codec.NewDecoderBytes(b, &ch).Decode(&res)

    fmt.Println("res: ", res, "tgt: ", target, "err: ", err)
}

Can this behaviour be prevented and/or detected? What do you think? If we increase size of res to match data then target will be set. I know that this is questionable and subject to discuss.

ugorji commented 6 years ago

What was outcome? What was expected? Why don't you agree with outcome?

ruz commented 6 years ago

Outcome:

Expected variants:

I understand why it happens, but it took quite a lot of time to discover.

ugorji commented 6 years ago

I think you may be trying to be push the limits of the package, so it works in quirky ways, that may not be true from one version to the next.

The rule is that we update the interface with the appropriate value ie formerly a ptr to a string, now becomes a string per what was in the stream.

Anything else, and I will have to explain to someone else who expected the simpler model to work.

This is working as designed.

PS the error on decode is not a viable outcome, because you can decide a string into an interface holder. There if no concept of a warning in the library ie it works or fails. The only viable expected outcome is the 3rd one, but it goes against the simple way the package works.