Main Content

CERT C: Rec. ERR01-C

Use ferror() rather than errno to check for FILE stream errors

Since R2026a

Description

Use ferror() rather than errno to check for FILE stream errors1

Polyspace Implementation

The rule checker checks for Use of errno for checking FILE stream error.

Examples

expand all

Issue

This issue occurs if your code reads errno immediately following a function that manipulates a FILE stream. Functions that manipulate a FILE stream include: fread, fgetc, fgets, fgetpos, fsetpos, fflush, puts, fputs, gets, getc, getchar, putchar, putc, fputc, fputwc, and printf.

Risk

Using errno for error checking can be error-prone and confusing. For example, if you check errno after a chain of calls to FILE stream functions, it is not clear which function sets the errno value. In fact, unless you explicitly set errno to zero beforehand, it is possible that the value of errno is not set by any of the relevant functions. If you call more than one function that modifies the value of errno to indicate an error, the errno value must be checked following each call. Otherwise, the value of errno set by a function is overwritten by subsequent calls.

Fix

Use ferror() to check for errors on a file stream. This function checks a specific FILE stream for errors and returns a nonzero value if the error indicator for the stream is set.

Example

This code manually checks errno after an fgets() function call. Polyspace® reports a violation.

#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;  // Reset errno before the operation

    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        if (errno != 0) {
            fprintf(stderr, "Error opening file: %s\n", strerror(errno));
        }
        return 1;
    }

    char buffer[256];
    errno = 0;  // Reset errno before reading

    if (fgets(buffer, sizeof(buffer), file) == NULL) {
        if (errno != 0) { //Noncompliant
            fprintf(stderr, "Error reading file: %s\n", strerror(errno));
        } else if (feof(file)) {
            printf("End of file reached.\n");
        }
    } else {
        printf("Read line: %s", buffer);
    }

    errno = 0;  // Reset errno before closing

    if (fclose(file) != 0) {
        if (errno != 0) {
            fprintf(stderr, "Error closing file: %s\n", strerror(errno));
        }
    }

    return 0;
}

Correction

To fix this violation, replace errno checking by a call to ferror().

#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    errno = 0;  // Reset errno before the operation

    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        if (errno != 0) {
            fprintf(stderr, "Error opening file: %s\n", strerror(errno));
        }
        return 1;
    }

    char buffer[256];

    if (fgets(buffer, sizeof(buffer), file) == NULL) {
        if (ferror(file)) { //Compliant
            fprintf(stderr, "Error reading file: %s\n", strerror(errno));
        } else if (feof(file)) {
            printf("End of file reached.\n");
        }
    } else {
        printf("Read line: %s", buffer);
    }

    if (fclose(file) != 0) {
        if (errno != 0) {
            fprintf(stderr, "Error closing file: %s\n", strerror(errno));
        }
    }

    return 0;
}

Check Information

Group: Rec. 12. Error Handling (ERR)
PQL Name: std.cert.ERR01_C

Version History

Introduced in R2026a


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.