Skip to content

Commit 07e675c

Browse files
author
Scott MacVicar
committed
MFH Fix bug #47644 - Valid integers are truncated with json_decode()
1 parent 1d5dbea commit 07e675c

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

ext/json/JSON_parser.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ static void json_create_zval(zval **z, smart_str *buf, int type)
289289

290290
if (type == IS_LONG)
291291
{
292-
double d = zend_strtod(buf->c, NULL);
293-
if (d > LONG_MAX || d < LONG_MIN) {
294-
ZVAL_DOUBLE(*z, d);
292+
long l = strtol(buf->c, NULL, 10);
293+
if (l > LONG_MAX || l < LONG_MIN) {
294+
ZVAL_DOUBLE(*z, zend_strtod(buf->c, NULL));
295295
} else {
296-
ZVAL_LONG(*z, (long)d);
296+
ZVAL_LONG(*z, l);
297297
}
298298
}
299299
else if (type == IS_DOUBLE)

ext/json/tests/bug47644.phpt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Bug #47644 (valid large integers are truncated)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('json')) die('skip: json extension not available');
6+
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
for ($i = 10000000000000000; $i < 10000000000000006; $i++) {
12+
var_dump(json_decode("[$i]"));
13+
}
14+
15+
16+
echo "Done\n";
17+
?>
18+
--EXPECT--
19+
array(1) {
20+
[0]=>
21+
int(10000000000000000)
22+
}
23+
array(1) {
24+
[0]=>
25+
int(10000000000000001)
26+
}
27+
array(1) {
28+
[0]=>
29+
int(10000000000000002)
30+
}
31+
array(1) {
32+
[0]=>
33+
int(10000000000000003)
34+
}
35+
array(1) {
36+
[0]=>
37+
int(10000000000000004)
38+
}
39+
array(1) {
40+
[0]=>
41+
int(10000000000000005)
42+
}
43+
Done

0 commit comments

Comments
 (0)