Skip to content

Commit

Permalink
Fix #78238: BCMath returns "-0"
Browse files Browse the repository at this point in the history
There is no negative zero in the decimal system, so we must suppress
the sign.

Closes GH-7250.
  • Loading branch information
cmb69 committed Jul 16, 2021
1 parent 8f97f82 commit bcb89c7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ PHP NEWS
- Core:
. Fixed bug #72595 (php_output_handler_append illegal write access). (cmb)

- BCMath:
. Fixed bug #78238 (BCMath returns "-0"). (cmb)

- CGI:
. Fixed bug #80849 (HTTP Status header truncation). (cmb)

Expand Down
2 changes: 2 additions & 0 deletions ext/bcmath/libbcmath/src/bcmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_for_scale, (bc_num num, int scale));

_PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale));

_PROTOTYPE(char bc_is_neg, (bc_num num));
Expand Down
2 changes: 1 addition & 1 deletion ext/bcmath/libbcmath/src/num2str.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ zend_string
int index, signch;

/* 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)
str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
else
Expand Down
10 changes: 8 additions & 2 deletions ext/bcmath/libbcmath/src/zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/* In some places we need to check if the number NUM is zero. */

char
bc_is_zero (bc_num num)
bc_is_zero_for_scale (bc_num num, int scale)
{
int count;
char *nptr;
Expand All @@ -49,7 +49,7 @@ bc_is_zero (bc_num num)
if (num == BCG(_zero_)) return TRUE;

/* Initialize */
count = num->n_len + num->n_scale;
count = num->n_len + scale;
nptr = num->n_value;

/* The check */
Expand All @@ -60,3 +60,9 @@ bc_is_zero (bc_num num)
else
return TRUE;
}

char
bc_is_zero (bc_num num)
{
return bc_is_zero_for_scale(num, num->n_scale);
}
18 changes: 18 additions & 0 deletions ext/bcmath/tests/bug78238.phpt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit bcb89c7

Please sign in to comment.