...
This does not apply to the character argument in putc(), which is guaranteed to be evaluated exactly once.
...
Noncompliant Code Example (getc)
This code calls the getc() function with an expression as the stream argument. If getc() is implemented as a macro, the file may be opened several times (see FIO31-C. Do not simultaneously open the same file multiple times).
| Code Block | ||
|---|---|---|
| ||
char *file_name;
FILE *fptr;
/* initialize file_name */
int c = getc(fptr = fopen(file_name, "r"));
if (c == EOF) {
/* Handle error */
}
|
This non-compliant noncompliant code example also violates FIO33-C. Detect and handle input output errors resulting in undefined behavior because the value returned by fopen() is not checked for errors.
...
| Code Block | ||
|---|---|---|
| ||
int c;
char *file_name;
FILE *fptr;
/* initialize file_name */
fptr = fopen(file_name, "r");
if (fptr == NULL) {
/* Handle error */
}
c = getc(fptr);
if (c == EOF) {
/* Handle error */
}
|
...
Noncompliant Code Example (putc)
In this non-compliant noncompliant example, putc() is called with an expression as the stream argument. If putc() is implemented as a macro, the expression can be evaluated several times within the macro expansion of putc() with unintended results.
...
The LDRA tool suite V 7.6.0 is able to can detect violations of this rule.
...