views:

1707

answers:

6

I am using the function gets() in my C code. My code is working fine but I am getting a warning message

(.text+0xe6): warning: the `gets' function is dangerous and should not be used.

I want this warning message not to pop up.Is there any way?

I am wondering that there might be such possibilities by creating a header file for disabling some warnings . Or is there any option during compiling that can serve my purpose? Or may be there is a particular way of using gets() for this warning not to pop up?

Kindly guide me...

+12  A: 

The obvious answer is to learn from what the compiler is trying to tell you - you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns.

char buff[100];
gets( buff);   // unsafe!
fgets( buff, 100, stdin );   // safe
anon
Thanks Neil...fgets works fine. thanks a lot.
Biswajyoti Das
In real life you will probably want to use `sizeof buff` instead of duplicating the buffer size.
Bastien Léonard
In real life you will want to size the buffer via a constant such as BUFFSIZE and also use that in the fgets() call.
anon
+6  A: 

I would heed the warning and replace gets. This is clear enough for me:

BUGS

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

Dirk Eddelbuettel
+4  A: 

Use fgets() instead of gets()

char buffer[BUFSIZ];
/* gets(buffer); */
fgets(buffer,sizeof(buffer), stdin);

The gets() function does not check the length of buffer and can write past the end and alter the stack. This is the "buffer overflow" you hear about.

Devon_C_Miller
+4  A: 

You shouldn't use the gets function at all, the manpage says to use fgets instead.

GCC does not provide the functionality that GCC does to disable warnings using pragmas. You must use the various warning options as flags to the compiler instead.

Mike McQuaid
This warning is given by the linker. I don't know a way to disable it.
AProgrammer
+2  A: 

There really is no good reason to use gets(). Even the C standard says it's obsolescent! Use fgets() instead.

[Edit]

It looks like the warning comes from the linker. Do you get warning when compiling with -c? (Which disables linking.)

Bastien Léonard
+11  A: 

If you really want use it.

Here is answer From: http://www.gamedev.net/community/forums/topic.asp?topic_id=523641

If you use a reasonably recent version of gcc, you can use:

#pragma GCC diagnostic ignored "your option here"

For example, if those headers produce a "floating point comparison is unsafe" error, you would use:

#pragma GCC diagnostic ignored "-Wfloat-equal".

Unluckily, you cannot disable "-Wall" that way (that would be too easy, wouldn't it...), you have to do the individual warning options which -Wall enables by hand (at least, the conflicting ones).

Docs: http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas

EDIT: But it seems not work for gets warning... I tried on my pc.

arsane
+1 Although I agree that gets() must not be used, you are the only one who actually answered OP's question :)
qrdl