mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-26 09:30:36 +08:00
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:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user