Skip to content

Commit

Permalink
Fix binaryVarPrefixer to work with more bytes than prefix (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
FacundoMora authored Feb 1, 2023
1 parent bb0b3da commit 2365855
Show file tree
Hide file tree
Showing 2 changed files with 31 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)
}

prefBytes := 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(prefBytes) < uint32Size {
prefBytes = append(bytes.Repeat([]byte{0x00}, uint32Size-len(prefBytes)), prefBytes...)
}

dataLen, err := bytesToInt(data)
dataLen, err := bytesToInt(prefBytes)
if err != nil {
return 0, 0, fmt.Errorf("decode length: %w", err)
}
Expand Down
26 changes: 26 additions & 0 deletions prefix/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ func Test_binaryVarPrefixer_DecodeLength(t *testing.T) {
wantRead: 1,
wantErr: false,
},
{
name: "success(L)_withMoreData",
fields: fields{
Digits: 1,
},
args: args{
maxLen: 32,
data: []byte{0x18, 0x0, 0x0, 0x0},
},
wantDataLen: 24,
wantRead: 1,
wantErr: false,
},
{
name: "success(LLL)",
fields: fields{
Expand All @@ -165,6 +178,19 @@ func Test_binaryVarPrefixer_DecodeLength(t *testing.T) {
wantRead: 3,
wantErr: false,
},
{
name: "success(LLL)_withMoreData",
fields: fields{
Digits: 3,
},
args: args{
maxLen: 19999999,
data: []byte{0xab, 0xcb, 0xff, 0x0, 0x0, 0x0},
},
wantDataLen: 11258879,
wantRead: 3,
wantErr: false,
},
{
name: "not_enough_data",
fields: fields{
Expand Down

0 comments on commit 2365855

Please sign in to comment.