...
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
if ((fdfp = tmpfile()) == NULL) { /* Handle Error */ } |
...