Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top

Webinar: Let's make a programming language. Part 1. Intro - 20.02

>
>
>
V3200. Possible overflow. The...
menu mobile close menu
Additional information
toggle menu Contents

V3200. Possible overflow. The expression will be evaluated before casting. Consider casting one of the operands instead.

05 Jui 2024

The analyzer has detected a suspicious type casting. The result of a binary operation is cast to a type with a large range.

Consider the example:

long Multiply(int a, int b)
{
    return (long)(a * b);
}

Such conversion is redundant. The 'int' type automatically expands to the 'long' type.

A similar casting pattern can be used to avoid overflow, but the pattern is incorrect. The multiplication of the 'int' type variables still leads to overflow. The meaningless multiplication result is explicitly expanded to the 'long' type.

To protect against overflow correctly, cast one of the arguments to the 'long' type. Here's the fixed code:

long Multiply(int a, int b)
{
    return (long)a * b;
}

This diagnostic is classified as: