Skip to content

Commit 524dc20

Browse files
committed
Address review comments
1 parent 32ec1cc commit 524dc20

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

ext/bcmath/libbcmath/src/floor_or_ceil.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
3737
{
38-
bc_num fractional;
39-
4038
/* clear result */
4139
bc_free_num(result);
4240

@@ -51,20 +49,18 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
5149
return;
5250
}
5351

54-
/* copy fractional part */
55-
fractional = bc_new_num(0, num->n_scale);
56-
memcpy(fractional->n_value, num->n_value + num->n_len, num->n_scale);
52+
/* check fractional part. */
53+
size_t count = num->n_scale;
54+
char *nptr = num->n_value + num->n_len;
55+
while ((count > 0) && (*nptr++ == 0)) count--;
5756

58-
if (bc_is_zero(fractional)) {
59-
goto cleanup;
57+
if (count == 0) {
58+
return;
6059
}
6160

6261
/* add/sub 1 to/from result */
6362
bc_num tmp = _bc_do_add(*result, BCG(_one_), 0);
6463
tmp->n_sign = (*result)->n_sign;
6564
bc_free_num(result);
6665
*result = tmp;
67-
68-
cleanup:
69-
bc_free_num(&fractional);
7066
}

ext/bcmath/libbcmath/src/round.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
4949
* end, the 0's are omitted and the number of digits in num is reduced.
5050
* In that case, may end up in the same situation as 2.
5151
*/
52-
if ((int) num->n_len + precision <= 0) {
52+
if (precision < 0 && num->n_len <= labs(precision)) {
5353
*result = bc_copy_num(BCG(_zero_));
5454
return;
5555
}

ext/bcmath/tests/bcround.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ try {
183183
} catch (Throwable $e) {
184184
echo $e->getMessage()."\n";
185185
}
186+
187+
try {
188+
bcround('0.001', 0, 1000);
189+
} catch (Throwable $e) {
190+
echo $e->getMessage()."\n";
191+
}
186192
?>
187193
--EXPECT--
188194
========== early return ==========
@@ -842,3 +848,4 @@ try {
842848
========== value error ==========
843849
bcround(): Argument #1 ($num) is not well-formed
844850
bcround(): Argument #1 ($num) is not well-formed
851+
bcround(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*)

0 commit comments

Comments
 (0)