Skip to content

Commit 5e0874f

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix #79908: json_encode encodes negative zero as int
2 parents 60fbd6d + 1ba190b commit 5e0874f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ext/json/json_encoder.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
8484

8585
php_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
8686
len = strlen(num);
87-
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2) {
87+
if ((options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2)
88+
|| (UNEXPECTED(len == 2 && num[0] == '-' && num[1] == '0'))) {
8889
num[len++] = '.';
8990
num[len++] = '0';
9091
num[len] = '\0';

ext/json/tests/bug79908.phpt

Lines changed: 12 additions & 0 deletions
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)