MFB: #40503 (json_encode() value corruption on 32bit systems with overflown values)

This commit is contained in:
Antony Dovgal 2007-02-19 19:44:44 +00:00
parent d315853532
commit 4676d78ab5
2 changed files with 27 additions and 8 deletions

View File

@ -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');
}

View 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