Skip to content

Commit

Permalink
zero memery padding on stream split, fix potential OOM issues. (#233)
Browse files Browse the repository at this point in the history
Co-authored-by: Bill.w <108987079+0xdadaking@users.noreply.github.com>
  • Loading branch information
0xbillw and 0xdadak committed Jan 17, 2023
1 parent b413df3 commit 6580cd4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package reedsolomon

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -585,8 +584,8 @@ func (r *rsStream) Split(data io.Reader, dst []io.Writer, size int64) error {
perShard := (size + int64(r.r.dataShards) - 1) / int64(r.r.dataShards)

// Pad data to r.Shards*perShard.
padding := make([]byte, (int64(r.r.totalShards)*perShard)-size)
data = io.MultiReader(data, bytes.NewBuffer(padding))
paddingSize := (int64(r.r.totalShards) * perShard) - size
data = io.MultiReader(data, io.LimitReader(zeroPaddingReader{}, paddingSize))

// Split into equal-length shards and copy.
for i := range dst {
Expand All @@ -601,3 +600,15 @@ func (r *rsStream) Split(data io.Reader, dst []io.Writer, size int64) error {

return nil
}

type zeroPaddingReader struct{}

var _ io.Reader = &zeroPaddingReader{}

func (t zeroPaddingReader) Read(p []byte) (n int, err error) {
n = len(p)
for i := 0; i < n; i++ {
p[i] = 0
}
return n, nil
}

0 comments on commit 6580cd4

Please sign in to comment.