CERT-ERR30-C_e
Synopsis
Only check errno when neccessary.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
For mathematical functions in <math.h>, if the integer expression math_errhandling & MATH_ERRNO is nonzero, on a domain error, errno acquires the value EDOM; on an overflow with default rounding or if the mathematical result is an exact infinity from finite arguments, errno acquires the value ERANGE; and on an underflow, whether errno acquires the value ERANGE is implementation-defined. Functions isnan, isinf, isnormal or isfinite can also be used to establish that the returned value is invalid.
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
#include <math.h>
#include <errno.h>
void example3(double x) {
sinh(x);
if (errno != 0) {
return;
}
}
The following code example passes the check and will not give a warning about this issue:
#include <math.h>
#include <fenv.h>
#include <errno.h>
void example2(double x) {
double result;
{
if (math_errhandling & MATH_ERREXCEPT) {
feclearexcept(FE_ALL_EXCEPT);
}
errno = 0;
result = sinh(x);
if ((math_errhandling & MATH_ERRNO) && errno != 0) {
return;
}
}
}
void example3(double x) {
double result;
{
errno = 0;
result = sinh(x);
if (isnan(result) != 0) {
if (errno == EDOM)
return;
}
}
}