mirror of
https://github.com/php/php-src.git
synced 2024-09-22 18:37:25 +00:00
MFB: #40503 (json_encode() value corruption on 32bit systems with overflown values)
This commit is contained in:
parent
d315853532
commit
4676d78ab5
@ -362,17 +362,17 @@ static void json_encode_r(smart_str *buf, zval *val TSRMLS_DC) /* {{{ */
|
||||
int len;
|
||||
double dbl = Z_DVAL_P(val);
|
||||
|
||||
if (!zend_isinf(dbl) && !zend_isnan(dbl))
|
||||
{
|
||||
if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
|
||||
len = spprintf(&d, 0, "%.9g", dbl);
|
||||
if (d)
|
||||
{
|
||||
smart_str_appendl(buf, d, len);
|
||||
if (d) {
|
||||
if (dbl > LONG_MAX && !memchr(d, '.', len)) {
|
||||
smart_str_append_unsigned(buf, (unsigned long)Z_DVAL_P(val));
|
||||
} else {
|
||||
smart_str_appendl(buf, d, len);
|
||||
}
|
||||
efree(d);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
zend_error(E_WARNING, "[json] (json_encode_r) double %.9g does not conform to the JSON spec, encoded as 0.", dbl);
|
||||
smart_str_appendc(buf, '0');
|
||||
}
|
||||
|
19
ext/json/tests/bug40503.phpt
Normal file
19
ext/json/tests/bug40503.phpt
Normal file
@ -0,0 +1,19 @@
|
||||
--TEST--
|
||||
Bug #40503 (json_encode() value corruption on 32bit systems with overflown values)
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded("json")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
function show_eq($x,$y) {
|
||||
echo "$x ". ($x==$y ? "==" : "!=") ." $y\n";
|
||||
}
|
||||
|
||||
$value = 0x7FFFFFFF; #2147483647;
|
||||
show_eq("$value", json_encode($value));
|
||||
$value++;
|
||||
show_eq("$value", json_encode($value));
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
2147483647 == 2147483647
|
||||
2147483648 == 2147483648
|
Loading…
Reference in New Issue
Block a user