Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V6131. Casting to a type with a...
menu mobile close menu
Additional information
toggle menu Contents

V6131. Casting to a type with a smaller range will result in an overflow.

May 26 2025

The analyzer has detected that an integer value is cast to a data type with a smaller size, which leads to an overflow. This happens because a source value is outside the valid range of the target type.

The example of such an overflow:

static final long MAX_CHUNK_SIZE = 4L * 1024 * 1024 * 1024;

void makeChunks(int fileSize) {
    int chunkSize = Math.min(fileSize, (int) MAX_CHUNK_SIZE);

    ....
}

The 4L * 1024 * 1024 * 1024 expression equals 4,294,967,296 (2^32), which is then cast to int. This value exceeds the valid int range ([-2^31..2^31-1]). As a result, the value becomes 0 instead of the expected maximum.

To avoid the overflow, use the long type for all variables and parameters involved in the calculations:

static final long MAX_CHUNK_SIZE = 4L * 1024 * 1024 * 1024;

void makeChunks(long fileSize) {
    long chunkSize = Math.min(fileSize, MAX_CHUNK_SIZE);

    ....
}

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
close form
Free PVS‑Studio license for Microsoft MVP specialists
close form
To get the licence for your open-source project, please fill out this form
close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam