Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Wiki MarkupC99 Section 7.12.1 defines two types of errors that relate specifically to math functions in {{math.h}} \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]:

a domain error occurs if an input argument is outside the domain over which the mathematical function is defined.

...

Range errors usually can not be prevented, as they are dependent on the implementation of floating-point numbers, as well as the function being applied. Instead of preventing range errors, one should attempt to detect them and take alternative action if a range error occurs.

Wiki MarkupThe following table lists standard mathematical functions, along with any checks that should be performed on their domain, and indicates if they also throw range errors, as reported by \ [ISO/IEC 9899:1999\]. If a function has a specific domain over which it is defined, one should check its input values, and if a function throws range errors, one should detect if a range error occurs. The standard math functions not on this table, such as {{atan()}} have no domain restrictions and do not throw range errors.

Function

Domain

Range

acos(x), asin(x)

-1 <= x && x <= 1

no

atan2(y,x)

x != 0 || y != 0

no

acosh(x)

x >= 1

no

atanh(x)

-1 < x && x < 1

no

cosh(x), sinh(x)

none

yes

exp(x), exp2(x), expm1(x)

none

yes

ldexp(x, exp)

none

yes

log(x), log10(x), log2(x)

x > 0

no

log1p(x)

x > -1

no

ilogb(x), logb(x)

x != 0

yes

scalbn(x, n), scalbln(x, n)

none

yes

hypot( x, y)

none

yes

pow(x,y)

x > 0 || (x == 0 && y > 0) ||
(x < 0 && y is an integer)

yes

sqrt(x)

x >= 0

no

erfc(x)

none

yes

lgamma(x), tgamma(x)

x != 0 &&
!(x < 0 && x is an integer)

yes

lrint(x), lround(x)

none

yes

fmod(x,y)

y != 0

no

nextafter(x,y), nexttoward(x,y)

none

yes

fdim(x,y)

none

yes

fma(x,y,z)

none

yes

...

Range errors cannot usually be prevented, so the most reliable way to handle range errors is to detect when they have occurred and act accordingly.

Wiki MarkupThe exact treatment of error conditions from math functions is quite complicated. C99, Section 7.12.1, defines the following behavior for floating point overflow \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]:

A floating result overflows if the magnitude of the mathematical result is finite but so large that the mathematical result cannot be represented without extraordinary roundoff error in an object of the specified type. If a floating result overflows and default rounding is in effect, or if the mathematical result is an exact infinity from finite arguments (for example log(0.0)), then the function returns the value of the macro HUGE_VAL, HUGE_VALF, or HUGE_VALL according to the return type, with the same sign as the correct value of the function; if the integer expression math_errhandling & MATH_ERRNO is nonzero, the integer expression errno acquires the value ERANGE; if the integer expression math_errhandling & MATH_ERREXCEPT is nonzero, the ''divide-by-zero'' floating-point exception is raised if the mathematical result is an exact infinity and the ''overflow'' floating-point exception is raised otherwise.

...

  • These are in general valid (albeit unlikely) data values.
  • Making such tests requires detailed knowledge of the various error returns for each math function.
  • There are three different possibilities, -HUGE_VAL, 0, and HUGE_VAL, and you must know which are possible in each case.
  • Different versions of the library have differed in their error-return behavior.

...

It is also difficult to check for math errors using {{errno}} because an implementation might not set it. For real functions, the programmer can tell whether the implementation sets {{errno}} by checking whether {{math_errhandling & MATH_ERRNO}} is nonzero. For complex functions, the C99, Section 7.3.2, simply states "an implementation may set {{errno}} but is not required to" \[" [ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\].

The System V Interface Definition, Third Edition (SVID3) provides more control over the treatment of errors in the math library. The user can provide a function named matherr that is invoked if errors occur in a math function. This function can print diagnostics, terminate the execution, or specify the desired return-value. The matherr() function has not been adopted by C99, so its use is not generally portable.

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FLP32-C

medium

probable

medium

P8

L2

Automated Detection

Tool

Version

Checker

Description

Section

Fortify SCA

Section

V. 5.0

 

Section

can detect violations of this rule with CERT C Rule Pack

...

MITRE CWE: CWE-682, "Incorrect Calculation"

Bibliography

...

\[[Plum 1985|AA. Bibliography#Plum 85] \] Rule 2-2 \
[[Plum 1989|AA. Bibliography#Plum 91]\] Topic 2.10, "conv - conversions and overflow"

...

FLP31-C. Do not call functions expecting real values with complex values      05. Floating Point (FLP)      FLP33-C. Convert integers to floating point for floating point operations