Integer read: use strtoimax rather than strtol and check explicitly for overflow of int32_t

This commit is contained in:
Henry
2015-02-18 21:24:09 +00:00
parent eeedf5a051
commit ecee27065d
2 changed files with 7 additions and 3 deletions

View File

@ -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);
}

View File

@ -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);
}