3

Whenever I try to do that, I get this :

warning: the `gets' function is dangerous and should not be used

now , I know why this function is awful. But for the sake of compiling my already-written program, I must use this function. What should I do so this warning won't appear? [or a function with the EXACT same properties. fgets is slightly different so my code won't work]

michael kova
  • 105
  • 2
  • 8
  • 3
    http://stackoverflow.com/questions/1214365/disable-warning-messages-in-gcc-through-header-files – Scooter Dec 11 '14 at 17:19
  • Can you elaborate why you "must" use `gets`? –  Dec 11 '14 at 17:20
  • 1
    I agree with Scooter, but personally, i'd leave the warning in there as a reminder for when you get some more round tuits. Using the gets function is unsafe, forgetting you've used the gets functions is unsafer. – IdeaHat Dec 11 '14 at 17:23
  • I must use gets because the program i wrote depends on char arrays that are strings looking like this : somethingsomething\0 in fgets you have somethingsomething[character for NL]\0 – michael kova Dec 11 '14 at 17:25
  • @michael Here are three questions that explain why `gets` is bad. [one](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used), [two](https://stackoverflow.com/questions/2843073/warninggets-function-is-dangerous) and [three](https://stackoverflow.com/questions/1214365/disable-warning-messages-in-gcc-through-header-files). It's been deprecated for quite a long time now. You should consider rewriting your program, or post code in the question for context. –  Dec 11 '14 at 17:27
  • remyabel - I need a function that gets a string "" to a char array and puts \0 in the end of it. do you know anything like that ?(fgets do no fit) – michael kova Dec 11 '14 at 17:31
  • Your assumption that `gets` returns different content to `fgets` is correct, but it's trivial to overwrite the terminating `\n` with a `NUL` character. – abligh Dec 11 '14 at 17:39
  • 1
    @Scooter The question you refer OP to explains why `gets()` is a bad idea but it does not explain how to get rid of the warning. Not useful. – fuz Dec 11 '14 at 17:41
  • 2
    @FUZxxl The way to get rid of the warning is to **stop using the function**. –  Dec 11 '14 at 17:42
  • @duskwuff False. There are good reasons to keep a program unchanged. – fuz Dec 11 '14 at 17:43
  • 2
    @FUZxxl Then you live with the warning. It doesn't prevent the program from compiling, linking, or running; it just notifies you that the program is unsafe. –  Dec 11 '14 at 17:49
  • @duskwuff It clutters the output and hides other warnings. – fuz Dec 11 '14 at 17:49
  • 1
    Your program has a bug. It uses `gets`, and therefore cannot defend against long input lines. What are your circumstances that make it impossible to fix that bug? Note that `gets` has been removed from the language as of the 2011 ISO C standard; a conforming impelmentation is not obligated to compile and link your program at all. – Keith Thompson Dec 11 '14 at 20:06
  • @FUZxxl: there is **no reason** to not change a program to stop using `gets`. `gets` is so broken that it was removed from ISO C11. It is so broken that it is the only function ever removed from the ISO C standard. – ninjalj Dec 11 '14 at 21:26
  • @ninjalj Not being allowed to change a program is a fairly good reason. – fuz Dec 11 '14 at 21:40
  • @FUZxxl There is an answer that does give a link that allegedly shows how to disable it. Unfortunately, after making my comment I found out that the link is now dead. That answer also talked about using pragmas to disable warning messages. – Scooter Dec 12 '14 at 00:21
  • You can write a replacement function that works the same way but will not read more than **N** characters, for whatever value of **N** happens to be appropriate. (Don't call it `gets`; prior C11 that was a standard library function, and redefining that name causes undefined behavior.) – Keith Thompson Dec 12 '14 at 20:46

3 Answers3

1

You should not suppress the warning. If you truly feel that you cannot live with the warning, write a simple script that parses the output and discards the warning and name it suitably so that you build with:

make ... | I-am-fully-cognizant-of-the-fact-that-this-program-uses-an-unsafe-function-and-the-resulting-executable-is-completely-unsafe

It would be unwise to shorten the name of the filter.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

Moved answer to

https://stackoverflow.com/a/27431134/2410359

Will delete this after a bit.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Have you tried compiling with -w to disable warnings?

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Dear downvoter, please tell me how my answer does not solve OPs problem (i.e. disabling the warning). – fuz Dec 13 '14 at 00:46