retiring gets(3)

Shankar Unni shankar at hpclscu.HP.COM
Tue Nov 15 08:00:05 AEST 1988


> gets() has legitimate uses.  It is in the library Base Document.
> It is widely used in existing code (sometimes safely, sometimes not).
> It stays.

Exactly how do you use gets "safely"? There is really no way to stop gets
from overwriting the end of your buffer, unless you fiddle around with the
internals of stdio. Or do you "know" that your buffer is large enough? (This
might be acceptable for a limited situation, like when you or a trusted
co-program is writing stuff that you're reading from stdin, but in a more
general case, it's impossible). 

The thing about gets is that until now, the hazards of using it have not been
adequately advertised. There is no mention in any book or reference on C
about how gets can be perverted to blow away your application. It does occur
to most C programmers ultimately that there is "something wrong" with gets
when you cannot specify the max length to read in, but the magnitude of the
problem rarely sinks in.

This is why the suggestion of moving gets() to a compatibility library
sounds so good: this gives you the opportunity of making C programmers
re-evaluate their use of gets(), and replace it with fgets() if they are
unsure of the security and integrity implications of using gets().

But then, C programmers are such a spoilt bunch (sigh!). They scream and
moan at the least little trouble they are put to :-(.
----
Shankar.



More information about the Comp.lang.c mailing list