yerden / go-util

Utility libraries to use with Golang.
MIT License
6 stars 3 forks source link

BCD Decoding fails if source is longer than destination #2

Open rohit-zenda opened 2 years ago

rohit-zenda commented 2 years ago

The test snippet showing this failure is as follows -

func TestDecoder_SourceLongerThanDest(t *testing.T) {
    dec := NewDecoder(Standard)

    var src, dst []byte
    var n int
    var err error

    src = []byte{0x21, 0x43, 0x55}
    dst = make([]byte, DecodedLen(len(src)-1))
    n, err = dec.Decode(dst, src)
    if err != nil {
        t.Error(err)
    }
    fmt.Println(string(dst[:n]))

    src = []byte{0x21, 0x43, 0xff}
    dst = make([]byte, DecodedLen(len(src)-1))
    n, err = dec.Decode(dst, src)
    if err != nil {
        t.Error(err)
    }
    fmt.Println(string(dst[:n]))
}
rohitpmore commented 2 years ago

Raised a PR here: https://github.com/yerden/go-util/pull/3

lnbotur commented 2 years ago

At least one problem is in .../encoding/bcd.go line 38: , err := dec.Decode(dst, src) correct is: , err := dec.Decode(dst, src[:read]) Maybe there are other similar problems. I assume that BCD/binary encoding were so far not really used with this library.

rohitpmore commented 2 years ago

At least one problem is in .../encoding/bcd.go line 38: , err := dec.Decode(dst, src) correct is: , err := dec.Decode(dst, src[:read]) Maybe there are other similar problems. I assume that BCD/binary encoding were so far not really used with this library.

I guess this also solves the problem at hand. Thanks @inbotur.