AutosarC++18_03-A4.7.1

An integer expression shall not lead to data loss

Required inputs: IR, StaticSemanticAnalysis

Rationale

Implicit conversions, casts and arithmetic expressions may lead to data loss, e.g.

overflows, underflows or wrap-around.

Integral expressions need to be performed on proper integral types that ensure that the data loss will not occur or appropriate guards should be used to statically detect or counteract such a data loss.

Example
// $Id: A4-7-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <stdexcept>
std::int8_t Fn1(std::int8_t x, std::int8_t y) noexcept {
  return (x + y); // Non-compliant - may lead to overflow
}
std::int8_t Fn2(std::int8_t x, std::int8_t y) {
  if (x > 100 || y > 100) // Range check
  {
    throw std::logic_error("Preconditions check error");
  }
  return (x + y); // Compliant - ranges of x and y checked before the
                  // arithmetic operation
}
std::int16_t Fn3(std::int8_t x, std::int8_t y) noexcept {
  return (static_cast<std::int16_t>(x) + y); // Compliant - std::int16_t type
                                             // is enough for this arithmetic
                                             // operation
}
std::uint8_t Fn4(std::uint8_t x, std::uint8_t y) noexcept {
  return (x * y); // Non-compliant - may lead to wrap-around
}
std::int8_t Fn5(std::int16_t x) {
  return static_cast<std::int8_t>(x); // Non-compliant - data loss
}
std::int8_t Fn6(std::int16_t x) {
  return x; // Non-compliant - data loss by implicit conversion
}
void F() {
  std::int8_t x1 =
      Fn1(5, 10); // Compliant - overflow will not occur for these values
  std::int8_t x2 = Fn1(250, 250); // Non-compliant - Overflow occurs
  try {
    std::int8_t x3 = Fn2(250, 250); // Compliant - No overflow, range checks
                                    // inside fn2() function
  } catch (std::logic_error &) {
    // Handle an error
  }
  std::int16_t x4 = Fn3(250, 250); // Compliant - No overflow, arithmetic
                                   // operation underlying type is wider than
                                   // std::int8_t
  std::uint8_t x5 = Fn4(50, 10);   // Non-compliant - Wrap-around occurs
  std::int8_t x6 = Fn5(100);       // Compliant - data loss will not occur
  std::int8_t x7 = Fn5(300);       // Non-compliant - Data loss occurs
  std::int8_t x8 = Fn6(300);       // Non-compliant - Data loss occurs

  std::int8_t x9 = 150;
  std::int16_t x10 = static_cast<std::int16_t>(x9 + x9); // Non-compliant
  x10 = x9 + x9;                                         // Non-compliant
  x10 = static_cast<std::int16_t>(x9) + x9;              // Compliant

  std::int8_t x11 = x9 << 5; // Non-compliant

  std::int8_t x12 = 127;
  ++x12; // Non-compliant

  std::uint8_t x13 = 255;
  ++x13; // Non-compliant
}
See Also
  • MISRA C++ 2008 [6]: Rule 5-0-6 An implicit integral or floating-point conversion shall not reduce the size of the underlying type.
  • MISRA C++ 2008 [6]: Rule 5-0-8 An explicit integral or floating-point conversion shall not increase the size of the underlying type of a cvalue expression.
  • HIC++ v4.0 [8]: 4.2.2 Ensure that data loss does not demonstrably occur in an integral expression.
  • JSF December 2005 [7]: AV Rule 212: Underflow or overflow functioning shall not be depended on in any special way.
  • C++ Core Guidelines [10]: ES.46: Avoid lossy (narrowing, truncating) arithmetic conversions.

Possible Messages

Key

Text

Severity

Disabled

cast_truncate

Cast may truncate value

None

False

certain_shift_amount_negative

Shift by a negative bit count (undefined behavior)

None

False

certain_shift_amount_too_large

Shift by the integer width or more (undefined behavior)

None

False

certain_shift_right_negative

Right shift with negative left-hand-side

None

False

shift_amount_negative

Possible shift by a negative bit count (undefined behavior)

None

False

shift_amount_too_large

Possible shift by the integer width or more (undefined behavior)

None

False

shift_right_negative

Possible right shift with negative left-hand-side

None

False

signed_cast_overflow_negative

Cast on result of arithmetic computation may cause negative overflow

None

False

signed_cast_overflow_positive

Cast on result of arithmetic computation may cause positive overflow

None

False

signed_overflow_negative

Arithmetic computation may cause negative overflow

None

False

signed_overflow_positive

Arithmetic computation may cause positive overflow

None

False

static_signed_cast_overflow_negative

Cast on result of arithmetic computation may cause negative overflow

None

False

static_signed_cast_overflow_positive

Cast on result of arithmetic computation may cause positive overflow

None

False

static_signed_overflow_negative

Arithmetic computation may cause negative overflow

None

False

static_signed_overflow_positive

Arithmetic computation may cause positive overflow

None

False

static_unsigned_cast_wraparound_negative

Cast on result of arithmetic computation may cause negative wrap-around

None

False

static_unsigned_cast_wraparound_negative_minus_1

Casting -1 to an unsigned type causes negative wrap-around

None

False

static_unsigned_cast_wraparound_positive

Cast on result of arithmetic computation may cause positive wrap-around

None

False

static_unsigned_wraparound_negative

Arithmetic computation may cause negative wrap-around

None

False

static_unsigned_wraparound_positive

Arithmetic computation may cause positive wrap-around

None

False

unsigned_cast_wraparound_negative

