Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8771
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: What's the most secure way to read a long int ? |
| Date | 2011-08-13 21:20 +1200 |
| Message-ID | <9amtu7FdetU2@mid.individual.net> (permalink) |
| References | <4e463fcc$0$32662$426a74cc@news.free.fr> |
On 08/13/11 09:12 PM, Julien wrote:
> Hello,
>
> I used cppcheck to detect problems and had this :
> (warning) scanf without field width limits can crash with huge input data
>
> void read_cputime(double& cpu) {
> long int c;
> cpu = 0;
> FILE* f = fopen(CPU_TIME, "r");
> if (!f) return;
> int n = fscanf(f, "%ld",&c);<-- pb detected
> fclose(f);
> if (n != 1) return;
> cpu = c;
> }
>
> First I thought about adding a number in the format :
> int n = fscanf(f, "%4ld",&c);
>
> But I want the code to be portable (it must ok for 32 bits or 64 bits).
> So what to use ? A macro ? A c++ const ? C++ internal library (cin ?) ?
> other ?
Do it the C++ way:
std::ifstream f( CPU_TIME );
if (!f) return;
long c;
f >> c;
if(!f) return;
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 11:12 +0200
Re: What's the most secure way to read a long int ? Ian Collins <ian-news@hotmail.com> - 2011-08-13 21:20 +1200
Re: What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 11:52 +0200
Re: What's the most secure way to read a long int ? Ian Collins <ian-news@hotmail.com> - 2011-08-13 22:57 +1200
Re: What's the most secure way to read a long int ? Julien <serval2412@yahoo.fr> - 2011-08-13 13:46 +0200
Re: What's the most secure way to read a long int ? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-14 05:58 +0000
csiph-web