...
See FIO35-C. Use feof() and ferror() to detect end-of-file and file errors when sizeof(int) == sizeof(char) for the case where feof()
and ferror()
must be used instead.
Noncompliant Code Example (scanf()
)
In this noncompliant example, the call to fscanf()
can result in a write outside the character array buf
.
Code Block | ||||
---|---|---|---|---|
| ||||
#define BUF_LENGTH 1024
void get_data(void) {
char buf[BUF_LENGTH];
fscanf(stdin, "%s", buf); */
/* rest of function
}
|
Compliant Solution (scanf()
)
In this compliant solution, the call to fscanf()
is constrained not to overflow buf
.
Code Block | ||||
---|---|---|---|---|
| ||||
#define BUF_LENGTH 1024
void get_data(void) {
char buf[BUF_LENGTH];
fscanf(stdin, "%1024s", buf);
/* rest of function */
}
|
Noncompliant Code Example (argv
)
Arguments read from the command line are stored in process memory. The function main()
, called at program startup, is typically declared as follows when the program accepts command-line arguments:
...