Suppress warning: the use of `mktemp' is dangerous

16,410

Solution 1

I guess you need the path because you pass it to a library that only accepts path names as argument and not file descriptors or FILE pointers. If so you can create a temp dir with mkdtemp and place your file there, the actual name is then unimportant because the path is already unique because of the directory.

Solution 2

If you have to use mktemp then there is not anything you can do to suppress that warning short of removing the section that uses mktemp from libc.so.6.

Why do you have to use mktemp?

Solution 3

Two things:

  • mktemp is not a standard function
  • the warning is a special one implemented in the linker as .gnu.warning.mktemp section

Use a native OS API if you really need to write to the disk. Or mkstemp() as suggested.

Solution 4

If you are statically linking the runtime, then the other option is to write your own version of mktemp in an object file. The linker should prefer your version over the runtime version.

Edit: Thanks to Jason Coco for pointing out a major misunderstanding that I had in mktemp and its relatives. This one is a little easier to solve now. Since the linker will prefer a version in an object file, you just need to write mktemp in terms of mkstemp.

The only difficulties are cleaning up the file descriptors that mkstemp will return to you and making everything thread safe. You could use a static array of descriptors and an atexit-registered function for cleanup if you can put a cap on how many temporary files you need. If not, just use a linked list instead.

Share:
16,410
Konstantin
Author by

Konstantin

Updated on June 05, 2022

Comments

  • Konstantin
    Konstantin almost 2 years

    How can I suppress following warning from gcc linker:

    warning: the use of 'mktemp' is dangerous, better use 'mkstemp'

    I do know that it's better to use mkstemp() but for some reason I have to use mktemp() function.

    • Jason Coco
      Jason Coco about 15 years
      Use mkstemp instead of mktemp.
    • Jason Coco
      Jason Coco about 15 years
      What is the reason that you have to use mktemp?
    • Scott Stensland
      Scott Stensland about 6 years
      you see this warning when compiling bash gnu.org/software/bash
    • pevik
      pevik almost 4 years
      Also compiling recent glibc produces it :).
  • Jason Coco
    Jason Coco about 15 years
    You can absolutely STILL use mkstemp: int fd = mkstemp(template); After this call, template will be replaced with the actual file name. You will have the file descriptor and the file's path.
  • Nathaniel Sharp
    Nathaniel Sharp about 15 years
    @Jason Coco you might consider making that an answer, so that it can be upvoted ;-)
  • Jason Coco
    Jason Coco about 15 years
    @D: It is an error to provide a constant string to mkstemp or mktemp because the library call will attempt to alter the string and you will get a bus fault.
  • Robert Siemer
    Robert Siemer over 10 years
    mktemp() is (or was) a standard function. For example in POSIX.1-2001. But don’t use it anyway.