Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
/* ... */
if (tmpnam(temp_file_name)) {
  /* temp_file_name may refer to an existing file */
  t_file = fopen(temp_file_name,"wb+");
  if (!t_file) {
     /* Handle Error */
  }
}
/* ... */

Wiki Markup
Unfortunately, this solution is still non-compliant because it violates \[[TMPxx-C. Temporary file names must be unique when the file is created]\], \[[TMP32-C. Temporary files must be opened with exclusive access]\], \[[TMP31-C. Temporary files must have an unpredictable name]\], and \[[TMP33-C. Temporary files must be removed before the program exits]\].

Non-Compliant Code Example: tmpnam_s() (ISO/IEC TR 24731-1)

Wiki Markup
The TR 24731-1 {{tmpnam_s()}} function generates a string that is a valid filename and that is not the same as the name of an existing file \[[ISO/IEC TR 24731-2006|AA. C References#SO/IEC TR 24731-2006]\]. The function is potentially capable of generating {{TMP_MAX_S}} different strings, but any or all of them may already be in use by existing files and thus not be suitable return values.  The lengths of these strings must be less than the value of the {{L_tmpnam_s}} macro.

The L_tmpnam_s macro expands to an integer constant expression that is the size needed for an array of char large enough to hold a temporary filename string generated by the tmpnam_s() function. The TMP_MAX_S macro expands to an integer constant expression that is the maximum number of unique filenames that can be generated by the tmpnam_s() function. The value of the macro TMP_MAX_S is only required to be 25 by ISO/IEC TR 24731-1.

Non-normative text in TR 24731-1 also recommends the following:

Implementations should take care in choosing the patterns used for names returned by tmpnam_s. For example, making a thread id part of the names avoids the race condition and possible conflict when multiple programs run simultaneously by the same user generate the same temporary file names.

If implemented, this reduces the space for unique names and increases the predictability of the resulting names.

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 */
}

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

Implementation Details

For Microsoft Visual Studio 2005 the name generated by tmpnam_s consists of a program-generated filename and, after the first call to tmpnam_s(), a file extension of sequential numbers in base 32 (.1-.1vvvvvu, when TMP_MAX_S in stdio.h is INT_MAX) Wiki MarkupThis solution is also non-compliant because it violates \[[TMPxx-C. Temporary file names must be unique when the file is created]\] and \[[TMP33-C. Temporary files must be removed before the program exits]\].

Non-Compliant Code Example: mktemp()/open() (POSIX)

...

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

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

The mktemp() function was marked LEGACY in the Open Group Base Specifications Issue 6.

Non-Compliant Code Example: tmpfile()

The C99 tmpfile() function creates a temporary binary file that is different from any other existing file and that is automatically removed when it is closed or at program termination.

It should be possible to open at least TMP_MAX temporary files during the lifetime of the program (this limit may be shared with tmpfile()). The value of the macro TMP_MAX is only required to be 25 by the C99 standard.

Most historic implementations provide only a limited number of possible temporary filenames (usually 26) before filenames are recycled.

Code Block
bgColor#FFCCCC

/* ... */
FILE *tempfile = tmpfile(void);
if (tempfile == NULL) {
  /* handle error condition */
}
/* ... */

Wiki Markup
The {{tmpfile()}} function may not be compliant with
Wiki Markup
This solution is also non-compliant because it violates \[[TMPxx-C. Temporary file names must be unique when the file is created]\] and \[[TMP33-C. Temporary files must be removed before the program exits]\] for implementations where the temporary file is not removed if the program terminates abnormally.

Include Page
c:FIO39 CS mkstemp
c:FIO39 CS mkstemp

...