From d68c336cbb4932de47051a2ca006cbf598bf6b4d Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 10 Nov 2020 12:00:28 +0200 Subject: [PATCH] prevent anyone from accidentally setting the blocksize to zero Signed-off-by: Avi Deitcher --- pkg/content/opts.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/content/opts.go b/pkg/content/opts.go index 5915a6b61..007e6a009 100644 --- a/pkg/content/opts.go +++ b/pkg/content/opts.go @@ -1,6 +1,8 @@ package content import ( + "errors" + "github.com/opencontainers/go-digest" ) @@ -47,9 +49,13 @@ func WithOutputHash(hash digest.Digest) WriterOpt { } // WithBlocksize set the blocksize used by the processor of data. -// The default is DefaultBlocksize, which is the same as that used by io.Copy +// The default is DefaultBlocksize, which is the same as that used by io.Copy. +// Includes a safety check to ensure the caller doesn't actively set it to <= 0. func WithBlocksize(blocksize int) WriterOpt { return func(w *WriterOpts) error { + if blocksize <= 0 { + return errors.New("blocksize must be greater than or equal to 0") + } w.Blocksize = blocksize return nil }