Page 1 of 1

How to *disable* GCC tmpnam() warnings

Posted: Fri Jun 02, 2006 6:28 am
by Solar
How I love this kind of thing... reeks of Microsoft "we know better than you", but it's actually GCC...

test.c:

Code: Select all

#include <stdio.h>

int main()
{
    char name[ L_tmpnam ];
    puts( tmpnam( name ) );
    return 0;
}
On GCC, this yields:

Code: Select all

/tmp/{gibberish}.o: In function 'main':
test.c:(.text+0x23): warning: the use of 'tmpnam' is dangerous, better use 'mkstemp'
Yes I know it's dangerous. Get out of my face!

Does anyone know of a trick to get rid of this warning, without resolving to [tt]2> /dev/null[/tt] or otherwise castrating more... valid... warnings?

Re:How to *disable* GCC tmpnam() warnings

Posted: Fri Jun 02, 2006 7:18 am
by mystran
Looks like a linker warning to me, actually :p

Re:How to *disable* GCC tmpnam() warnings

Posted: Fri Jun 02, 2006 11:12 am
by Candy
Solar wrote: How I love this kind of thing... reeks of Microsoft "we know better than you", but it's actually GCC...

Code: Select all

/tmp/{gibberish}.o: In function 'main':
test.c:(.text+0x23): warning: the use of 'tmpnam' is dangerous, better use 'mkstemp'
Uugggh... what's up with warning for a given name of a function? Jeez.
Does anyone know of a trick to get rid of this warning, without resolving to [tt]2> /dev/null[/tt] or otherwise castrating more... valid... warnings?
Are you very sure it isn't a #warning in the code? If not, please do change to a more sane compiler that doesn't assume "char *tmpnam()" must be evil because you called it tmpnam.

Re:How to *disable* GCC tmpnam() warnings

Posted: Fri Jun 02, 2006 7:52 pm
by mystran
Now that I think of it, there IS an option to turn that on and off. Unfortunately, I couldn't find it mentioned in the funny manual when I quickly tried to check it.

Re:How to *disable* GCC tmpnam() warnings

Posted: Sat Jun 03, 2006 12:32 pm
by nick8325
It seems that the unsafe functions aren't hard-coded: for every unsafe function in libc, there's a section .gnu.warning.name-of-function with the text of a warning to be displayed. For example:

Code: Select all

$ objdump -s -j.gnu.warning.tmpnam /lib/libc.so.6

/lib/libc.so.6:     file format elf32-i386

Contents of section .gnu.warning.tmpnam:
 0000 74686520 75736520 6f662060 746d706e  the use of `tmpn
 0010 616d2720 69732064 616e6765 726f7573  am' is dangerous
 0020 2c206265 74746572 20757365 20606d6b  , better use `mk
 0030 7374656d 702700                      stemp'.
which seems at least a *bit* saner than hardcoding a list of functions into the linker (a non-glibc definition of tmpnam won't give the warning, I hope).

I couldn't see a way to stop the warning, though (apart from stripping the .gnu.warning.tmpnam section out of libc).

Re:How to *disable* GCC tmpnam() warnings

Posted: Tue Jun 20, 2006 10:42 am
by Pype.Clicker
nick8325 wrote: I couldn't see a way to stop the warning, though (apart from stripping the .gnu.warning.tmpnam section out of libc).
objcopy --remove-section .gnu.warning.tmpnam libc.so ??

muhhaaaahahahhaaaahaahaa. you better have *very* good reason to want this (e.g. testing PDClib)

Re:How to *disable* GCC tmpnam() warnings

Posted: Tue Jun 20, 2006 1:03 pm
by Kemp
Should I ask what the logic behind the decision to create this warning was? If a compiler/linker wants to ***** me out on my logic design or anything else that it has to directly deal with then I don't mind, but doing it over the name of a variable/function? ::)

Just in case I'm misunderstanding, is tmpnam a built-in thing?

Re:How to *disable* GCC tmpnam() warnings

Posted: Tue Jun 20, 2006 3:36 pm
by nick8325
Kemp wrote: Just in case I'm misunderstanding, is tmpnam a built-in thing?
The linker doesn't know about tmpnam specially - libc.so contains a section which says to the linker "if you see someone using tmpnam, give a warning". So it's not built in to the linker, but instead the library containing the dangerous function.
Pype.Clicker wrote: muhhaaaahahahhaaaahaahaa
My thoughts exactly ;)

Re:How to *disable* GCC tmpnam() warnings

Posted: Tue Jun 20, 2006 5:15 pm
by Kemp
Actually, I was asking if the function itself was a builtin thing, but you answered that anyway. I thought it was something that Solar had made himself and the [linker|whatever] (or the creator of said [linker|whatever]) had just arbitrarily decided [it,they] didn't like the name.

Prolog syntax if you're wondering, the tail end ("whatever") will contain all the other possibilities which I have no experience with and thus can't name off-hand ;) I think a simple list can be represented comma-seperated, been a year or two since I used it.

Re:How to *disable* GCC tmpnam() warnings

Posted: Wed Jun 21, 2006 2:07 pm
by mystran
About the original [tt]2>/dev/null[/tt] solution:

You can do [tt]2>&1[/tt] and pipe it to a grep which removes those messages you don't care about, if you don't want to remove them for good.