Misuse of readlink()
Third argument of readlink does not
leave space for null terminator in buffer
Description
This defect occurs
when you pass a buffer size argument to readlink() that
does not leave space for a null terminator in the buffer.
For instance:
ssize_t len = readlink("/usr/bin/perl", buf, sizeof(buf));readlink() does
not leave space to enter a null terminator.Risk
The readlink() function copies the content
of a symbolic link (first argument) to a buffer (second argument).
However, the function does not append a null terminator to the copied
content. After using readlink(), you must explicitly
add a null terminator to the buffer.
If you fill the entire buffer when using readlink,
you do not leave space for this null terminator.
Fix
When using the readlink() function, make
sure that the third argument is one less than the buffer size.
Then, append a null terminator to the buffer. To determine where
to add the null terminator, check the return value of readlink().
If the return value is -1, an error has occurred. Otherwise, the return
value is the number of characters (bytes) copied.
Examples
Result Information
| Group: Security |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: READLINK_MISUSE |
| Impact: Medium |
Version History
Introduced in R2017aSee Also
Find defects (-checkers) | Returned value of a sensitive function not checked | Invalid use of standard library string routine | Array access out of bounds | Pointer access out of bounds | File access between time of check and use (TOCTOU)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)