Main Content

AUTOSAR C++14 Rule A0-4-4

Range, domain and pole errors shall be checked when using math functions

Since R2022a

Description

Rule Definition

Range, domain and pole errors shall be checked when using math functions.

Rationale

Before using a math function, check input arguments for potential domain, range, and pole errors.

This checker searches for domain errors when a function argument falls outside the allowed domain, pole errors when finite arguments result in infinite results, and range errors when results of a function cannot be represented by the return value limitations.

Domain, pole and range errors result in unexpected or undefined behavior.

Polyspace Implementation

Polyspace® raises this defect when you call a math function that results in a domain, pole, or range error.

By default, a Bug Finder analysis does not recognize infinities and NaNs. Operations that results in infinities and NaNs might be flagged as defects. To handle infinities and NaN values in your code, use the option Consider non finite floats (-allow-non-finite-floats).

Extend Checker

Extend this checker to check for defects caused by specific values and invalid use of functions from a custom library. For instance:

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cmath>

double getSqrt(double val)
{
    return sqrt(val);                       //Noncompliant
}

double getRefinedSqrt(double val)
{
    if (val >= 0)
        return sqrt(val);                   //Compliant
    else
        return 0;
}


void main()
{
    double root, refinedRoot;

    root = getSqrt(4);
    root = getSqrt(-1);

    refinedRoot = getRefinedSqrt(4);
    refinedRoot = getRefinedSqrt(-1);
}

Because the math function sqrt within the function getSqrt results in a domain error, Polyspace flags it as noncompliant. Performing a check on the variable val, as seen with the function getRefinedSqrt, helps ensure the value passed to the math function sqrt is compliant and expected.

Check Information

Group: Language independent issues
Category: Required, Partially automated

Version History

Introduced in R2022a