Fix encodeLength for 32-bit platforms

When `int` is 32 bits, `MaxInt64` doesn't fit. It's apparent that
`encodeLength` expects to handle 64-bit inputs, so let's make that
explicit, which allows the test to run on all platforms.
This commit is contained in:
Rob Kennedy
2022-01-25 00:03:50 -06:00
parent fce495f83e
commit 1c408d05be
2 changed files with 3 additions and 3 deletions

View File

@@ -16,7 +16,7 @@ type FixedHeader struct {
// Encode encodes the FixedHeader and returns a bytes buffer. // Encode encodes the FixedHeader and returns a bytes buffer.
func (fh *FixedHeader) Encode(buf *bytes.Buffer) { func (fh *FixedHeader) Encode(buf *bytes.Buffer) {
buf.WriteByte(fh.Type<<4 | encodeBool(fh.Dup)<<3 | fh.Qos<<1 | encodeBool(fh.Retain)) buf.WriteByte(fh.Type<<4 | encodeBool(fh.Dup)<<3 | fh.Qos<<1 | encodeBool(fh.Retain))
encodeLength(buf, fh.Remaining) encodeLength(buf, int64(fh.Remaining))
} }
// decode extracts the specification bits from the header byte. // decode extracts the specification bits from the header byte.
@@ -44,7 +44,7 @@ func (fh *FixedHeader) Decode(headerByte byte) error {
} }
// encodeLength writes length bits for the header. // encodeLength writes length bits for the header.
func encodeLength(buf *bytes.Buffer, length int) { func encodeLength(buf *bytes.Buffer, length int64) {
for { for {
digit := byte(length % 128) digit := byte(length % 128)
length /= 128 length /= 128

View File

@@ -192,7 +192,7 @@ func BenchmarkFixedHeaderDecode(b *testing.B) {
func TestEncodeLength(t *testing.T) { func TestEncodeLength(t *testing.T) {
tt := []struct { tt := []struct {
have int have int64
want []byte want []byte
}{ }{
{ {