Skip to main content

IAR Embedded Workbench for RL78 5.20

CERT-FIO34-C

In this section:
Synopsis

Distinguish between characters read from a file and EOF or WEOF.

Enabled by default

Yes

Severity/Certainty

High/Medium

highmedium.png
Full description

On an implementation where int and char have the same width, a character-reading function can read and return a valid character that has the same bit-pattern as EOF. Consequently, failing to use feof() and ferror() to detect end-of-file and file errors can result in incorrectly identifying the EOF character on rare implementations where sizeof(int) == sizeof(char).

Coding standards
CERT FIO34-C

Use int to capture the return value of character IO functions

Code examples

The following code example fails the check and will give a warning:

#include <assert.h>
#include <limits.h>
#include <stdio.h>

void func(void) {
    char c;
    static_assert(UCHAR_MAX < UINT_MAX, "FIO34-C violation");

    do {
        c = getchar();
    } while (c != EOF);
}

The following code example passes the check and will not give a warning about this issue:

#include <assert.h>
#include <stdio.h>
#include <limits.h>

void func(void) {
    int c;
    static_assert(UCHAR_MAX < UINT_MAX, "FIO34-C violation");

    do {
        c = getchar();
    } while (c != EOF);
}