How to *disable* GCC tmpnam() warnings

Programming, for all ages and all languages.
Post Reply
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

How to *disable* GCC tmpnam() warnings

Post 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?
Every good solution is obvious once you've found it.
mystran

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

Post by mystran »

Looks like a linker warning to me, actually :p
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

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

Post 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.
mystran

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

Post 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.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

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

Post 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).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

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

Post 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)
Kemp

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

Post 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?
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

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

Post 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 ;)
Kemp

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

Post 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.
mystran

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

Post 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.
Post Reply