Fix incorrect block size due to integer division
Fail-fast if the block size exceeds the limit, preventing an upload that
is guaranteed to fail and leaves lingering unstaged blocks.
@@ -321,6 +321,10 @@ func (bb *Client) uploadFromReader(ctx context.Context, reader io.ReaderAt, read
if readerSize <= MaxUploadBlobBytes {
o.BlockSize = MaxUploadBlobBytes // Default if unspecified
} else {
+ if remainder := readerSize % MaxBlocks; remainder > 0 {
+ // ensure readerSize is a multiple of MaxBlocks
+ readerSize += (MaxBlocks - remainder)
+ }
o.BlockSize = readerSize / MaxBlocks // buffer / max blocks = block size to use all 50,000 blocks
if o.BlockSize < blob.DefaultDownloadBlockSize { // If the block size is smaller than 4MB, round up to 4MB
o.BlockSize = blob.DefaultDownloadBlockSize
@@ -343,6 +347,10 @@ func (bb *Client) uploadFromReader(ctx context.Context, reader io.ReaderAt, read
}
var numBlocks = uint16(((readerSize - 1) / o.BlockSize) + 1)
+ if numBlocks > MaxBlocks {
+ // prevent any math bugs from attempting to upload too many blocks which will always fail
+ return uploadFromReaderResponse{}, errors.New("block limit exceeded")
+ }
blockIDList := make([]string, numBlocks) // Base-64 encoded block IDs
progress := int64(0)