Cast on result of arithmetic computation may cause negative wrap-around

None

False

unsigned_cast_wraparound_positive

Cast on result of arithmetic computation may cause positive wrap-around

None

False

unsigned_wraparound_negative

Arithmetic computation may cause negative wrap-around

None

False

unsigned_wraparound_positive

Arithmetic computation may cause positive wrap-around

None

False

Options

abstract_interpretation_maximal_tracked_array_index

abstract_interpretation_maximal_tracked_array_index : int = 10

The number of explicit indices in array expressions per routine tracked by the "symbolic expression analysis". For example, consider the following program.

extern signed char a[6];
int main()
{
    if (a[2] < 0)
    {
        a[2]++;
    }
    if (a[3] < 0)
    {
        a[3]++;
    }
    if (a[4] < 0)
    {
        a[4]++;
    }
    return 0;
}

If the value of this option is set to 2, the first two array index expressions encountered in the routine are tracked. Hence, the analysis can use the facts a[2] < 0 and a[3] < 0 to infer that a[2]++ and a[3]++ do not overflow, but it will not track the third array access in this routine.

A higher value of the option can cause more consumption of memory and time for the analysis.

 

abstract_interpretation_overflow

abstract_interpretation_overflow : bool = False

Use abstract-interpretation-based "symbolic expression analysis" as additional postprocessing step.
 

abstract_interpretation_overflow_unrolling_level

abstract_interpretation_overflow_unrolling_level : int = 0

How many levels of conditions are traversed to compute additional constraints for the "symbolic expression analysis".
 

check_signed

check_signed : bool = True

Whether issues for signed integer operations should be reported. For casts including implicit conversions, the target type of the cast is used.
 

check_unsigned

check_unsigned : bool = True

Whether wrap-around for unsigned integer operations should be reported. For casts including implicit conversions, the target type of the cast is used.
 

relevant_expressions

relevant_expressions

Type: RelevantExpressions

Default: 'const_expressions_only'

Which (const / constant) expressions should be considered.

Note: this is only relevant for the purely static parts of the analysis. The StaticSemanticAnalysis-based checks for runtime errors will be performed independently.

 

suppress_well_defined_findings

suppress_well_defined_findings

Type: SuppressionMode

Default: 'NONE'

Some overflows have well-defined semantics in all C/C++ standard versions. The typical example is UINT_MAX+1 which is well-defined as 0 via wraparound. This differs from INT_MAX+1 which is either undefined or implementation-defined depending on the considered standard version. Most CPUs will compute INT_MIN but this wraparound is not guaranteed by any C/C++ standard.

Both cases are overflows and are reported by this rule. However, one might want to suppress messages for the well-defined cases. To suppress these activate this option.

Different C and C++ standard versions differ in what is well-defined, implementation-defined, or undefined. Luckily, if we only consider well-defined and do not discern between implementation-defined and undefined, we end up with only two groups: pre-C++20 and since-C++20.

 

Option Types

These types are used by options listed above:

RelevantExpressions

none

No (additional) checks for overflows in const-expressions or compile time constant expressions.

const_expressions_only

Whether the analysis should statically check const-expressions (i.e., const variables and literals) that might have been reduced to a literal during compilation.

const_and_compile_time_constant

Whether the analysis should statically check const-expressions (i.e., const variables and literals) as well as compile-time constant expressions (i.e., preprocessor defines, constexprs or literals) that might have been reduced to a literal during compilation.

SuppressionMode

NONE

Suppress nothing.

PRE_CPP2020

Suppress findings that are well-defined before C++20. These are:

  • Positive and negative wrap-arounds of unsigned integers during addition, subtraction, and multiplication
  • Conversions from unsigned to unsigned integers
  • Wrap-around caused by left-shifting of unsigned integer

CPP2020

Suppress findings that are well-defined since C++20. These are:

  • Positive and negative wrap-arounds of unsigned integers during addition, subtraction, and multiplication
  • Conversions between signed and unsigned integers
  • Wrap-around caused by left-shifting
  • Shifting negative integers

Surprising mechanics of C++20 signed narrow integers

Since C++20, casts between signed and unsigned are defined as two-complement wrap-around. Overflows of signed integers are still undefined behavior and are reported by this rule. But, due to integer promotion rules, certain expressions are computed using wider integer types, which can lead to the false impression that this is no longer the case, because no overflow findings are reported there.

Suppose, that the code is compiled on a platform where short is smaller than or equal to half the size of an int. Very commonly the sizes are 2 and 4. This assumption is thus true for many platforms.

In this case, narrow signed integer types such as short or signed char are first implicitly promoted to int before the arithmetic operation is executed. Because of this promotion, the actual operation does not overflow and is thus well-defined. After the operation, an implicit cast is performed to the narrower type. This cast is well-defined in C++20 as wrapping around.

Consider the following snippet:

    static_assert(sizeof(short) == 2);
    static_assert(sizeof(int) == 4);
    short a = 0x1000;
    short b = 0x1001;
    short c = a*b;
C++20 defines c as 0x1000. The reason is that a*b is implicitly promoted to static_cast<int> (a)*static_cast<int>(b). After the promotion, the multiplication does not overflow and yields a well-defined 0x1001000. This number is then implicitly cast to 0x1000 which is also a well-defined operation.

An analogous effect can be observed for signed short addition and multiplication. Another effect is that it is well-defined to shift by up to as many bits as int has even if the shifted integer has fewer bits.

DERIVE_FROM_IR

Derive the language version from the IR compilation flags and suppress findings accordingly.