Access mode argument of function in fopen or open group
is invalid
This defect occurs
when you use functions in the fopen or open group
with invalid or incompatible file access modes, file creation flags,
or file status flags as arguments. For instance, for the open function,
examples of valid:
Access modes include O_RDONLY, O_WRONLY,
and O_RDWR
File creation flags include O_CREAT, O_EXCL, O_NOCTTY,
and O_TRUNC.
File status flags include O_APPEND, O_ASYNC, O_CLOEXEC, O_DIRECT, O_DIRECTORY, O_LARGEFILE, O_NOATIME, O_NOFOLLOW, O_NONBLOCK, O_NDELAY, O_SHLOCK, O_EXLOCK, O_FSYNC, O_SYNC and
so on.
The defect can occur in the following situations.
| Situation | Risk | Fix |
|---|---|---|
You pass an empty or invalid access mode to the According
to the ANSI® C standard, the valid access modes for
|
Some implementations allow extension of the access mode such as:
However, your access mode string must begin with one of the valid sequences. | Pass a valid access mode to fopen. |
You pass the status flag O_APPEND to the open function
without combining it with either O_WRONLY or O_RDWR. |
The | Pass either O_APPEND|O_WRONLY or O_APPEND|O_RDWR as
access mode. |
You pass the status flags O_APPEND and O_TRUNC together
to the open function. |
The | Depending on what you intend to do, pass one of the two modes. |
You pass the status flag O_ASYNC to the open function. | On certain implementations, the mode O_ASYNC does
not enable signal-driven I/O operations. | Use the fcntl(pathname, F_SETFL, O_ASYNC); instead. |
The fix depends on the root cause of the defect. Often the result details show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show the event history, you can trace back using right-click options in the source code and see previous related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Polyspace Results Through Bug Fixes or Justifications.
| Group: Programming |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: BAD_FILE_ACCESS_MODE_STATUS |
| Impact: Medium |
| CWE ID: 628, 686 |