Integer read: use strtoimax rather than strtol and check explicitly for overflow of int32_t
This commit is contained in:
parent
4db6925cee
commit
0ca9a1fb09
2 changed files with 7 additions and 3 deletions
|
|
@ -28,6 +28,7 @@ License
|
|||
#include "int32.H"
|
||||
#include "IOstreams.H"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <sstream>
|
||||
#include <cerrno>
|
||||
|
||||
|
|
@ -87,9 +88,11 @@ bool Foam::read(const char* buf, int32_t& s)
|
|||
{
|
||||
char *endptr = NULL;
|
||||
errno = 0;
|
||||
long l = strtol(buf, &endptr, 10);
|
||||
intmax_t l = strtoimax(buf, &endptr, 10);
|
||||
s = int32_t(l);
|
||||
return (*endptr == 0) && (errno == 0);
|
||||
return
|
||||
(*endptr == 0) && (errno == 0)
|
||||
&& (l >= INT32_MIN) && (l <= INT32_MAX);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ License
|
|||
#include "int64.H"
|
||||
#include "IOstreams.H"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <sstream>
|
||||
#include <cerrno>
|
||||
|
||||
|
|
@ -87,7 +88,7 @@ bool Foam::read(const char* buf, int64_t& s)
|
|||
{
|
||||
char *endptr = NULL;
|
||||
errno = 0;
|
||||
long l = strtol(buf, &endptr, 10);
|
||||
intmax_t l = strtoimax(buf, &endptr, 10);
|
||||
s = int64_t(l);
|
||||
return (*endptr == 0) && (errno == 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue