Skip to content

Commit 9fbcaa5

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #78238: BCMath returns "-0"
2 parents 12a858a + bcb89c7 commit 9fbcaa5

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP NEWS
55
- Core:
66
. Fixed bug #72595 (php_output_handler_append illegal write access). (cmb)
77

8+
- BCMath:
9+
. Fixed bug #78238 (BCMath returns "-0"). (cmb)
10+
811
- CGI:
912
. Fixed bug #80849 (HTTP Status header truncation). (cmb)
1013

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2));
120120

121121
_PROTOTYPE(char bc_is_zero, (bc_num num));
122122

123+
_PROTOTYPE(char bc_is_zero_for_scale, (bc_num num, int scale));
124+
123125
_PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale));
124126

125127
_PROTOTYPE(char bc_is_neg, (bc_num num));

ext/bcmath/libbcmath/src/num2str.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ zend_string
5050
int index, signch;
5151

5252
/* Allocate the string memory. */
53-
signch = num->n_sign != PLUS; /* Number of sign chars. */
53+
signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale)); /* Number of sign chars. */
5454
if (scale > 0)
5555
str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
5656
else

ext/bcmath/libbcmath/src/zero.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/* In some places we need to check if the number NUM is zero. */
4141

4242
char
43-
bc_is_zero (bc_num num)
43+
bc_is_zero_for_scale (bc_num num, int scale)
4444
{
4545
int count;
4646
char *nptr;
@@ -49,7 +49,7 @@ bc_is_zero (bc_num num)
4949
if (num == BCG(_zero_)) return TRUE;
5050

5151
/* Initialize */
52-
count = num->n_len + num->n_scale;
52+
count = num->n_len + scale;
5353
nptr = num->n_value;
5454

5555
/* The check */
@@ -60,3 +60,9 @@ bc_is_zero (bc_num num)
6060
else
6161
return TRUE;
6262
}
63+
64+
char
65+
bc_is_zero (bc_num num)
66+
{
67+
return bc_is_zero_for_scale(num, num->n_scale);
68+
}

ext/bcmath/tests/bug78238.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #78238 (BCMath returns "-0")
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('bcmath')) die("sikp bcmath extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(bcadd("0", "-0.1"));
10+
11+
$a = bcmul("0.9", "-0.1", 1);
12+
$b = bcmul("0.90", "-0.1", 1);
13+
var_dump($a, $b);
14+
?>
15+
--EXPECT--
16+
string(1) "0"
17+
string(3) "0.0"
18+
string(3) "0.0"

0 commit comments

Comments
 (0)