Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed references to /tmp to help with TMP00 contradictions

...

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

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

/* A TOCTOU race condition exists here */

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

...

Code Block
char template[] = "/tmp/temp-XXXXXX";
if ((fd = mkstemp(template)) == -1) {
   /* Handle Error */
}

...

Code Block
bgColor#ccccff
char sfn[] = "/tmp/temp-XXXXXX";
FILE *sfp;
int fd = -1;

if ((fd = mkstemp(sfn)) == -1) {
  /* Handle Error */
}

unlink(sfn); /* unlink immediately to allow name to be recycled */

if ((sfp = fdopen(fd, "w+")) == NULL) {
  close(fd);
  /* Handle Error */
}

/* use temporary file */

fclose(sfp); /* also closes fd */

...