Commit 0ca9a1fb authored by Henry's avatar Henry
Browse files

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

parent 4db6925c
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment