Re: why the usage of gets() is dangerous.




"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message
It is possible,
though rather difficult, to implement a safe gets(), that is to say one
that always terminates the program with an error message if the buffer is
exceeded.

Show me.

We'll declare that pointer cosist of three values - the address, 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.

With this device we have a perfectly safe gets() fucntion. It cannot return an incorrect string, or corrupt another variable, or put little elves on screen. It can only fill the buffer correctly or report that it has been exceeded.
>
What is not possible to is implement is a safe fgets(), that is to say,
one that can be used safely given the limitations of the average human
programmer.

The fgets function is very easy to use safely.

Time after time it has been shown that this is not the case. Very often people treat incomplete reads as full lines. So if the line contains a drug dose your fgets() - enabled machine might deliver only one tenth of the amount needed, given an off by one line length error.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



Relevant Pages

  • Re: why the usage of gets() is dangerous.
    ... that is to say one that always terminates the program ... with an error message if the buffer is exceeded. ... You have to make it safe within the guarantees provided by the C ...
    (comp.lang.c)
  • Re: why the usage of gets() is dangerous.
    ... though rather difficult, to implement a safe gets, that is to say one ... that always terminates the program with an error message if the buffer is ...
    (comp.lang.c)
  • Re: why the usage of gets() is dangerous.
    ... that is to say one that always terminates the program ... with an error message if the buffer is exceeded. ... We'll declare that pointer cosist of three values - the address, ...
    (comp.lang.c)
  • Re: why the usage of gets() is dangerous.
    ... that is to say one that always terminates the program ... with an error message if the buffer is exceeded. ... You have to make it safe within the guarantees provided by the C ... Also, the buffer passed to getsmay not be malloc'ed, but can be an array, or even a sub-array. ...
    (comp.lang.c)
  • Re: conveyor non-stop
    ... output is accumulated until the program using them either terminates ... or the buffer is filled ... for input and output, so, you cannot force an EOF in the input side ... sub-standard NNTP-gateways just apparently cannot process control ...
    (comp.unix.programmer)