Skip to content

Commit

Permalink
panic for < 0 shred counts
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Dec 12, 2024
1 parent 46738ec commit 79e17b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 9 additions & 3 deletions gerasure/gereedsolomon/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ type Encoder struct {
// The options within the given reedsolomon.Encoder determine the number of shards.
func NewEncoder(dataShreds, parityShreds int, opts ...reedsolomon.Option) (*Encoder, error) {
if dataShreds <= 0 {
return nil, fmt.Errorf("data shreds must be > 0")
panic(fmt.Errorf(
"BUG: attempted to create reed solomon encoder with dataShreds < 0, got %d",
dataShreds,
))
}
if parityShreds <= 0 {
return nil, fmt.Errorf("parity shreds must be > 0")
panic(fmt.Errorf(
"BUG: attempted to create reed solomon encoder with parityShreds < 0, got %d",
parityShreds,
))
}
rs, err := reedsolomon.New(dataShreds, parityShreds, opts...)
if err != nil {
Expand All @@ -31,7 +37,7 @@ func NewEncoder(dataShreds, parityShreds int, opts ...reedsolomon.Option) (*Enco

// Encode satisfies [gerasure.Encoder].
// Callers should assume that the Encoder takes ownership of the given data slice.
func (e *Encoder) Encode(_ context.Context, data []byte) ([][]byte, error) {
func (e Encoder) Encode(_ context.Context, data []byte) ([][]byte, error) {
// From the original data, produce new subslices for the data shards and parity shards.
allShards, err := e.rs.Split(data)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions gerasure/gereedsolomon/reconstructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ type Reconstructor struct {
// The shardSize and totalDataSize must be discovered out of band;
func NewReconstructor(dataShards, parityShards, shardSize int, opts ...reedsolomon.Option) (*Reconstructor, error) {
if dataShards <= 0 {
return nil, fmt.Errorf("data shards must be > 0")
panic(fmt.Errorf(
"BUG: attempted to create reed solomon encoder with dataShreds < 0, got %d",
dataShards,
))
}
if parityShards <= 0 {
return nil, fmt.Errorf("parity shards must be > 0")
panic(fmt.Errorf(
"BUG: attempted to create reed solomon encoder with parityShreds < 0, got %d",
parityShards,
))
}
rs, err := reedsolomon.New(dataShards, parityShards, opts...)
if err != nil {
Expand Down

0 comments on commit 79e17b8

Please sign in to comment.