go-restruct / restruct

Rich binary (de)serialization library for Golang
https://restruct.io/
ISC License
360 stars 17 forks source link

Packing int or uint returns zero bytes without error #58

Open fritzr opened 1 year ago

fritzr commented 1 year ago

Unable to use restruct.Pack to pack an int or uint. See:

x := int(-100)
buf, err := restruct.Pack(binary.LittleEndian, x)
fmt.Printf("buf=%+v, err=%+v")

The result of which is:

buf=[0 0 0 0], err=<nil>

See https://go.dev/play/p/I7SFt1XI7nU.

The problem appears to be with the switch at encoder.go:289 which omits the reflect.Int and reflect.Uint cases and silently returns without touching the buffer.

I'm happy to submit a PR if you let me know which behavior is intended.

jchv commented 1 year ago

This seems like a mistake to me, it should probably be possible. Feel free to submit a PR!

edit: Though, interestingly, it also seems to be omitted from encoding/binary as well, unless I misunderstand. So, maybe it's not so clear-cut. Nonetheless, I think it's possibly more surprising that it doesn't work at all without any obvious output. This is tricky though, because implementing this either way (error message, or treating it like u/int64) will break compatibility with existing behavior AND encoding/binary. Tricky.