Skip to content

Commit

Permalink
Fix Split function with 0 erasure (#234)
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
klauspost committed Jan 17, 2023
1 parent 6580cd4 commit 7e59db9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
8 changes: 6 additions & 2 deletions leopard.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ func (r *leopardFF16) Split(data []byte) ([][]byte, error) {
if len(data) == 0 {
return nil, ErrShortData
}
if r.totalShards == 1 && len(data)&63 == 0 {
return [][]byte{data}, nil
}
dataLen := len(data)
// Calculate number of bytes per data shard.
perShard := (len(data) + r.dataShards - 1) / r.dataShards
Expand All @@ -295,8 +298,9 @@ func (r *leopardFF16) Split(data []byte) ([][]byte, error) {
copyFrom = copyFrom[copy(padding[i], copyFrom):]
}
} else {
for i := dataLen; i < dataLen+r.dataShards; i++ {
data[i] = 0
zero := data[dataLen : r.totalShards*perShard]
for i := range zero {
zero[i] = 0
}
}

Expand Down
9 changes: 7 additions & 2 deletions leopard8.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ func (r *leopardFF8) Split(data []byte) ([][]byte, error) {
if len(data) == 0 {
return nil, ErrShortData
}
if r.totalShards == 1 && len(data)&63 == 0 {
return [][]byte{data}, nil
}

dataLen := len(data)
// Calculate number of bytes per data shard.
perShard := (len(data) + r.dataShards - 1) / r.dataShards
Expand All @@ -335,8 +339,9 @@ func (r *leopardFF8) Split(data []byte) ([][]byte, error) {
copyFrom = copyFrom[copy(padding[i], copyFrom):]
}
} else {
for i := dataLen; i < dataLen+r.dataShards; i++ {
data[i] = 0
zero := data[dataLen : r.totalShards*perShard]
for i := range zero {
zero[i] = 0
}
}

Expand Down
9 changes: 7 additions & 2 deletions reedsolomon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,10 @@ func (r *reedSolomon) Split(data []byte) ([][]byte, error) {
if len(data) == 0 {
return nil, ErrShortData
}
if r.totalShards == 1 {
return [][]byte{data}, nil
}

dataLen := len(data)
// Calculate number of bytes per data shard.
perShard := (len(data) + r.dataShards - 1) / r.dataShards
Expand All @@ -1574,8 +1578,9 @@ func (r *reedSolomon) Split(data []byte) ([][]byte, error) {
}
data = data[0 : perShard*fullShards]
} else {
for i := dataLen; i < dataLen+r.dataShards; i++ {
data[i] = 0
zero := data[dataLen : r.totalShards*perShard]
for i := range zero {
zero[i] = 0
}
}

Expand Down
14 changes: 14 additions & 0 deletions reedsolomon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,20 @@ func TestNew(t *testing.T) {
}
}

func TestSplitZero(t *testing.T) {
data := make([]byte, 512)
for _, opts := range testOpts() {
ecctest, err := New(1, 0, opts...)
if err != nil {
t.Fatal(err)
}
_, err = ecctest.Split(data)
if err != nil {
t.Fatal(err)
}
}
}

// Benchmark 10 data shards and 4 parity shards and 160MB data.
func BenchmarkSplit10x4x160M(b *testing.B) {
benchmarkSplit(b, 10, 4, 160*1024*1024)
Expand Down

0 comments on commit 7e59db9

Please sign in to comment.