Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   gets() rationale (http://www.velocityreviews.com/forums/t316463-gets-rationale.html)

Christopher Benson-Manica 12-02-2003 09:28 PM

gets() rationale
 
gets() is universally acknowledged to be broken and useless; however,
it is still part of the standard library. Why? Is there enough
conforming code out there using gets() to justify retaining it? Are
there plans to deprecate or eliminate it in a future version?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Eric Sosman 12-02-2003 09:56 PM

Re: gets() rationale
 
Christopher Benson-Manica wrote:
>
> gets() is universally acknowledged to be broken and useless; however,
> it is still part of the standard library. Why? Is there enough
> conforming code out there using gets() to justify retaining it? Are
> there plans to deprecate or eliminate it in a future version?


A question for comp.std.c, methinks. Follow-ups set.

At a guess, the Committee did not feel at liberty to
"reform" the traditional C library, even when hindsight
showed some of its features could have been better designed.
<varargs.h> was, I think, just about the only "subtraction,"
and the reason was that its traditional form was pretty
much unimplementable on certain machines of interest.

--
Eric.Sosman@sun.com

Ben Pfaff 12-03-2003 06:15 AM

Re: gets() rationale
 
Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:

> gets() is universally acknowledged to be broken and useless; however,
> it is still part of the standard library. Why? Is there enough
> conforming code out there using gets() to justify retaining it? Are
> there plans to deprecate or eliminate it in a future version?


Here is what the C99 rationale says:

30 7.19.7.7 The gets function

Because gets does not check for buffer overrun, it is
generally unsafe to use when its input is not under the
programmer's control. This has caused some to question
whether it should appear in the Standard at all. The
35 Committee decided that gets was useful and convenient in
those special circumstances when the programmer does have
adequate control over the input, and as longstanding
existing practice, it needed a standard specification. In
general, however, the preferred function is fgets (see
ยง7.19.7.2).

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

James Hu 12-03-2003 06:16 AM

Re: gets() rationale
 
On 2003-12-02, Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:
> gets() is universally acknowledged to be broken and useless; however,
> it is still part of the standard library. Why? Is there enough
> conforming code out there using gets() to justify retaining it? Are
> there plans to deprecate or eliminate it in a future version?


A C compiler could generate a diagnostic when functions that are known
to be unsafe are used and suggest a safer alternative instead. (I seem
to remember seeing such warnings on the BSD version of gcc.)

-- James

Dan Pop 12-03-2003 12:32 PM

Re: gets() rationale
 
In <HYWdnQ7P8JEh4lCi4p2dnA@comcast.com> James Hu <jxh@despammed.com> writes:

>On 2003-12-02, Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:
>> gets() is universally acknowledged to be broken and useless; however,
>> it is still part of the standard library. Why? Is there enough
>> conforming code out there using gets() to justify retaining it? Are
>> there plans to deprecate or eliminate it in a future version?

>
>A C compiler could generate a diagnostic when functions that are known
>to be unsafe are used and suggest a safer alternative instead. (I seem
>to remember seeing such warnings on the BSD version of gcc.)


It's actually the linker that does it:

fangorn:~/tmp 26> gcc -c test.c
fangorn:~/tmp 27> gcc test.o
test.o: In function `main':
test.o(.text+0x7): the `gets' function is dangerous and should not be used.

Generating such warnings is a feature of GNU ld.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de

Dan Pop 12-03-2003 12:38 PM

Re: gets() rationale
 
In <bqj06o$8p9$1@chessie.cirr.com> Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:

>gets() is universally acknowledged to be broken and useless; however,
>it is still part of the standard library. Why? Is there enough
>conforming code out there using gets() to justify retaining it? Are
>there plans to deprecate or eliminate it in a future version?


The committee (lame) excuse is that their job was to standardise
existing practice and gets was existing practice. And, because it is
standardised, now, it continues to be existing practice, so there is no
hope to see it removed from the standard.

The real solution would have been to deprecate it in C89 and remove it in
C99. If some C99 program needed it badly, implementing gets() is a piece
of cake and a C99 program would have been allowed to define it. But they
missed the opportunity to fix this bug in the language and they are also
convinced that they did the right thing...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de

Joona I Palaste 12-09-2003 02:02 PM

Re: gets() rationale
 
Dan Pop <Dan.Pop@cern.ch> scribbled the following:
> In <bqj06o$8p9$1@chessie.cirr.com> Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:
>>gets() is universally acknowledged to be broken and useless; however,
>>it is still part of the standard library. Why? Is there enough
>>conforming code out there using gets() to justify retaining it? Are
>>there plans to deprecate or eliminate it in a future version?


> The committee (lame) excuse is that their job was to standardise
> existing practice and gets was existing practice. And, because it is
> standardised, now, it continues to be existing practice, so there is no
> hope to see it removed from the standard.


This begs the question, why was it existing practice in the first place?
What daemon possessed Dennis Ritchie to ever conceive it?

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen

Eric 12-09-2003 04:36 PM

Re: gets() rationale
 
Joona I Palaste <palaste@cc.helsinki.fi> wrote:

> This begs the question, why was it existing practice in the first place?
> What daemon possessed Dennis Ritchie to ever conceive it?


Perhaps because of space limitations.

Once upon a time every single last byte was important and gets works
just fine and is all one needs in certain cases.



--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===

P.J. Plauger 12-09-2003 05:25 PM

Re: gets() rationale
 
"Eric" <egDfAusenetE5fz@verizon.net> wrote in message
news:1g5p6nt.1jvc9pj1cxy8weN%egDfAusenetE5fz@veriz on.net...

> Joona I Palaste <palaste@cc.helsinki.fi> wrote:
>
> > This begs the question, why was it existing practice in the first place?
> > What daemon possessed Dennis Ritchie to ever conceive it?

>
> Perhaps because of space limitations.
>
> Once upon a time every single last byte was important and gets works
> just fine and is all one needs in certain cases.


I don't know for sure, but I doubt that DMR is the perpetrator of
gets. For a period of time in the mid/late 1970s, all sorts of
people were throwing functions into the C library (and applications
into the set of Unix tools, and even system calls into the Unix
kernel). Not all of these were well thought out.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Les Cargill 12-09-2003 05:45 PM

Re: gets() rationale
 
Joona I Palaste wrote:
>
> Dan Pop <Dan.Pop@cern.ch> scribbled the following:
> > In <bqj06o$8p9$1@chessie.cirr.com> Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:
> >>gets() is universally acknowledged to be broken and useless; however,
> >>it is still part of the standard library. Why? Is there enough
> >>conforming code out there using gets() to justify retaining it? Are
> >>there plans to deprecate or eliminate it in a future version?

>
> > The committee (lame) excuse is that their job was to standardise
> > existing practice and gets was existing practice. And, because it is
> > standardised, now, it continues to be existing practice, so there is no
> > hope to see it removed from the standard.

>
> This begs the question, why was it existing practice in the first place?
> What daemon possessed Dennis Ritchie to ever conceive it?
>


The 'C' libraries evoved, and were not necessarily designed from a
rigorous set of requirements ( beyond what they were designed from).

"Doctor, doctor, it hurts when I do that."
"Well, don't do that."

> --
> /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
> \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
> "My absolute aspect is probably..."
> - Mato Valtonen



--
Les Cargill


All times are GMT. The time now is 02:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.