|
From: Ioannis V. <iv...@at...> - 2003-04-26 01:48:03
|
> -----Original Message-----
> From: Christian Sch=E4fer [mailto:ca...@kr...]=20
> Sent: Friday, April 25, 2003 11:55 PM
> To: dev...@li...; Ioannis Vranos
> Cc: dev...@li...
> Subject: Re[2]: [Dev-C++] stdin reset...
>=20
>=20
> 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'?
>=20
> 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..)
>=20
> I don't think this is a unique problem so there must be a known way to
> solve it, is there?
fflush() is intended for output streams, not input. For example
fflush(stdin); does not work in GNU/Linux. So you can't expect
fflush(stdin); to be portable. You will have to use aither a plain
getchar(); call after a plain scanf() or create a clean function in the
style:
void clean_stdin()
{
while(getchar()!=3D'\n')
;
}
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|