Ben Bacarisse said:
CBFalconer <cb********@yah oo.comwrites:
>James Dow Allen wrote:
>>>
... snip about gets ...
>>>
Whenever I build the Index to the Fabulous Pedigree
http://fabpedigree.com/altix.htm
I do several hundred thousand gets()'s, but none of them are
"dangerous" . I live with a few "dangerous" messages during the
build (although I'm sure the pedants would prefer that each of
the several hundred thousand gets()'s produced its own such
message. :-)
They're all dangerous. For example, a one bit error in reading a
'\n' from your prewritten data can blow everything.
I assume you use it because of the simplicity of the call. You can
get that simplicity, together with safety, by using ggets.
How does ggets overcome the problem you've just identified with the
OP's use of gets?
It doesn't - insofar as it makes no special provision against one-bit
errors in reading a '\n' from your prewritten data. It does, however,
return an error if it runs out of memory. But it fails to take the obvious
precaution of allowing the caller to specify an upper limit to the number
of bytes taken from the stream.
So let's say you have this situation - you know your lines are no longer
than 6 bytes (the same argument applies to more typical line lengths, eg
72 or 80, but the much lower value is chosen simply because it is easy to
write and easy to read in a Usenet article).
Here are your data:
3.141
1.618
A one bit error in reading a '\n' from the 3.141 line results in it being
interpreted as a J instead (01001010 instead of 00001010). So if ggets
really did overcome this obstacle, it would detect the one-bit error and
correct for it or at least report it. In practice, what ggets will do is
read the next line too, so that you'll get:
3.141J1.618
which is very much *not* what is intended. Now, I'm not saying that ggets
*should* be able to defend against bit errors like this. I'm saying it
*doesn't*.
I have suggested, many times, that Mr Falconer should add a "maximum bytes
to read" parameter to ggets - and doing so would offer the caller a
reasonable line of defence against such an error: rc = ggets(&line, 6)
would give you a fighting chance of at least detecting that something is
wrong in the above situation. But ggets in its current state does not, so
the *best* you can hope for is that you run out of memory. Not a happy
state of affairs.
Again, I stress that there is no shame in an input routine not detecting
bit errors of this kind. But ggets was offered, by its author, as a
solution to the problem of a one bit error in reading a '\n' - and it is
no such thing.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999