...
Compliant Solution (POSIX)
If the programmer's intent is to not remove an existing destination file, the POSIX {{Wiki Markup access()}} function can be used to check for the existence of a file \ [[Open Group 2004|AA. Bibliography#Open Group 04]\]. This compliant solution renames the source file only if the destination file does not exist.
| Code Block | ||||
|---|---|---|---|---|
| ||||
const char *src_file = /* ... */;
const char *dest_file = /* ... */;
if (access(dest_file, F_OK) != 0) {
if (rename(src_file, dest_file) != 0) {
/* Handle error condition */
}
}
else {
/* Handle file-exists condition */
}
|
...
Compliant Solution (Windows)
On Windows, the [{{Wiki Markup rename()}}|http://msdn.microsoft.com/en-us/library/zw5t957f(VS.80).aspx] function fails if \[ [MSDN|AA. Bibliography#MSDN]\]
File or directory specified by
newnamealready exists or could not be created (invalid path).
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
FIO10-C | medium | probable | medium | P8 | L2 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
ISO/IEC 9899:1999 Section 7.9.4.2, "The rename function"
Bibliography
\[[MSDN|AA. Bibliography#MSDN] \] [{{Wiki Markup rename()}}|http://msdn.microsoft.com/en-us/library/zw5t957f(VS.80).aspx]
\[[Open Group 2004|AA. Bibliography#Open Group 04]\] [{{access()}}|http://www.opengroup.org/onlinepubs/009695399/functions/access.html]
[Open Group 2004] access()
...