tony2001 Mon Feb 19 19:44:44 2007 UTC Modified files: /php-src/ext/json json.c /php-src/ext/json/tests bug40503.phpt Log: MFB: #40503 (json_encode() value corruption on 32bit systems with overflown values) http://cvs.php.net/viewvc.cgi/php-src/ext/json/json.c?r1=1.22&r2=1.23&diff_format=u Index: php-src/ext/json/json.c diff -u php-src/ext/json/json.c:1.22 php-src/ext/json/json.c:1.23 --- php-src/ext/json/json.c:1.22 Mon Jan 1 09:29:25 2007 +++ php-src/ext/json/json.c Mon Feb 19 19:44:44 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: json.c,v 1.22 2007/01/01 09:29:25 sebastian Exp $ */ +/* $Id: json.c,v 1.23 2007/02/19 19:44:44 tony2001 Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -362,17 +362,17 @@ 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'); } http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/bug40503.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/json/tests/bug40503.phpt diff -u /dev/null php-src/ext/json/tests/bug40503.phpt:1.2 --- /dev/null Mon Feb 19 19:44:44 2007 +++ php-src/ext/json/tests/bug40503.phpt Mon Feb 19 19:44:44 2007 @@ -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
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php