Re: gets() is dead
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 08:54:01 +0100
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
Actually the compiler can. It is possible, though unfortunately not easy, to write a compiler that handles buffer overflows on gets() correctly and safely, by terminating the program with an error message.
If you insist on writing unsafe code, nobody can stop you.
You haven't read the subthread. If it was trivial to use fgets() safely then peole wouldn't have had such trouble creating correct code. Chuck Falconer's ggets() does actually appear to work, but look at the number of conditions in the code he posted. You'd expect at least one bug in code of that complexity until after thorough testing.
It's
trivial to write your own function that works the same way gets()
does. (It's also trivial to write an equivalent of gets() that
doesn't have the same problem, something like fgets() that deletes the
trailing '\n' character.)
The problem with code that does the wrong thing, as opposed to code that invokes undefined behaviour, is that the compiler is obliged to honour a controlled flight into terrain. If you tell the computer to read only the first 10 digits of an 11-digit field, because you've forgotten space for the NUL in passing MAXLINLEN to fgets(), then the computer is obliged to return a value that is out by a factor of ten. If you tell it to overrun the buffer by one, it may return a number out by a factor of ten, but it will probably crash, we can write a compiler that guarantees it will crash, and it is almost certainly better that it crash than that it calculate the wrong value.
--
But if you want to shoot yourself in the foot, don't expect us to load
the gun for you. We just *might* unknowingly run your code ourselves
some day. And if your problem with C is that it does too much to
prevent buffer overflows, I'd rather not do that.
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- Follow-Ups:
- Re: gets() is dead
- From: Chris Dollin
- Re: gets() is dead
- From: Richard Heathfield
- Re: gets() is dead
- References:
- gets() is dead
- From: jacob navia
- Re: gets() is dead
- From: Tor Rustad
- Re: gets() is dead
- From: Charles Richmond
- Re: gets() is dead
- From: Harald van Dijk
- Re: gets() is dead
- From: Flash Gordon
- Re: gets() is dead
- From: Harald van Dijk
- Re: gets() is dead
- From: Keith Thompson
- Re: gets() is dead
- From: Harald van Dijk
- Re: gets() is dead
- From: Flash Gordon
- Re: gets() is dead
- From: Harald van Dijk
- Re: gets() is dead
- From: Charles Richmond
- Re: gets() is dead
- From: Keith Thompson
- gets() is dead
- Prev by Date: Re: A one's complement sanity check, please
- Next by Date: Re: String Comparision
- Previous by thread: Re: gets() is dead
- Next by thread: Re: gets() is dead
- Index(es):
Relevant Pages
|