Skip to content

Commit 1ba190b

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #79908: json_encode encodes negative zero as int
2 parents 1631b96 + 717f1ed commit 1ba190b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ PHP NEWS
2323
. Fixed bug #68471 (IntlDateFormatter fails for "GMT+00:00" timezone). (cmb)
2424
. Fixed bug #74264 (grapheme_strrpos() broken for negative offsets). (cmb)
2525

26+
- JSON:
27+
. Fixed bug #79908 (json_encode encodes negative zero as int). (cmb)
28+
2629
- OpenSSL:
2730
. Fixed bug #52093 (openssl_csr_sign truncates $serial). (cmb)
2831

ext/json/json_encoder.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
102102

103103
php_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
104104
len = strlen(num);
105-
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2) {
105+
if ((options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2)
106+
|| (UNEXPECTED(len == 2 && num[0] == '-' && num[1] == '0'))) {
106107
num[len++] = '.';
107108
num[len++] = '0';
108109
num[len] = '\0';

ext/json/tests/bug79908.phpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug #79908 (json_encode encodes negative zero as int)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('json')) die("skip json extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(json_encode(-0.));
10+
?>
11+
--EXPECT--
12+
string(4) "-0.0"

0 commit comments

Comments
 (0)