Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If implemented, this reduces the space for unique names and increases the predictability of the resulting names.  But in general, TR 24731-1 does not establish any criteria for predictability of names.

Code Block
bgColor#FFCCCC
FILE *file_ptr;
char filename[L_tmpnam_s];

if (tmpnam_s(filename, L_tmpnam_s) != 0) {
  /* Handle Error */
}

/* A TOCTOU race condition exists here */

if (!fopen_s(&file_ptrfp, filename, "wb+")) {
  /* Handle Error */
}

...

The POSIX function mktemp() takes a given filename template and overwrites a portion of it to create a filename. The template may be any filename with some number of Xs appended to it (for example, /tmp/temp.XXXXXX). The trailing Xs are replaced with the current process number and/or a unique letter combination. The number of unique filenames mktemp() can return depends on the number of Xs provided.

Code Block
bgColor#FFcccc
int fd;
char temp_name[] = "/tmp/temp-XXXXXX";

if (mktemp(temp_name) == NULL) {
  /* Handle Error */
}


/* A TOCTOU race condition exists here */

if ((fd = open(temp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0600)) == -1) {
  /* Handle Error */
}

...

Code Block
bgColor#FFCCCC
if ((fdfp = tmpfile()) == NULL) {
  /* Handle Error */
}

...