Skip to content

Commit 20a8ba1

Browse files
author
Bogdan Degtyariov
committed
Processing 64-bit large numbers in parser
1 parent be13399 commit 20a8ba1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cdk/parser/json_parser.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ PUSH_BOOST_WARNINGS
3535
POP_BOOST_WARNINGS
3636
POP_SYS_WARNINGS
3737

38+
/*
39+
The maximum absolute value that 64-bit signed integer can have.
40+
For most platforms it is the minimum negative LLONG_MIN.
41+
The absolute value for it cannot be obtained because
42+
ABS(LLONG_MAX) < ABS(LLONG_MIN). Therefore, we define a constant.
43+
*/
44+
#define INTEGER_ABS_MAX 9223372036854775808UL
3845

3946
using namespace parser;
4047
using cdk::string;
@@ -108,7 +115,7 @@ bool JSON_scalar_parser::do_parse(It &first, const It &last, Processor *vp)
108115
if(vp)
109116
{
110117
uint64_t val = boost::lexical_cast<uint64_t>(first->get_text());
111-
if (val > (uint64_t)std::numeric_limits<int64_t>::max())
118+
if (val > INTEGER_ABS_MAX)
112119
{
113120
if (neg)
114121
throw Error("The value is too large for a signed type");
@@ -117,7 +124,10 @@ bool JSON_scalar_parser::do_parse(It &first, const It &last, Processor *vp)
117124
}
118125
else
119126
{
120-
// All values MOD(val) < LLONG_MAX are signed
127+
// Absolute values of 9223372036854775808UL can only be negative
128+
if (!neg && val == INTEGER_ABS_MAX)
129+
throw Error("The value is too large for a signed type");
130+
// All values ABS(val) < 9223372036854775808UL are treated as signed
121131
vp->num(neg ? -(int64_t)val : (int64_t)val);
122132
}
123133
}

0 commit comments

Comments
 (0)