The C standard identifies specific strings to use for the mode
on calls to fopen()
[[ISO/IEC 9899-1999]]. To be strictly conforming and portable, one of the strings from the following table (adapted from the C standard) must be used:
|
Result |
---|---|
r |
open text file for reading |
w |
truncate to zero length or create text file for writing |
a |
append; open or create text file for writing at end-of-file |
rb |
open binary file for reading |
wb |
truncate to zero length or create binary file for writing |
ab |
append; open or create binary file for writing at end-of-file |
r+ |
open text file for update (reading and writing) |
w+ |
truncate to zero length or create text file for update |
a+ |
append; open or create text file for update, writing at end-of-file |
r+b or rb+ |
open binary file for update (reading and writing) |
w+b or wb+ |
truncate to zero length or create binary file for update |
a+b or ab+ |
append; open or create binary file for update, writing at end-of-file |
If the mode string begins with one of the above sequences, the implementation might choose to ignore the
remaining characters, or it might use them to select different kinds of files.
An implementation may define additional mode strings, but only the modes in the above table are fully portable and C99 [[ISO/IEC 9899-1999]] compliant.
Risk Assessment
Using a mode string that is not recognized by an implementation may cause the call to fopen()
to fail.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
FIO11-A |
low |
probable |
low |
P6 |
L2 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[ISO/IEC 9899-1999]] Section 7.9.15.3, "The fopen
function"
FIO10-A. Take care when using the rename() function 09. Input Output (FIO) FIO12-A. Prefer setvbuf() to setbuf()