Add checks for data and parity to not exceed 255 shards in total.

Fixes #16
master
Harshavardhana 2016-06-03 01:07:27 -07:00
parent 4fadad8564
commit ba30981088
3 changed files with 14 additions and 10 deletions

View File

@ -85,10 +85,14 @@ type reedSolomon struct {
}
// ErrInvShardNum will be returned by New, if you attempt to create
// an Encoder where either data or parity shards is zero or less,
// or the number of data shards is higher than 256.
// an Encoder where either data or parity shards is zero or less.
var ErrInvShardNum = errors.New("cannot create Encoder with zero or less data/parity shards")
// ErrMaxShardNum will be returned by New, if you attempt to create
// an Encoder where data and parity shards cannot be bigger than
// Galois field GF(2^8) - 1.
var ErrMaxShardNum = errors.New("cannot create Encoder with 255 or more data+parity shards")
// New creates a new encoder and initializes it to
// the number of data shards and parity shards that
// you want to use. You can reuse this encoder.
@ -104,8 +108,8 @@ func New(dataShards, parityShards int) (Encoder, error) {
return nil, ErrInvShardNum
}
if dataShards > 256 {
return nil, ErrInvShardNum
if dataShards+parityShards > 255 {
return nil, ErrMaxShardNum
}
// Start with a Vandermonde matrix. This matrix would work,

View File

@ -517,12 +517,12 @@ func TestNew(t *testing.T) {
data, parity int
err error
}{
{10, 500, nil},
{256, 256, nil},
{127, 127, nil},
{256, 256, ErrMaxShardNum},
{0, 1, ErrInvShardNum},
{1, 0, ErrInvShardNum},
{257, 1, ErrInvShardNum},
{257, 1, ErrMaxShardNum},
// overflow causes r.Shards to be negative
{256, int(^uint(0) >> 1), errInvalidRowSize},

View File

@ -585,12 +585,12 @@ func TestNewStream(t *testing.T) {
data, parity int
err error
}{
{10, 500, nil},
{256, 256, nil},
{127, 127, nil},
{256, 256, ErrMaxShardNum},
{0, 1, ErrInvShardNum},
{1, 0, ErrInvShardNum},
{257, 1, ErrInvShardNum},
{257, 1, ErrMaxShardNum},
// overflow causes r.Shards to be negative
{256, int(^uint(0) >> 1), errInvalidRowSize},