20

I'm using tempnam() only to get the directory name, so this security warning does not apply to my case. How can I disable it? I couldn't find any switches to do it.

jackhab
  • 17,128
  • 37
  • 99
  • 136

6 Answers6

6

If you really only want the directory name, use the string constant macro P_tmpdir, defined in <stdio.h>.

gnud
  • 77,584
  • 5
  • 64
  • 78
1

The answer is no, because - on many systems - the GNU C library (glibc) which implements this function is compiled so as to trigger a linker warnings when it is used.

See:

Note that the problem is not specific to GCC - any linker is supposed to emit this warning, its trigger is "hard-coded" in the compiled library.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

"The tempnam() function returns a pointer to a string that is a valid filename, and such that a file with this name did not exist when tempnam() checked."

The warning arises because of the race condition between checking and a later creating of the file.

You want to only get the directory name? What should that be good for?

Like stranger already said, you may disable this (and similar warnings) using -Wno-deprecated-declarations.

Bodo Thiesen
  • 2,476
  • 18
  • 32
0

If you want to create a temporary directory that's unique for the process, you can use mkdtemp.

This can, e.g., be useful to create FIFOs in there, or when a program needs to create lots of temporary files or trees of directories and files: Then they can be put into that directory.

0

As linker warning it may be obfuscated by using this answer's ASM workaround/hack: https://stackoverflow.com/a/29205123/2550395

Something like this (quick and dirty):

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>

char my_file[20];

#define __hide_section_warning(section_string)    \
    __asm__ (".section " section_string "\n.string \"\rquidquid agis prudenter agas et respice finem \"\n\t.previous");

/* If you want to hide the linker's output */
#define hide_warning(symbol) \
    __hide_section_warning (".gnu.warning." #symbol)

hide_warning(tmpnam)

tmpnam( my_file );

lock_fd = open( my_file, O_CREAT | O_WRONLY, (S_IRUSR | S_IWUSR | S_IRGRP) );

However, it still will leave a trace in the Make.p file and therefore isn't perfectly clean, besides already being a hack.

PS: It works on my machine ¯\_(ツ)_/¯

Jonathan Root
  • 535
  • 2
  • 14
  • 31
-5

You can use GCC's -Wno-deprecated-declarations option to disable all warnings like this. I suggest you handle the warning properly, though, and take the suggestion of the compiler.

strager
  • 88,763
  • 26
  • 134
  • 176