gets() rationale

  • Thread starter Christopher Benson-Manica
  • Start date
M

Mark McIntyre

I'd tell him that I could do it, but only at the expense of
introducing a known and serious bug into the code which provided a
real risk of the programme crashing under perfectly normal conditions.

So, you disagree with Keith Thompson who stated:

'gets() can be used safely *only* if you have absolute
control over what the program will see on stdin.'[/QUOTE]

My statement in no way disagrees with Keith. Except in your mind of
course.

You sir, are a troll. Go back under your rock.
You believe there absolutely does not exist any case where gets() can be
used safely?

I believe there's absolutely no point in arguing with a troll.
 
E

Eric

Arthur J. O'Dwyer said:
Now, I may be naive, but it seems to me that it is in those
situations where absolutely every byte in your program space is
significant that you would want to take the *most* care not to
stomp on any of those bytes.

Agreed. However, I am not speaking about cases where that would be a
worry.
 
E

Eric

Mark McIntyre said:
So, you disagree with Keith Thompson who stated:

'gets() can be used safely *only* if you have absolute
control over what the program will see on stdin.'
?

My statement in no way disagrees with Keith.[/QUOTE]

I would include under the heading of a 'safe use of gets()' that there
would be no risk of crashing under perfectly normal conditions.

When you have absolute control over what the program will see on stdin,
this potential does not exist.

If one cannot guarantee absolute control, gets() should not be used.
 
K

Keith Thompson

Unless, of course, you don't have those extra bytes to spare that an
implementation of fgets() would need which gets does not.

In which case, you would be deemed a hero, by your boss, for getting
something to work which someone who had an irrational and absolute
hatred of gets() could not.

Would you really tell your boss that 'it can't be done' simply because
you would not use gets() and therefore be labeled sloppy and ignorant by
certain members of comp.lang.c?

My hatred of gets() is neither irrational nor absolute.

It's unlikely that a hosted implementation would have such tight
memory constraints. A freestanding implementation need not provide
the standard I/O library at all.

Overall, it's extremely unlikely that the tiny amount of extra memory
(code and/or data) required to use fgets() rather than gets() is going
to be significant. If you run into such a situation in real life,
feel free to come back and tell us about it.
 
E

Eric

Keith Thompson said:
It's unlikely that a hosted implementation would have such tight
memory constraints. A freestanding implementation need not provide
the standard I/O library at all.

Overall, it's extremely unlikely that the tiny amount of extra memory
(code and/or data) required to use fgets() rather than gets() is going
to be significant.

Agreed.

Quite rare or unlikely, but not out of the question and most certainly
would not make someone look look sloppy and ignorant, which had been
asserted.
 
K

Keith Thompson

Agreed.

Quite rare or unlikely, but not out of the question and most certainly
would not make someone look look sloppy and ignorant, which had been
asserted.

Using gets() makes you look sloppy and ignorant. The cases where it's
justified are so rare that they're hardly worth considering.

If I ever saw a legitimate use of gets(), my first reaction would be
that the programmer should have known enough to use fgets() instead.
My second reaction, after spending a few minutes reading the comment
explaining exactly why gets() is more appropriate than fgets() in this
context, would be astonishment. (Lacking such a comment, I wouldn't
get past my first reaction).

Historical note: The Morris Worm hit in 1988, while ANSI was working
on the first C standard. I wonder why that wasn't enough to persuade
the committee to deprecate gets().
 
M

Mark McIntyre

Overall, it's extremely unlikely that the tiny amount of extra memory
(code and/or data) required to use fgets() rather than gets() is going
to be significant.

Agreed.

Quite rare or unlikely, but not out of the question [/QUOTE]


Agreed
and most certainly
would not make someone look look sloppy and ignorant, which had been
asserted.

I disagree as vigorously as you troll. /Any/ use of gets is poor
program design, memory constraints notwithstanding. I'd expect any
programmer using it to be able to explain precisely why he was utterly
convinced that there was no chance ever of it causing a problem.

And I do mean /ever/, not merely "in the program's current
incarnation" or "on the current hardware" or "in the current operating
environment".

His code that today reads from 80-char punched cards will tomorrow
read from 132-char cards, or from a pipe, or run on a machine with no
memory protection, or be used as a library function in a heart monitor
or nuclear missile silo.
 
E

Eric

Keith Thompson said:
Using gets() makes you look sloppy and ignorant. The cases where it's
justified are so rare that they're hardly worth considering.

If I ever saw a legitimate use of gets(), my first reaction would be
that the programmer should have known enough to use fgets() instead.
My second reaction, after spending a few minutes reading the comment
explaining exactly why gets() is more appropriate than fgets() in this
context, would be astonishment. (Lacking such a comment, I wouldn't
get past my first reaction).

So, you would really tell your boss that 'it can't be done' simply
because you would not use gets() and therefore be labeled sloppy and
ignorant by certain members of comp.lang.c.
 
K

Keith Thompson

So, you would really tell your boss that 'it can't be done' simply
because you would not use gets() and therefore be labeled sloppy and
ignorant by certain members of comp.lang.c.

I didn't say that. I didn't say anything resembling that. You might
consider re-reading what I actually wrote.

I would use gets() if it were appropriate. I have never seen a
situation in which it's appropriate, and I am certain, beyond
reasonable doubt, that I never will. If memory constraints were so
severe that a program using gets() would fit and one using fgets()
wouldn't, there are numerous other things I would try before I would
consider using gets(). (I'm not going to bother enumerating them.)

As I've said before, if you can present a real-world situation in
which using gets() is appropriate, I'll be glad to discuss it with
you. (An artificially contrived scenario, invented for this
discussion, doesn't count.)

Until then, I'm done with this. If I don't respond to your postings
on this thread in the future, it doesn't mean I agree with you; it
just means I'm bored.
 
E

Eric

Keith Thompson said:
I would use gets() if it were appropriate.

Then, under what, if any, conditions would you use gets() and not be
required to lable yourself sloppy and ignorant?
 
M

Mark McIntyre

However, it also has that effect. The two aren't exclusive. :)

Nonsense.

There are perfectly legitimate uses for gets that would do nothing of
the sort.[/QUOTE]

Troll alert.
 
M

Mark McIntyre

No, I would write something like the following, and cackle: snippage

and encourage the users to declare char arrays of size MAXLGH. I
could also be persuaded to return some status information rather
than the void.

'The check of the buffer length put you over pre-defined limits. We're
going with another person who understands our situation. You're fired.'[/QUOTE]

Troll alert.
 
M

Mark McIntyre

On Fri, 12 Dec 2003 18:54:25 -0500, in comp.lang.c ,
Why are you going to insist on putting in an absolutely unnecessary
buffer check in the subset of cases I am talking about, making the
problem unsolvable?

troll alert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top