Main Content

CERT C: Rec. FLP04-C

R2026b

Check floating-point inputs for exceptional values

Since R2026b

Description

Check floating-point inputs for exceptional values.1

Polyspace Implementation

Polyspace® checks for the issue Input floating-point value not checked for exceptional value

Examples

expand all

Issue

The issue occurs when your code meets both these conditions:

  • Accepts a floating-point input value using one of these functions: atof, strtod, strtof, strtold, or scanf.

  • Uses the floating-point value returned by the preceding functions without first checking for exceptional values such as NAN, INFINITY and -INFINITY.

Polyspace considers a floating-point value to be used if it is part of an arithmetic or Boolean expression, passed to another function, returned by a function, or assigning it to a global variable.

Risk

Using exceptional values of floating-point numbers can result in unexpected behavior. For example:

  • Comparisons with a floating point variable x, such as (x == x) or (x > 0.0), always evaluate to false when x is NAN, which can result in logic errors.

  • The sum x + y returns INFINITY if one these variables has the value INFINITY. This can cause overflow and break range checks.

Fix

To fix this violation:

  • Avoid storing the floating-point return value of atof, strtod, strtof, strtold, or scanf in a nonlocal variable.

  • Check for NAN and INFINITY before using the returned floating-point value. Polyspace reports no violations if you use one of these techniques to verify that a value is not an exceptional values:

    • Use both isnan and isinf and check that their return values are false.

    • Use fpclassify and check for a negative comparison to both FP_NAN and FP_INFINITE.

    • Use either isfinite or isnormal and check that their return values are true

Example

In this example, the conversion result from atof is used directly in a comparison without checking for NAN or INFINITY. Polyspace reports a violation.

#include <stdlib.h>
#include <stdio.h>

void bad_direct_use(const char *s)
{
    if (atof(s) > 10.0)   // Noncompliant
    {
        printf("large\n");
    }
}

Correction

Store the conversion result in a local variable and validate it with isfinite before any use. If the value is non-finite, handle the error instead of continuing.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void good_direct_use(const char *s)
{
    double x = atof(s);

    if (!isfinite(x))     // Compliant
    {
        /* handle error: NaN or Infinity */
        return;
    }

    if (x > 10.0)
    {
        printf("large\n");
    }
}

Check Information

Group: Rec. 05. Floating Point (FLP)
PQL Name: std.cert.FLP04_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.