Fix #78238: BCMath returns "-0"

There is no negative zero in the decimal system, so we must suppress
the sign.

Closes GH-7250.
This commit is contained in:
Christoph M. Becker 2021-07-16 14:49:25 +02:00
parent 8f97f82e35
commit bcb89c75ec
No known key found for this signature in database
GPG key ID: D66C9593118BCCB6
5 changed files with 32 additions and 3 deletions

3
NEWS
View file

@ -5,6 +5,9 @@ PHP NEWS
- Core: - Core:
. Fixed bug #72595 (php_output_handler_append illegal write access). (cmb) . Fixed bug #72595 (php_output_handler_append illegal write access). (cmb)
- BCMath:
. Fixed bug #78238 (BCMath returns "-0"). (cmb)
- CGI: - CGI:
. Fixed bug #80849 (HTTP Status header truncation). (cmb) . Fixed bug #80849 (HTTP Status header truncation). (cmb)

View file

@ -120,6 +120,8 @@ _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2));
_PROTOTYPE(char bc_is_zero, (bc_num num)); _PROTOTYPE(char bc_is_zero, (bc_num num));
_PROTOTYPE(char bc_is_zero_for_scale, (bc_num num, int scale));
_PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale)); _PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale));
_PROTOTYPE(char bc_is_neg, (bc_num num)); _PROTOTYPE(char bc_is_neg, (bc_num num));

View file

@ -50,7 +50,7 @@ zend_string
int index, signch; int index, signch;
/* Allocate the string memory. */ /* Allocate the string memory. */
signch = num->n_sign != PLUS; /* Number of sign chars. */ signch = num->n_sign != PLUS && !bc_is_zero_for_scale(num, MIN(num->n_scale, scale)); /* Number of sign chars. */
if (scale > 0) if (scale > 0)
str = zend_string_alloc(num->n_len + scale + signch + 1, 0); str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
else else

View file

@ -40,7 +40,7 @@
/* In some places we need to check if the number NUM is zero. */ /* In some places we need to check if the number NUM is zero. */
char char
bc_is_zero (bc_num num) bc_is_zero_for_scale (bc_num num, int scale)
{ {
int count; int count;
char *nptr; char *nptr;
@ -49,7 +49,7 @@ bc_is_zero (bc_num num)
if (num == BCG(_zero_)) return TRUE; if (num == BCG(_zero_)) return TRUE;
/* Initialize */ /* Initialize */
count = num->n_len + num->n_scale; count = num->n_len + scale;
nptr = num->n_value; nptr = num->n_value;
/* The check */ /* The check */
@ -60,3 +60,9 @@ bc_is_zero (bc_num num)
else else
return TRUE; return TRUE;
} }
char
bc_is_zero (bc_num num)
{
return bc_is_zero_for_scale(num, num->n_scale);
}

View file

@ -0,0 +1,18 @@
--TEST--
Bug #78238 (BCMath returns "-0")
--SKIPIF--
<?php
if (!extension_loaded('bcmath')) die("sikp bcmath extension not available");
?>
--FILE--
<?php
var_dump(bcadd("0", "-0.1"));
$a = bcmul("0.9", "-0.1", 1);
$b = bcmul("0.90", "-0.1", 1);
var_dump($a, $b);
?>
--EXPECT--
string(1) "0"
string(3) "0.0"
string(3) "0.0"