Skip to content

Commit

Permalink
Fix short unknown data size read (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Dec 20, 2019
1 parent 6a60d21 commit b80e73d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64,
return nil, ErrUnknownElement
}

size, nb, err := readVInt(r)
size, nb, err := readDataSize(r)
headerSize += uint64(nb)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ var perTypeReader = map[DataType]func(io.Reader, uint64) (interface{}, error){
DataTypeBlock: readBlock,
}

func readDataSize(r io.Reader) (uint64, int, error) {
v, n, err := readVInt(r)
if v == (uint64(0xFFFFFFFFFFFFFFFF) >> uint(64-n*7)) {
return SizeUnknown, n, err
}
return v, n, err
}
func readVInt(r io.Reader) (uint64, int, error) {
var bs [1]byte
bytesRead, err := io.ReadFull(r, bs[:])
Expand Down
38 changes: 37 additions & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDataSize(t *testing.T) {
}

for n, c := range testCases {
t.Run("Decode "+n, func(t *testing.T) {
t.Run("DecodeVInt "+n, func(t *testing.T) {
r, _, err := readVInt(bytes.NewBuffer(c.b))
if err != nil {
t.Fatalf("Failed to readVInt: %v", err)
Expand All @@ -42,6 +42,17 @@ func TestDataSize(t *testing.T) {
}
})
}
for n, c := range testCases {
t.Run("DecodeDataSize "+n, func(t *testing.T) {
r, _, err := readDataSize(bytes.NewBuffer(c.b))
if err != nil {
t.Fatalf("Failed to readDataSize: %v", err)
}
if r != c.i {
t.Errorf("Unexpected readVInt result, expected: %d, got: %d", c.i, r)
}
})
}
for n, c := range testCases {
t.Run("Encode "+n, func(t *testing.T) {
b := encodeDataSize(c.i, 0)
Expand All @@ -52,6 +63,31 @@ func TestDataSize(t *testing.T) {
}
}

func TestDataSize_Unknown(t *testing.T) {
testCases := map[string][]byte{
"1 byte": []byte{0xFF},
"2 bytes": []byte{0x7F, 0xFF},
"3 bytes": []byte{0x3F, 0xFF, 0xFF},
"4 bytes": []byte{0x1F, 0xFF, 0xFF, 0xFF},
"5 bytes": []byte{0x0F, 0xFF, 0xFF, 0xFF, 0xFF},
"6 bytes": []byte{0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
"7 bytes": []byte{0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
"8 bytes": []byte{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
}

for n, b := range testCases {
t.Run("DecodeDataSize "+n, func(t *testing.T) {
r, _, err := readDataSize(bytes.NewBuffer(b))
if err != nil {
t.Fatalf("Failed to readDataSize: %v", err)
}
if r != SizeUnknown {
t.Errorf("Unexpected readDataSize result, expected: %d, got: %d", SizeUnknown, r)
}
})
}
}

func TestElementID(t *testing.T) {
testCases := map[string]struct {
b []byte
Expand Down

0 comments on commit b80e73d

Please sign in to comment.