tags:

views:

975

answers:

4

When I try to compile C code that uses the gets function, I get a warning: warning: the gets function is dangerous and should not be used.

I remember this has to do something with stack protection and security, but I'm not sure. Can someone help me with removing this warning and explain why is there such warning? If gets is "dangerous" then why can't we remove it?

+13  A: 

In order to use gets safely, you have to know exactly how many characters that you will be reading so you can make your buffer large enough. You will only know that if you know exactly what data you will be reading.

Instead of using gets, you want to use fgets, which has the signature char* fgets(char *string, int length, FILE * stream).

It was never removed from the C standard for backwards compatibility.

Thomas Owens
I would strengthen the end of that first paragraph: in the real world, you almost never know exactly what the data will look like, and if you ever use `gets` on data that an untrusted user that might possibly be able to manipulate, you have a security hole.
Michael E
I believe `gets` was the cause of a hole in an early Unix mail or newsgroup or similar application for that exact reason.
Thomas Owens
like history teaches, giving similar answers doesn't imply anything if people is more notorious than you, how sad :(
Jack
@Jack: his answer is also somewhat clearer than yours. High rep implies having a lot of practice at answering questions....
jprete
That's undoubtely true, but it can be frustrating sometimes..
Jack
@Thomas Owens: you'd be thinking of the 'Robert Morris Worm' from 1988 - http://en.wikipedia.org/wiki/Morris_worm.
Jonathan Leffler
The word is that `gets()` will be made obsolete by upcoming C standard, which is due next year.
qrdl
+3  A: 

You can't remove API functions without breaking the API. If you would, many applications would no longer compile or run at all.

This is the reason that one reference gives:

Reading a line that overflows the array pointed to by s results in undefined behavior. The use of fgets() is recommended.

Gerd Klima
+2  A: 

Because gets doesn't do any kind of check while getting bytes from stdin and putting them somewhere. A simple example:

char array1[] = "12345";
char array2[] = "67890";

gets(array1);

Now, first of all you are allowed to input how many characters you want, gets won't care about it. Secondly the bytes over the size of the array in which you put them (in this case array1) will overwrite whatever they find in memory because gets will write them. In the previous example this means that if you input "abcdefghijklmnopqrts" maybe, unpredictably, it will overwrite also array2 or whatever.

The function is unsafe because it assumes consistent input. NEVER USE IT!

Jack
A: 

I read recently, in a USENET post to comp.lang.c, that gets() is getting removed from the Standard. WOOHOO

You'll be happy to know that the committee just voted (unanimously, as it turns out) to remove gets() from the draft as well.

pmg
It is excellent that it is being removed from the standard. However, most implementations will provide it as a 'now non-standard extension' for at least the next 20 years, because of backwards compatibility.
Jonathan Leffler
Yes, right, but when you compile with `gcc -std=c2012 -pedantic ...` gets() will not get through. (I just made up the `-std` parameter)
pmg