...
Code Block | ||||
---|---|---|---|---|
| ||||
char *file_name; FILE *fp; /* Initialize file_name */ fp = fopen(file_name, "r"); if (fp == NULL) { /* Handle open error */ } /* Read data */ if (fseek(fp, 0L, SEEK_SET) != 0) { /* Handle repositioning error */ } /* Continue */ |
Both the noncompliant code example and the compliant solution are taken from FIO07-C. Prefer fseek() to rewind().
Noncompliant Code Example (setbuf()
)
...
Code Block | ||||
---|---|---|---|---|
| ||||
FILE *file; char *buf = NULL; /* Setup file */ if (setvbuf(file, buf, buf ? _IOFBF : _IONBF, BUFSIZ) != 0) { /* Handle error */ } /* ... */ |
...
Risk Assessment
Although it is rare for a violation of this rule to result in a security vulnerability, it can easily result in lost or misinterpreted data.
...