Skip to content

Commit 534176e

Browse files
committed
Fix integer parsing on 32bit archs
Closes #274
1 parent 224deca commit 534176e

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

vm/vm/main/unpickler.cc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ namespace mozart {
2828

2929
namespace {
3030

31+
namespace internal {
32+
// Helpers to convert strings to nativeint
33+
template<size_t Size>
34+
struct StrTo {};
35+
36+
template<>
37+
struct StrTo<4> {
38+
static int32_t strto(const char* start, char** end, int base) {
39+
return std::strtol(start, end, base);
40+
}
41+
};
42+
43+
template<>
44+
struct StrTo<8> {
45+
static int64_t strto(const char* start, char** end, int base) {
46+
return std::strtoll(start, end, base);
47+
}
48+
};
49+
50+
template<typename T>
51+
static T strto(const char* start, char** end, int base) {
52+
return StrTo<sizeof(T)>::strto(start, end, base);
53+
}
54+
}
55+
3156
///////////////
3257
// Unpickler //
3358
///////////////
@@ -96,9 +121,8 @@ class Unpickler {
96121
std::string str = readString();
97122
char* end = nullptr;
98123
errno = 0; // reset errno since we need to know if strtoll() overflowed
99-
long long intResult = std::strtoll(str.c_str(), &end, 10);
124+
nativeint value = internal::strto<nativeint>(str.c_str(), &end, 10);
100125
assert(*end == '\0' && "bad integer string");
101-
nativeint value = (nativeint) intResult;
102126
if ((value == SmallInt::min() || value == SmallInt::max()) &&
103127
errno == ERANGE) { // Overflow
104128
return BigInt::build(vm, str);

0 commit comments

Comments
 (0)