From 7f673ac70a88e6b534ff67f6b1584d8cca39d265 Mon Sep 17 00:00:00 2001 From: buzzcosm <97390468+buzzcosm@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:58:47 +0100 Subject: [PATCH] Fix int overflow of the chunk offset in uploadLarge for files larger than 2 GiB uploadLargeParts computed the start offset of each chunk as bufferSize * partNumber with two ints and widened the result to long afterwards. Once the offset passes 2^31 bytes the multiplication overflows, the Content-Range header gets a negative start offset and the upload fails with "Invalid Content-Range header". Widen to long before multiplying and add an offline test that uploads 2.3 GB through uploadLarge and checks the Content-Range of every chunk. --- .../main/java/com/cloudinary/Uploader.java | 2 +- .../cloudinary/UploaderUploadLargeTest.java | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 cloudinary-core/src/test/java/com/cloudinary/UploaderUploadLargeTest.java diff --git a/cloudinary-core/src/main/java/com/cloudinary/Uploader.java b/cloudinary-core/src/main/java/com/cloudinary/Uploader.java index 39b21950..a6109ff7 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/Uploader.java +++ b/cloudinary-core/src/main/java/com/cloudinary/Uploader.java @@ -183,7 +183,7 @@ private Map uploadLargeParts(InputStream input, Map options, int bufferSize, lon if (atEnd || fullBuffer) { totalBytes += currentBufferSize; - long currentLoc = offset + bufferSize * partNumber; + long currentLoc = offset + (long) bufferSize * partNumber; if (!atEnd) { //verify not on end - try read another byte bytesRead = input.read(nibbleBuffer, 0, 1); diff --git a/cloudinary-core/src/test/java/com/cloudinary/UploaderUploadLargeTest.java b/cloudinary-core/src/test/java/com/cloudinary/UploaderUploadLargeTest.java new file mode 100644 index 00000000..32ecfb96 --- /dev/null +++ b/cloudinary-core/src/test/java/com/cloudinary/UploaderUploadLargeTest.java @@ -0,0 +1,84 @@ +package com.cloudinary; + +import com.cloudinary.strategies.AbstractUploaderStrategy; +import com.cloudinary.utils.ObjectUtils; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class UploaderUploadLargeTest { + private Cloudinary cloudinary; + + @Before + public void setUp() { + cloudinary = new Cloudinary("cloudinary://a:b@test123?load_strategies=false&analytics=false"); + } + + @Test + public void testUploadLargeContentRangeBeyondIntRange() throws IOException { + // Offsets past Integer.MAX_VALUE must not overflow into negative Content-Range values. + final int chunkSize = 20 * 1024 * 1024; + final long totalSize = 2300000000L; + RecordingUploaderStrategy strategy = new RecordingUploaderStrategy(); + Uploader uploader = new Uploader(cloudinary, strategy); + + uploader.uploadLarge(new SizedInputStream(totalSize), new HashMap(), chunkSize); + + List ranges = strategy.contentRanges; + int chunks = (int) ((totalSize + chunkSize - 1) / chunkSize); + assertEquals(chunks, ranges.size()); + for (int i = 0; i < chunks; i++) { + String expectedPrefix = "bytes " + ((long) i * chunkSize) + "-"; + assertTrue("Content-Range of chunk " + i + " should start with '" + expectedPrefix + "' but was '" + ranges.get(i) + "'", + ranges.get(i).startsWith(expectedPrefix)); + } + long lastStart = (long) (chunks - 1) * chunkSize; + assertEquals("bytes " + lastStart + "-" + (totalSize - 1) + "/" + totalSize, ranges.get(chunks - 1)); + } + + private static class RecordingUploaderStrategy extends AbstractUploaderStrategy { + final List contentRanges = new ArrayList(); + + @Override + public Map callApi(String action, Map params, Map options, Object file, ProgressCallback progressCallback) { + Map extraHeaders = (Map) options.get("extra_headers"); + contentRanges.add((String) extraHeaders.get("Content-Range")); + return ObjectUtils.asMap("public_id", "test"); + } + } + + /** + * Reports a fixed number of bytes without filling the buffer, so large sizes cost no memory. + */ + private static class SizedInputStream extends InputStream { + private long remaining; + + SizedInputStream(long size) { + this.remaining = size; + } + + @Override + public int read() { + if (remaining <= 0) return -1; + remaining--; + return 0; + } + + @Override + public int read(byte[] b, int off, int len) { + if (remaining <= 0) return -1; + int count = (int) Math.min(len, remaining); + remaining -= count; + return count; + } + } +}