Disable warning: the `gets' function is dangerous in GCC through header files?

17,254

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.)

Solution 5

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.

Share:
17,254
Biswajyoti Das
Author by

Biswajyoti Das

I am doing my bachelors in Computer science and engineering .

Updated on July 26, 2022

Comments

  • Biswajyoti Das
    Biswajyoti Das almost 2 years

    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?

  • qrdl
    qrdl almost 15 years
    +1 Although I agree that gets() must not be used, you are the only one who actually answered OP's question :)
  • zwol
    zwol about 13 years
    This only works for diagnostics issued by the compiler. The "gets is unsafe" message comes from the linker and AFAIK there is no way to disable it.
  • bgw
    bgw almost 12 years
    Who signs a SO answer with their name, phone number, and a date?
  • chux - Reinstate Monica
    chux - Reinstate Monica over 9 years
    If user input consist only of '\n', this routine returns NULL. Original gets() returned "".
  • Deebster
    Deebster over 2 years
    I think security education is definitely an exception (the only exception?) to everyone's strident objections. Anyway, now that C11 has removed gets from the library, even this doesn't work.