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.
6 Answers
If you really only want the directory name, use the string constant macro P_tmpdir
, defined in <stdio.h>
.

- 77,584
- 5
- 64
- 78
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:
- GCC bug page regarding this issue - I filed this a short while ago.
- GNU ld bug page regarding this issue - filed in 2010!
- GNU ld bug page suggesting an approach for resolving the issue - I filed this a short while ago.
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.

- 118,144
- 57
- 340
- 684
"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.

- 2,476
- 18
- 32
-
I need to create a FIFO file in a temporary directory so I use dirname(tempnam...) – jackhab Feb 12 '09 at 13:59
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.
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 ¯\_(ツ)_/¯

- 535
- 2
- 14
- 31
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.

- 88,763
- 26
- 134
- 176
-
4
-
1
-
8It is a warning produced at the linking, not compiling stage, so this warning has no effect. (Giving it to the link stage also does not work). (In g++ 4.8.2, at least) – Darren Cook May 06 '15 at 21:25