diff --git a/pkg/download/buffer_unit_test.go b/pkg/download/buffer_unit_test.go index 2c94ecc..6ff4c78 100644 --- a/pkg/download/buffer_unit_test.go +++ b/pkg/download/buffer_unit_test.go @@ -45,7 +45,7 @@ func newTestServer(t *testing.T, content []byte) *httptest.Server { // TODO: Implement the test // func TestGetFileSizeFromContentRange(t *testing.T) {} func TestFileToBufferChunkCountExceedsMaxChunks(t *testing.T) { - contentSize := int64(humanize.KByte) + contentSize := int64(humanize.KiByte) content := generateTestContent(contentSize) server := newTestServer(t, content) defer server.Close() @@ -98,9 +98,9 @@ func TestFileToBufferChunkCountExceedsMaxChunks(t *testing.T) { maxConcurrency: 2, }, { - // humanize.KByte = 1024, remainder will result in 1024/10 = 102 chunks, max-chunks is set to 25 + // humanize.KByte = 1024, remainder will result in 1024/10 = 102 chunks, concurrency is set to 25 // resulting in a chunkSize of 41 - name: "many chunks, low maxChunks", + name: "many chunks, low maxConcurrency", chunkSize: 10, maxConcurrency: 25, }, diff --git a/pkg/download/buffered_reader.go b/pkg/download/buffered_reader.go index 973eeea..d18a430 100644 --- a/pkg/download/buffered_reader.go +++ b/pkg/download/buffered_reader.go @@ -42,6 +42,11 @@ func (b *bufferedReader) done() { func (b *bufferedReader) downloadBody(resp *http.Response) error { expectedBytes := resp.ContentLength + + if expectedBytes > int64(b.buf.Cap()) { + b.err = fmt.Errorf("Tried to download 0x%x bytes to a 0x%x-sized buffer", expectedBytes, b.buf.Cap()) + return b.err + } n, err := b.buf.ReadFrom(resp.Body) if err != nil && err != io.EOF { b.err = fmt.Errorf("error reading response for %s: %w", resp.Request.URL.String(), err)