Re: Why is it dangerous?
- From: spambait@xxxxxxxxxx (Doug Miller)
- Date: Sun, 10 Aug 2008 18:35:46 GMT
In article <3a0u94l248orcmuvn0o61gmjc9vrirkhfn@xxxxxxx>, Barry Schwarz <schwarzb@xxxxxxxx> wrote:
On Sun, 10 Aug 2008 09:27:13 +0100, "Malcolm McLean"
<regniztar@xxxxxxxxxxxxxx> wrote:
"Gordon Burditt" <gordonb.d6qjl@xxxxxxxxxxx> wrote in message
There is no non-dangerous gets() function with the same interface.This is a hardy annual.
The non-dangerous function is called fgets().
Of course fgets() can be used safely, but won't be. For instance Richard
Heathfield posted a dangerous use of fgets() in this very thread. It will
give the wrong answer if the user enters a string of over 2000 characters.
Any code that does not check the status of "service requests" is
dangerous. But this is a result of sloppy programming. It is not an
inherent property of the request itself as a call to gets() is.
Since gets() returns the same indication both when the input overflows the
buffer allocated for it, and when it doesn't -- thus necessarily precluding
the possibility of any such status check -- it could be argued that the use of
gets() constitutes "sloppy programming" in and of itself.
Exactly so.
Of course it is not dangerous in a little exercise program that doesn't do
anything, but then neither is gets().
I guess on your system undefined behavior can never do any harm.
To use fgets() safely you must check for the newline. If it is not present a
buffer overflow occurred. So you must then take action against the buffer to
Actually, a buffer overflow was prevented.
ensure that the next read doesn't get the remainder of the previous line.
The recommended action should be either:
Whatever the program needs to do to obtain the remainder of
the line so the input can be processed as intended .
Reject the input with appropriate notification to the user and
suitable follow-on action
.
- References:
- Why is it dangerous?
- From: Julian
- Re: Why is it dangerous?
- From: Gordon Burditt
- Re: Why is it dangerous?
- From: Malcolm McLean
- Re: Why is it dangerous?
- From: Barry Schwarz
- Why is it dangerous?
- Prev by Date: Re: Variable naming conventions.
- Next by Date: Re: Equivalency of Integral Pointers
- Previous by thread: Re: Why is it dangerous?
- Next by thread: Re: Why is it dangerous?
- Index(es):
Relevant Pages
|