Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8769 > unrolled thread

What's the most secure way to read a long int ?

Started byJulien <serval2412@yahoo.fr>
First post2011-08-13 11:12 +0200
Last post2011-08-14 05:58 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#8769 — What's the most secure way to read a long int ?

FromJulien <serval2412@yahoo.fr>
Date2011-08-13 11:12 +0200
SubjectWhat's the most secure way to read a long int ?
Message-ID<4e463fcc$0$32662$426a74cc@news.free.fr>
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 ?
would a memset (c, 0, sizeof(c)) useful before ?

Julien.

[toc] | [next] | [standalone]


#8771

FromIan Collins <ian-news@hotmail.com>
Date2011-08-13 21:20 +1200
Message-ID<9amtu7FdetU2@mid.individual.net>
In reply to#8769
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

[toc] | [prev] | [next] | [standalone]


#8775

FromJulien <serval2412@yahoo.fr>
Date2011-08-13 11:52 +0200
Message-ID<4e46491d$0$23660$426a74cc@news.free.fr>
In reply to#8771
>> ...
>
> Do it the C++ way:
>
> std::ifstream f( CPU_TIME );
> if (!f) return;
>
> long c;
> f >> c;
>
> if(!f) return;
>
Ok, cppcheck is mute about this now.

What about for this kind of code ?
time_t read_progress() {
     time_t stored_secs;
     FILE* f = fopen(PROGRESS_FN, "r");
     if (!f) return(0);
     int n = fscanf(f, "%ld",&stored_secs);
     fclose(f);
     if (n != 1) return(0);
     else return(stored_secs);
}

It's quite the same except the variable is a struct. So f >> stored_secs
  wouldn't work here.

Julien

[toc] | [prev] | [next] | [standalone]


#8779

FromIan Collins <ian-news@hotmail.com>
Date2011-08-13 22:57 +1200
Message-ID<9an3kiFdeuU1@mid.individual.net>
In reply to#8775
On 08/13/11 09:52 PM, Julien wrote:

Please don't snip attributions, it's rude.

 > I wrote:

>> Do it the C++ way:
>>
>> std::ifstream f( CPU_TIME );
>> if (!f) return;
>>
>> long c;
>> f>>  c;
>>
>> if(!f) return;
>>
> Ok, cppcheck is mute about this now.
>
> What about for this kind of code ?

It's horrible...

> time_t read_progress() {
>       time_t stored_secs;
>       FILE* f = fopen(PROGRESS_FN, "r");
>       if (!f) return(0);
>       int n = fscanf(f, "%ld",&stored_secs);
>       fclose(f);
>       if (n != 1) return(0);
>       else return(stored_secs);
> }
>
> It's quite the same except the variable is a struct. So f>>  stored_secs
>    wouldn't work here.

Which variable is a struct?

Why do you want to do things the C way, rather than the more idiomatic 
C++ forms?

fscanf requires you to get the types right, iostreams delegate the task 
to the compiler.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#8782

FromJulien <serval2412@yahoo.fr>
Date2011-08-13 13:46 +0200
Message-ID<4e4663d2$0$420$426a34cc@news.free.fr>
In reply to#8779
Le 13/08/2011 12:57, Ian Collins a écrit :
> On 08/13/11 09:52 PM, Julien wrote:
>
> Please don't snip attributions, it's rude.
>
Sorry for this. I've got to remember this.
> ...
>>> ...
>> What about for this kind of code ?
>
> It's horrible...
>
>> time_t read_progress() {
>> time_t stored_secs;
>> FILE* f = fopen(PROGRESS_FN, "r");
>> if (!f) return(0);
>> int n = fscanf(f, "%ld",&stored_secs);
>> fclose(f);
>> if (n != 1) return(0);
>> else return(stored_secs);
>> }
>>
>> It's quite the same except the variable is a struct. So f>> stored_secs
>> wouldn't work here.
>
> Which variable is a struct?
Sorry, I made a mistake, time_t is not a struct but a datatype.
> Why do you want to do things the C way, rather than the more idiomatic
> C++ forms?
>
In fact, I try to correct cppcheck errors of a file on the internet 
which is C style whereas the file has cpp extension.
I replaced the code above by this (the same way of the code you gave 
before) :
time_t read_progress() {
     time_t stored_secs;
     std::ifstream f(CPU_TIME);
     if (!f) return 0;
     f >> stored_secs;
     if (!f) return 0;
     else return stored_secs;
}

> fscanf requires you to get the types right, iostreams delegate the task
> to the compiler.
Ok.
Thank you for your help. I'll do other changes to use the C++ style.

Sorry again for having snipped attributions (I never know if i cut too 
little or too much)

Julien.

[toc] | [prev] | [next] | [standalone]


#8819

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-08-14 05:58 +0000
Message-ID<slrnj4ep0d.gtj.grahn+nntp@frailea.sa.invalid>
In reply to#8779
On Sat, 2011-08-13, Ian Collins wrote:
> On 08/13/11 09:52 PM, Julien wrote:
...
>>       int n = fscanf(f, "%ld",&stored_secs);
...

> Why do you want to do things the C way, rather than the more idiomatic 
> C++ forms?

To be fair to C, scanf() is not /the/ C way, just /a/ C way.
In both languages, I prefer to do my own parsing, using strtol() and
friends.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web