Skip to content

uploadLarge sends a negative Content-Range start offset for files larger than 2 GiB (int overflow in Uploader.uploadLargeParts) #394

Open

Description

@buzzcosm

Bug report for Cloudinary Java SDK

Reproduced with the latest release (2.6.0) and on master.

Describe the bug in a sentence or two.

Uploader.uploadLarge(...) computes the start offset of each chunk with an int * int multiplication. Once the offset passes 2^31 bytes (2,147,483,648), the multiplication overflows, the Content-Range header of that chunk gets a negative start offset, and Cloudinary rejects the request with Invalid Content-Range header. Files larger than roughly 2.1 GB cannot be uploaded with uploadLarge, regardless of the account's file size limit.

Issue Type (Can be multiple)

[ ] Build - Can’t install or import the SDK
[ ] Performance - Performance issues
[x] Behaviour - Functions aren’t working as expected (Such as generate URL)
[ ] Documentation - Inconsistency between the docs and behaviour
[ ] Other (Specify)

Affected code

cloudinary-core/src/main/java/com/cloudinary/Uploader.java, method uploadLargeParts (line 186 on master):

long currentLoc = offset + bufferSize * partNumber;

bufferSize and partNumber are both int, so bufferSize * partNumber is evaluated in 32-bit arithmetic and only then widened to long. The same line is present in cloudinary-core 2.0.0, 2.6.0 and on master.

Impact

The first chunk whose start offset reaches 2^31 gets a negative start. With chunk size c, that is chunk index ceil(2^31 / c), so every file larger than ceil(2^31 / c) * c bytes fails:

chunkSize First broken chunk (index) Files larger than this fail
6,000,000 358 (start 2,148,000,000) 2,148,000,000 bytes
20,971,520 (20 MiB) 103 (start 2,160,066,560) 2,160,066,560 bytes
100,000,000 22 (start 2,200,000,000) 2,200,000,000 bytes

Example of a broken header, 20 MiB chunks, file of 2,505,886,269 bytes (index 103):
Content-Range: bytes -2134900736--2113929217/2505886269. The start is the real offset 2,160,066,560 minus 2^32. Increasing the chunk size does not help, it only moves the limit slightly.

We observed this against a real Cloudinary product environment with a 2.26 GB video and 20 MiB chunks: the upload failed with java.lang.RuntimeException: Invalid Content-Range header. The same file uploaded successfully when we sent the chunks ourselves with the offsets calculated as long.

Steps to reproduce

No account and no network access needed. The example uses a sparse file (no disk space) and a local HTTP server standing in for the upload endpoint (via upload_prefix) that only prints the Content-Range header of every request.

import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
import com.sun.net.httpserver.HttpServer;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class Repro {
    public static void main(String[] args) throws Exception {
        // Sparse file of 2.3 GB: takes no disk space, but is larger than 2^31 bytes.
        File file = File.createTempFile("big", ".bin");
        file.deleteOnExit();
        try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
            raf.setLength(2_300_000_000L);
        }

        // Local stand-in for the upload endpoint that only logs the Content-Range header.
        HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
        server.createContext("/", ex -> {
            byte[] buffer = new byte[1 << 16];
            while (ex.getRequestBody().read(buffer) >= 0) { /* discard body */ }
            System.out.println("Content-Range: " + ex.getRequestHeaders().getFirst("Content-Range"));
            byte[] body = "{\"public_id\":\"x\",\"bytes\":0}".getBytes(StandardCharsets.UTF_8);
            ex.sendResponseHeaders(200, body.length);
            ex.getResponseBody().write(body);
            ex.close();
        });
        server.start();

        Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
                "cloud_name", "demo", "api_key", "key", "api_secret", "secret",
                "upload_prefix", "http://127.0.0.1:" + server.getAddress().getPort()));
        cloudinary.uploader().uploadLarge(file, ObjectUtils.asMap("resource_type", "video"), 100_000_000);
        server.stop(0);
    }
}

Error screenshots or Stack Trace (if applicable)

Output of the example (first, some middle and the last request):

Content-Range: bytes 0-99999999/2300000000
Content-Range: bytes 100000000-199999999/2300000000
Content-Range: bytes 200000000-299999999/2300000000
...
Content-Range: bytes 2000000000-2099999999/2300000000
Content-Range: bytes 2100000000-2199999999/2300000000
Content-Range: bytes -2094967296--1994967297/2300000000

The last start should be 2200000000, it is -2094967296 (2,200,000,000 - 2^32). Against the real API the upload ends with java.lang.RuntimeException: Invalid Content-Range header.

Build System

[x] Maven
[x] Gradle
[ ] Other (Specify)

OS (Please specify version)

[ ] Windows
[ ] Linux
[x] Mac
[ ] Other (specify)

macOS on Apple Silicon. The bug is plain Java arithmetic and does not depend on the OS.

Versions and Libraries (fill in the version numbers)

Cloudinary Java SDK version - 2.6.0 (also 2.0.0 and master)
JVM (dev environment) - 25 (example above); 1.8 and 11 (unit test in the PR)
JVM (production environment) - N/A (independent of the JVM)
Maven - 3.10.0
Gradle - 6.0.1 (wrapper of this repository)

Repository

Not needed, the example above is self-contained.

Suggested fix

Widen before multiplying:

long currentLoc = offset + (long) bufferSize * partNumber;

The arithmetic around totalBytes, totalBytesUploaded and length already uses long, so this appears to be the only place. As far as we can see, the other uploadLarge overloads delegate to the same method.

I'll open a PR with this fix and an offline unit test in cloudinary-core that checks the Content-Range of every chunk for a 2.3 GB upload.

Activity

  1. jroco-cloudinary commented on Nov 19, 2026

    @jroco-cloudinary

    @adimiz1 can you please have a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions