From 158bb9bbd588aa03a3567f40b367025ccbd81fb3 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Thu, 17 Nov 2016 18:30:35 +0000 Subject: [PATCH] Fix use of cap in MultiReadSeeker Signed-off-by: Tom Wilkie --- pkg/ioutils/multireader.go | 2 +- pkg/ioutils/multireader_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/ioutils/multireader.go b/pkg/ioutils/multireader.go index 234999bc92..0806552838 100644 --- a/pkg/ioutils/multireader.go +++ b/pkg/ioutils/multireader.go @@ -155,7 +155,7 @@ func (r *multiReadSeeker) Read(b []byte) (int, error) { r.pos = &pos{0, 0} } - bCap := int64(cap(b)) + bCap := int64(len(b)) buf := bytes.NewBuffer(nil) var rdr io.ReadSeeker diff --git a/pkg/ioutils/multireader_test.go b/pkg/ioutils/multireader_test.go index 306f5b0e1f..65309a9565 100644 --- a/pkg/ioutils/multireader_test.go +++ b/pkg/ioutils/multireader_test.go @@ -2,6 +2,7 @@ package ioutils import ( "bytes" + "encoding/binary" "fmt" "io" "io/ioutil" @@ -188,3 +189,23 @@ func TestMultiReadSeekerCurAfterSet(t *testing.T) { t.Fatalf("reader size does not match, got %d, expected %d", size, mid+18) } } + +func TestMultiReadSeekerSmallReads(t *testing.T) { + readers := []io.ReadSeeker{} + for i := 0; i < 10; i++ { + integer := make([]byte, 4, 4) + binary.BigEndian.PutUint32(integer, uint32(i)) + readers = append(readers, bytes.NewReader(integer)) + } + + reader := MultiReadSeeker(readers...) + for i := 0; i < 10; i++ { + var integer uint32 + if err := binary.Read(reader, binary.BigEndian, &integer); err != nil { + t.Fatalf("Read from NewMultiReadSeeker failed: %v", err) + } + if uint32(i) != integer { + t.Fatalf("Read wrong value from NewMultiReadSeeker: %d != %d", i, integer) + } + } +}