Skip to content

Commit

Permalink
Merge pull request #4 from mercadolibre/fix/adjust-binary-prefixer-de…
Browse files Browse the repository at this point in the history
…coding

Fix/adjust binary prefixer decoding
  • Loading branch information
FacundoMora authored Feb 1, 2023
2 parents b0e41ef + 1adb10b commit 156b413
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions prefix/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ func (p *binaryVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, err
return 0, 0, fmt.Errorf("not enough data length: %d to read: %d bytes", len(data), p.Digits)
}

lengthBytes := data[:p.Digits]

// it take 4 bytes to encode (u)int32
uint32Size := 4

// prepend with 0x00 if len of data is less than intSize (4 bytes)
if len(data) < uint32Size {
data = append(bytes.Repeat([]byte{0x00}, uint32Size-len(data)), data...)
if len(lengthBytes) < uint32Size {
lengthBytes = append(bytes.Repeat([]byte{0x00}, uint32Size-len(lengthBytes)), lengthBytes...)
}

dataLen, err := bytesToInt(data)
dataLen, err := bytesToInt(lengthBytes)
if err != nil {
return 0, 0, fmt.Errorf("decode length: %w", err)
}
Expand Down

0 comments on commit 156b413

Please sign in to comment.