RES: Re[2]: [Dev-C++] stdin reset...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2003-04-25 21:57:59
|
> >> fflush(stdin);
> > Which is not safe. getchar(), fgets(), etc, should be used instead,
>
> and why is that?
> I am very interested in code security issues but at least i want to
> understand instead of obey them. so what exactly is considered
> 'unsafe'?
The same reason you shouldn't fflush(2.5), fflush("some string") nor
fflush(a, b, c), you shouldn't fflush(stdin); the standard says that
"fflush()" is for output streams; not input streams, not strings, not
anything else; the non-standard way can work on a particular system, but it
can kill some lives on others.
> what I need is a secure way to read as much as there is from stdin
> undtil '\n' into a char* buffer. I want that buffer to match the
> needed length (I don't know how to do that, since I don't know the
> exact needed length until i read it from stdin...) and I need to avoid
> buffer overflows (of course).
> I thought scanf() is a good way, since it reads only a specified
> number of characters right? (ok, this is just a workaround, I guess..)
Try fgets(), then sscanf().
> I don't think this is a unique problem so there must be a known way to
> solve it, is there?
Suggestion (for all you C programmers):
The comp.lang.c answers to frequently asked questions:
http://www.eskimo.com/~scs/C-faq/top.html
Daniel K. O.
|