Suppress warning: using `mktemp 'is dangerous

How can I suppress the following warning from the gcc linker:

warning: using "mktemp" is dangerous, it is better to use "mkstemp"

I know it is better to use mkstemp() , but for some reason I have to use the mktemp() function.

+6
c gcc security gcc-warning
source share
5 answers

I think you need a path because you pass it to a library that only accepts path names as an argument, not file descriptors or FILE pointers. If so, you can create a temporary directory with mkdtemp and put your file there, then the actual name does not matter, because the path is already unique due to the directory.

+8
source share

If you use to use mktemp , you can do nothing to stop this warning without deleting the section that mktemp uses from libc.so.6.

Why do you have to use mktemp ?

+4
source share

Two things:

  • mktemp not a standard feature
  • the warning is special, implemented in the linker as a section of .gnu.warning.mktemp

Use your own API if you really need to burn to disk. Or mkstemp() as suggested.

+4
source share

If you are statically linking the runtime, then another option is to write your own version of mktemp in the object file. The component should prefer your version over the run-time version.

Change Thanks to Jason Coco for pointing out the big misunderstanding that I had at mktemp and his relatives. Now this is a little easier to solve. Since the linker will prefer the version in the 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 make everything thread safe. You can use a static array of descriptors and an atexit registered function to clean up, if you can set a limit on the number of temporary files that you need. If not, just use a linked list.

0
source share

Use mkstemp :

 int fd = mkstemp(template); 

After this call, template will be replaced with the actual file name. You will have a file descriptor and file path.

0
source share

All Articles