AutosarC++17_03-A5.5.1

The right hand operand of the integer division or remainder operators shall not be equal to zero

Required inputs: IR, StaticSemanticAnalysis

Rationale

Usage of a pointer-to-member expression leads to undefined behavior in the following cases:

  • The pointer to member is a null pointer.
  • The dynamic type of the object does not contain the member to which the called pointer to member refers.
Example
// $Id: A5-5-1.cpp 272338 2017-03-28 08:15:01Z piotr.tanski $
#include <cstdint>
#include <stdexcept>
std::int32_t division1(std::int32_t a, std::int32_t b) noexcept {
return (a / b); // Non-compliant - value of b could be zero
}
std::int32_t division2(std::int32_t a, std::int32_t b) {
if (b == 0) {
throw std::runtime_error("Division by zero error");
}
return (a / b); // Compliant - value of b checked before division
}
double fn() {
std::int32_t x = 20 / 0; // Non-compliant - undefined behavior
x = division1(20, 0);    // Undefined behavior
x = division2(20, 0); // Preconditions check will throw a runtime_error from
                // division2() function
std::int32_t remainder = 20 % 0; // Non-compliant - undefined behavior
}
See Also
  • HIC++ v4.0 [8]: 5.5.1 Ensure that the right hand operand of the division or remainder operators is demonstrably non-zero.
  • C++ Core Guidelines [10]: ES.105: Don’t divide by zero.

Possible Messages

Key

Text

Severity

Disabled

division_by_zero

Division by zero

None

False

float_division_by_zero

Floating point division by floating point zero

None

False

modulo_by_zero

Modulo by zero

None

False

possible_division_by_zero

Possible division by zero

None

False

possible_float_division_by_zero

Possible floating point division by zero

None

False

possible_modulo_by_zero

Possible modulo by zero

None

False

Options

abstract_interpretation_div_by_zero

abstract_interpretation_div_by_zero : bool = False

Use additional "symbolic expression analysis" as postprocessing step. This can remove false positives, but might require more time. Option is automatically active if StaticSemanticAnalysis/performance.general.enhanced_analysis is active.
 

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];
extern signed char x;
int main()
{
    if (a[2] < 0)
    {
        x = x / a[2];
    }
    if (a[3] < 0)
    {
        x = x / a[3];
    }
    if (a[4] < 0)
    {
        x = x / 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 the respective divisions do not divide by zero, 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_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".