Skip to content

Commit

Permalink
Fix out-of-bounds when chunk data is exactly min chunk size (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
folbricht committed Oct 2, 2018
1 parent 773dfee commit 96ab403
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion chunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Chunker) Next() (uint64, []byte, error) {
}

// No need to carry on if we don't have enough bytes left to even fill the min chunk
if len(c.buf) < int(c.min) {
if len(c.buf) <= int(c.min) {
return c.split(len(c.buf), nil)
}

Expand Down
33 changes: 32 additions & 1 deletion chunker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func TestChunkerSmallFile(t *testing.T) {
if start != 0 {
t.Fatalf("unexpected start position %d, expected 0", start)
}

}

// There are no chunk boundaries when all data is nil, make sure we get the
Expand Down Expand Up @@ -143,6 +142,38 @@ func TestChunkerNoBoundary(t *testing.T) {
}
}

// Test with exactly min, avg, max chunk size of data
func TestChunkerBounds(t *testing.T) {
for _, c := range []struct {
name string
size uint64
}{
{"chunker with exactly min chunk size data", ChunkSizeMinDefault},
{"chunker with exactly avg chunk size data", ChunkSizeAvgDefault},
{"chunker with exactly max chunk size data", ChunkSizeMaxDefault},
} {
t.Run(c.name, func(t *testing.T) {
b := make([]byte, c.size)
r := bytes.NewReader(b)
c, err := NewChunker(r, ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault)
if err != nil {
t.Fatal(err)
}

start, buf, err := c.Next()
if err != nil {
t.Fatal(err)
}
if len(buf) != len(b) {
t.Fatalf("unexpected size %d, expected %d", len(buf), len(b))
}
if start != 0 {
t.Fatalf("unexpected start position %d, expected 0", start)
}
})
}
}

// Global vars used for results during the benchmark to prevent optimizer
// from optimizing away some operations
var (
Expand Down

0 comments on commit 96ab403

Please sign in to comment.