From b1c8b4b073c09a297ddb2df3bec09bcda4162d4e Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 5 Aug 2016 19:23:08 +0200 Subject: [PATCH] Make Join return an error if a reconstruction is required first If one or more required data shards are still nil and we can't correctly join them before a reconstruction, return ErrReconstructRequired. --- reedsolomon.go | 14 ++++++++++++++ reedsolomon_test.go | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/reedsolomon.go b/reedsolomon.go index b89a1dc..0c98981 100644 --- a/reedsolomon.go +++ b/reedsolomon.go @@ -487,12 +487,18 @@ func (r reedSolomon) Split(data []byte) ([][]byte, error) { return dst, nil } +// ErrReconstructRequired is returned if too few data shards are intact and a +// reconstruction is required before you can successfully join the shards. +var ErrReconstructRequired = errors.New("reconstruction required as one or more required data shards are nil") + // Join the shards and write the data segment to dst. // // Only the data shards are considered. // You must supply the exact output size you want. +// // If there are to few shards given, ErrTooFewShards will be returned. // If the total data size is less than outSize, ErrShortData will be returned. +// If one or more required data shards are nil, ErrReconstructRequired will be returned. func (r reedSolomon) Join(dst io.Writer, shards [][]byte, outSize int) error { // Do we have enough shards? if len(shards) < r.DataShards { @@ -503,7 +509,15 @@ func (r reedSolomon) Join(dst io.Writer, shards [][]byte, outSize int) error { // Do we have enough data? size := 0 for _, shard := range shards { + if shard == nil { + return ErrReconstructRequired + } size += len(shard) + + // Do we have enough data already? + if size >= outSize { + break + } } if size < outSize { return ErrShortData diff --git a/reedsolomon_test.go b/reedsolomon_test.go index 6cffc8c..61f8154 100644 --- a/reedsolomon_test.go +++ b/reedsolomon_test.go @@ -482,6 +482,12 @@ func TestSplitJoin(t *testing.T) { if err != ErrShortData { t.Errorf("expected %v, got %v", ErrShortData, err) } + + shards[0] = nil + err = enc.Join(buf, shards, len(data)) + if err != ErrReconstructRequired { + t.Errorf("expected %v, got %v", ErrReconstructRequired, err) + } } func TestCodeSomeShards(t *testing.T) {