go-restruct / restruct

Rich binary (de)serialization library for Golang

Home Page:https://restruct.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Packing int or uint returns zero bytes without error

fritzr opened this issue · comments

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.

  • If uint and int are intentionally not implemented, this should be documented, and an error should propagate from encoder.write.
  • If uint and int are meant to be packable, this should be fixed by adding a case for them.

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

commented

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.