Re: why the usage of gets() is dangerous.
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 17 Nov 2007 13:25:29 -0800
CBFalconer wrote:
Malcolm McLean wrote:"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote:
We'll declare that pointer cosist of three values - the address,It is possible, though rather difficult, to implement a safeShow me.
gets(), that is to say one that always terminates the program
with an error message if the buffer is exceeded.
the start of the object, and the end of the object. Now in the
write to array code we specify that if the address execceds the
end of the object, the program is to terminate with an error
meaage.
No good. Pointers do not necessarily contain those components. You have to make it safe within the guarantees provided by the C
standard.
No, he doesn't. You're asking for more than Malcolm claimed.
Malcolm didn't claim that it could be made safe within the gaurantees
provided by the C standard. His claim is a much more modest one,
that it's possible for a (hypothetical) C implementation to provide a
"safe" gets() function, and I believe he's correct.
His solution requires the use of "fat pointers", which are not
widely implemented but are reasonably well understood. In such an
implementation, the char* parameter to gets() provides information
about the size of the buffer to which it points. (Portable C code
cannot make use of this information, but gets() needn't be implemented
in portable C.) If the size of the input line exceeds the size of the
buffer, the behavior is undefined. This means the implementation is
free to do whatever it likes, including terminating the program with
an error message (or discarding the remainder of the line, or leaving
the remainder of the line on the input stream).
I know of no C implementations that actually use fat pointers; even if
there were, the possibility of making gets() safe in one implementation
does no good for code that is to be used with other implementations.
I believe Malcolm's claim as stated is correct. It's not particularly
useful, but he didn't claim that it was; I believe it was merely an
intellectual excercise, not a serious proposal.
--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: why the usage of gets() is dangerous.
- From: Tor Rustad
- Re: why the usage of gets() is dangerous.
- References:
- why the usage of gets() is dangerous.
- From: jayapal
- Re: why the usage of gets() is dangerous.
- From: Paul Hsieh
- Re: why the usage of gets() is dangerous.
- From: Malcolm McLean
- Re: why the usage of gets() is dangerous.
- From: Richard Heathfield
- Re: why the usage of gets() is dangerous.
- From: Malcolm McLean
- Re: why the usage of gets() is dangerous.
- From: CBFalconer
- why the usage of gets() is dangerous.
- Prev by Date: Re: using static char arrays to be on the safe side
- Next by Date: Re: Record density of crud and spam
- Previous by thread: Re: why the usage of gets() is dangerous.
- Next by thread: Re: why the usage of gets() is dangerous.
- Index(es):
Relevant Pages
|