From 79e17b8790bafd63506b26d2ecd3797d9bf8b537 Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Thu, 12 Dec 2024 08:50:59 -0700 Subject: [PATCH] panic for < 0 shred counts --- gerasure/gereedsolomon/encoder.go | 12 +++++++++--- gerasure/gereedsolomon/reconstructor.go | 10 ++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/gerasure/gereedsolomon/encoder.go b/gerasure/gereedsolomon/encoder.go index cf78cc3..fa98bce 100644 --- a/gerasure/gereedsolomon/encoder.go +++ b/gerasure/gereedsolomon/encoder.go @@ -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 { @@ -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 { diff --git a/gerasure/gereedsolomon/reconstructor.go b/gerasure/gereedsolomon/reconstructor.go index 2ea8f82..6d5b696 100644 --- a/gerasure/gereedsolomon/reconstructor.go +++ b/gerasure/gereedsolomon/reconstructor.go @@ -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 {