Skip to content
Tcl Source Code View Ticket
Login

View Ticket

use of `tmpnam' is dangerous

Closed / Fixed442636ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-07-19 01:38:24
The following warning is produced when compiling Tcl on later versions of Linux and FreeBSD:

/tmp/video4_Linux_2.2.19-7.0.1/mash-code/tcl8.3/unix/libtcl8.3.a(tclUnixPipe.o): In 
function `TclpCreateTempFile':
tclUnixPipe.o(.text+0xef): the use of `tmpnam' is dangerous, better use `mkstemp'

Comments in tclUnixPipe.c say "Linux says we should use mkstemp, but Solaris prefers tmpnam.".  
Please note that Solaris does _not_ prefer tmpnam, it prefers tmpfile (which is more similar to 
mkstemp than to tmpnam).  This preference is just because tmpfile cleans up after itself.  There is 
nothing wrong with mkstemp on Solaris.  On the other hand, the use of tmpnam on Linux and 
FreeBSD is a security issue and generates a compile time warning.  Therefore, I recommend that 
Tcl switch to using mkstemp.

Here is a patch to replace tmpnam with mkstemp:

--- tclUnixPipe.c.old   Wed Jul 18 16:12:31 2001
+++ tclUnixPipe.c       Wed Jul 18 17:50:34 2001
@@ -186,19 +186,21 @@
 TclpCreateTempFile(contents)
     CONST char *contents;      /* String to write into temp file, or NULL. */
 {
-    char fileName[L_tmpnam], *native;
+    char fileName[L_tmpnam + 9], *native;
     Tcl_DString dstring;
     int fd;

     /*
-     * Linux says we should use mkstemp, but Solaris prefers tmpnam.
-     * We should also check against making more then TMP_MAX of these.
+     * We should check against making more than TMP_MAX of these.
      */

-    if (tmpnam(fileName) == NULL) {                    /* INTL: Native. */
-       return NULL;
+    strcpy(fileName, P_tmpdir);
+    if (fileName[strlen(fileName) - 1] != '/') {
+        strcat(fileName, "/");
     }
-    fd = open(fileName, O_RDWR|O_CREAT|O_EXCL, 0666);  /* INTL: Native. */
+    strcat(fileName, "tclXXXXXX");
+    fd = mkstemp(fileName);                             /* INTL: Native. */
     if (fd == -1) {
        return NULL;
     }
hobbs Ticket [442636ffff]: 1 change [History diff]2001-07-20 05:26:59
hobbsplain2001-08-07 06:14:14
Logged In: YES 
user_id=72656

fixed in 8.4a3 cvs.
Log in (anonymous is fine) to add a comment.