Skip to content

Latest commit

 

History

History
94 lines (75 loc) · 1.95 KB

File metadata and controls

94 lines (75 loc) · 1.95 KB
description Learn more about: Arithmetic overflow: '%operator%' operation causes overflow at compile time. Use a wider type to store the operands
title Warning C26450
ms.date 01/08/2017
f1_keywords
C26450
RESULT_OF_ARITHMETIC_OPERATION_PROVABLY_LOSSY
helpviewer_keywords
C26450

Warning C26450

Arithmetic overflow: 'operator' operation causes overflow at compile time. Use a wider type to store the operands (io.1)

Remarks

This warning indicates that an arithmetic operation was provably lossy at compile time. It can be asserted when the operands are all compile-time constants. Currently, we check left shift, multiplication, addition, and subtraction operations for such overflows.

Warning C4307 is a similar check in the Microsoft C++ compiler.

Code analysis name: RESULT_OF_ARITHMETIC_OPERATION_PROVABLY_LOSSY

Example 1

int multiply()
{
    const int a = INT_MAX;
    const int b = 2;
    int c = a * b; // C26450 reported here
    return c;
}

To correct this warning, use the following code.

long long multiply()
{
    const int a = INT_MAX;
    const int b = 2;
    long long c = (long long)a * b; // OK
    return c;
}

Example 2

int add()
{
    const int a = INT_MAX;
    const int b = 2;
    int c = a + b; // C26450 reported here
    return c;
}

To correct this warning, use the following code:

long long add()
{
    const int a = INT_MAX;
    const int b = 2;
    long long c = (long long)a + b; // OK
    return c;
}

Example 3

int subtract()
{
    const int a = -INT_MAX;
    const int b = 2;
    int c = a - b; // C26450 reported here
    return c;
}

To correct this warning, use the following code.

long long subtract()
{
    const int a = -INT_MAX;
    const int b = 2;
    long long c = (long long)a - b; // OK
    return c;
}

See also

ES.103: Don't overflow