php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41567 json_encode double conversion is inconsistent with PHP
Submitted: 2007-06-02 03:44 UTC Modified: 2007-06-05 12:21 UTC
From: lucas at facebook dot com Assigned: iliaa (profile)
Status: Closed Package: JSON related
PHP Version: 5CVS-2007-06-02 (CVS) OS: linux, osx
Private report: No CVE-ID: None
 [2007-06-02 03:44 UTC] lucas at facebook dot com
Description:
------------
json_encode is ignoring PHP integer precision and losing precision on large doubles. The patch below changes json_enode double to match the same conversion done internally in _convert_to_string.

This is related to http://bugs.php.net/bug.php?id=40503 and reverts the patch applied there. Tested against these use cases as well.

Reproduce code:
---------------
json_encode(123456789.12345)

Expected result:
----------------
123456789.12345 = 123456789.12345

Actual result:
--------------
123456789.12345 = 123456789



Index: json.c
===================================================================
RCS file: /repository/php-src/ext/json/json.c,v
retrieving revision 1.9.2.15
diff -u -r1.9.2.15 json.c
--- json.c      25 May 2007 13:24:50 -0000      1.9.2.15
+++ json.c      2 Jun 2007 03:41:47 -0000
@@ -354,15 +354,11 @@
                 double dbl = Z_DVAL_P(val);
 
                 if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
-                       len = spprintf(&d, 0, "%.9g", dbl);
-                       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);
-                       }
+                    len = spprintf(&d, 0, "%.*G", (int) EG(precision), dbl);
+                    if (d) {
+                        smart_str_appendl(buf, d, len);
+                        efree(d);
+                    }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-05 12:21 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC