diff --git a/node/store.go b/node/store.go index 5e33f66c2..33217d562 100644 --- a/node/store.go +++ b/node/store.go @@ -374,14 +374,14 @@ func (s *Store) DeleteKeys(ctx context.Context, keys *[][]byte) error { // // EncodeChunks implements encoding format "001" func EncodeChunks(chunks [][]byte) ([]byte, error) { + if len(chunks) == 0 { + return []byte{}, nil + } totalSize := 0 for _, chunk := range chunks { totalSize += len(chunk) } result := make([]byte, totalSize+8) - if len(chunks) == 0 { - return result, nil - } buf := result chunkSize := uint64(len(chunks[0])) | (1 << 61) binary.LittleEndian.PutUint64(buf, chunkSize) diff --git a/node/store_test.go b/node/store_test.go index 676944c85..2f91ff12d 100644 --- a/node/store_test.go +++ b/node/store_test.go @@ -227,20 +227,22 @@ func TestDecodeCompactChunks(t *testing.T) { _, err = node.DecodeChunks(invalid) assert.EqualError(t, err, "unrecognized chunks encoding format") data := make([]byte, 0, 9) + data = append(data, byte(2)) for i := 0; i < 6; i++ { data = append(data, byte(0)) } - data = append(data, byte(2)) data = append(data, byte(0b00100000)) - data = append(data, byte(0)) + data = append(data, byte(5)) _, err = node.DecodeChunks(data) assert.EqualError(t, err, "invalid compact data to decode") - data = append(data, byte(0)) + data = append(data, byte(6)) + data = append(data, byte(7)) + data = append(data, byte(8)) chunks, err := node.DecodeChunks(data) assert.Nil(t, err) assert.Equal(t, 2, len(chunks)) - assert.Equal(t, byte(0), chunks[0]) - assert.Equal(t, byte(1), chunks[1]) + assert.Equal(t, []byte{byte(5), byte(6)}, chunks[0]) + assert.Equal(t, []byte{byte(7), byte(8)}, chunks[1]) } func TestStoringBlob(t *testing.T) {