| Wiki Markup |
|---|
If the C99 {{fgets()}} function fails, the contents of the array it was writing to are [indeterminate| BB. Definitions#indeterminate value] \[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]. (See also [undefined behavior 161| CC. Undefined Behavior#ub_161] of Annex J.) Consequently, it is necessary to reset the string to a known value to avoid possible errors on subsequent string manipulation functions. |
The fgetws() function is similarly affectedIf either of the C Standard fgets() or fgetws() functions fail, the contents of the array being written is indeterminate. (See undefined behavior 175.) It is necessary to reset the string to a known value to avoid errors on subsequent string manipulation functions.
Noncompliant Code Example
In this noncompliant code example, an error flag is set upon if fgets() failure. fails. However, buf is not reset and has indeterminate contents:
| Code Block | ||||
|---|---|---|---|---|
| ||||
char buf[BUFSIZ]; #include <stdio.h> enum { BUFFER_SIZE = 1024 }; void func(FILE *file;) { /* Initialize file */char buf[BUFFER_SIZE]; if (fgets(buf, sizeof(buf), file) == NULL) { /* setSet error flag and continue */ } |
However, buf is not reset and has unknown contents.
} |
Compliant Solution
In this compliant solution, buf is set to an empty string if fgets() fails. The equivalent solution for fgetws() would set buf to an empty wide string.
| Code Block | ||||
|---|---|---|---|---|
| ||||
char buf[BUFSIZ]; #include <stdio.h> enum { BUFFER_SIZE = 1024 }; void func(FILE *file;) { /* Initialize file */char buf[BUFFER_SIZE]; if (fgets(buf, sizeof(buf), file) == NULL) { /* setSet error flag and continue */ *buf = '\0'; } } |
Exceptions
FIO40-C-EX1: If the string goes out of scope immediately following the call to fgets() or fgetws(), or is not referenced in the case of a failure, it need not be reset.
Risk Assessment
Making invalid assumptions about the contents of an array modified by fgets() or fgetws() can result in undefined behavior and 175 and abnormal program termination.
Rule | Severity | Likelihood | Detectable |
|---|
Repairable | Priority | Level |
|---|---|---|
FIO40-C |
low
probable
medium
P4
L3
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Low | Probable | Yes | Yes | P6 | L2 |
Automated Detection
...
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| CodeSonar |
| LANG.MEM.UVAR | Uninitialized Variable | ||||||
| Cppcheck Premium |
| premium-cert-fio40-c | |||||||
| Helix QAC |
| DF4861, DF4862, DF4863 | |||||||
| LDRA tool suite |
| 44 S | Enhanced enforcement | ||||||
| Parasoft C/C++test |
| CERT_C-FIO40-a |
Reset strings on fgets() |
...
ISO/IEC 9899:1999 Section 7.19.7.2, "The fgets function" and Section 7.24.3.2, "the fgetws function"
Bibliography
or fgetws() failure | |||||||||
| CERT C: Rule FIO40-C | Checks for use of indeterminate string (rule partially covered) | |||||||
| PVS-Studio |
| V1024 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
09. Input Output (FIO) FIO41-C. Do not call getc() or putc() with stream arguments that have side effects