Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added a second compliant implementation for Windows. It is not portable code, but it does avoid the race condition.

...

This code contains unavoidable race conditions between the calls to _access_s(), remove(), and rename() and can consequently be safely executed only within a secure directory. (See FIO15-C. Ensure that file operations are performed in a secure directory.)  Another option would be to use the MoveFileEx API, and pass in the MOVEFILE_REPLACE_EXISTING flag.

Code Block
bgColor#ccccff
langc
const char *src_file = /* ... */;
const char *dest_file = /* ... */;

if (!MoveFileEx(src_file, dest_file, MOVEFILE_REPLACE_EXISTING)) {
  /* Handle error condition */
}

While this is not portable code, it does avoid the race condition when using _access_s(), remove(), and rename().

Compliant Solution (POSIX)

...