CERT-FIO41-C
In this section:
Synopsis
Do not call getc(), putc(), getwc(), or putwc() with a stream argument that has side effects.
Enabled by default
Yes
Severity/Certainty
Low/Low

Full description
Do not invoke getc() or putc() or their wide-character analogues getwc() and putwc() with a stream argument that has side effects. The stream argument passed to these macros may be evaluated more than once if these functions are implemented as unsafe macros.
Coding standards
- CERT FIO41-C
Do not call getc() or putc() with stream arguments that have side effects
Code examples
The following code example fails the check and will give a warning:
#include <stdio.h>
void func(const char *file_name) {
FILE *fptr;
int c = getc(fptr = fopen(file_name, "r"));
if (feof(stdin) || ferror(stdin)) {
/* Handle error */
}
if (fclose(fptr) == EOF) {
/* Handle error */
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdio.h>
void func(const char *file_name) {
int c;
FILE *fptr;
fptr = fopen(file_name, "r");
if (fptr == NULL) {
/* Handle error */
}
c = getc(fptr);
if (c == EOF) {
/* Handle error */
}
if (fclose(fptr) == EOF) {
/* Handle error */
}
}