Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC2012-Rule-22.7_a

In this section:
Synopsis

(Required) The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

The macro EOF is compared with a value not originating from a function capable of returning EOF.

Coding standards
MISRA C:2012 Rule-22.7

(Required) The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF

Code examples

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

#include <stdio.h>
#include <stdint.h>

void f1(void)
{
  char ch;
  ch = (char)getchar();
  /*
   * The following test is non-compliant. It will not be reliable as the
   * return value is cast to a narrower type before checking for EOF. */
  if (EOF != (int32_t) ch) {
  }
}

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

#include <stdio.h>
#include <stdint.h>

void f2(void)
{
  char ch;
  ch = (char)getchar();
  if (!feof(stdin))
  {
  }
}

void f3(void)
{
  int32_t i_ch;
  i_ch = getchar();
  /*
   * The following test is compliant. It will be reliable as the
   * unconverted return value is used when checking for EOF. */
  if (EOF != i_ch)
  {
    char ch;
    ch = (char)i_ch;
  }
}