Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4b2081c008d48a9a4486f3939f2c3b8c169e7f8a
Ticket: 442636ffffffffffffffffffffffffffffffffff
use of `tmpnam' is dangerous
User & Date: lloydlim on 2001-07-19 01:38:24
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    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;
         }
    
    
  4. foundin changed to: "obsolete: 8.3.3"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "lloydlim"
  9. subsystem changed to: "52. Portability Support"
  10. title changed to: "use of `tmpnam' is dangerous"
  11. type changed to: "Bug"