Skip to content

Commit 2d0a52f

Browse files
committed
Address comments 1
1 parent 524dc20 commit 2d0a52f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

ext/bcmath/libbcmath/src/floor_or_ceil.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,25 @@ void bc_floor_or_ceil(bc_num num, bool is_floor, bc_num *result)
4545
/* copy integer part */
4646
memcpy((*result)->n_value, num->n_value, num->n_len);
4747

48+
/* If the number is positive and we are flooring, then nothing else needs to be done.
49+
* Similarly, if the number is negative and we are ceiling, then nothing else needs to be done. */
4850
if (num->n_scale == 0 || (*result)->n_sign == (is_floor ? PLUS : MINUS)) {
4951
return;
5052
}
5153

5254
/* check fractional part. */
5355
size_t count = num->n_scale;
54-
char *nptr = num->n_value + num->n_len;
55-
while ((count > 0) && (*nptr++ == 0)) count--;
56+
const char *nptr = num->n_value + num->n_len;
57+
while ((count > 0) && (*nptr++ == 0)) {
58+
count--;
59+
}
5660

61+
/* If all digits past the decimal point are 0 */
5762
if (count == 0) {
5863
return;
5964
}
6065

61-
/* add/sub 1 to/from result */
66+
/* Increment the absolute value of the result by 1 and add sign information */
6267
bc_num tmp = _bc_do_add(*result, BCG(_one_), 0);
6368
tmp->n_sign = (*result)->n_sign;
6469
bc_free_num(result);

ext/bcmath/libbcmath/src/round.c

Lines changed: 10 additions & 4 deletions
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 (precision < 0 && num->n_len <= labs(precision)) {
52+
if (precision < 0 && num->n_len <= (size_t) (-(precision + Z_L(1))) + 1) {
5353
*result = bc_copy_num(BCG(_zero_));
5454
return;
5555
}
@@ -68,7 +68,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
6868
size_t rounded_len = num->n_len + precision;
6969
memcpy((*result)->n_value, num->n_value, rounded_len);
7070

71-
char *nptr = num->n_value + rounded_len;
71+
const char *nptr = num->n_value + rounded_len;
7272

7373
/* Check cases that can be determined without looping. */
7474
switch (mode) {
@@ -88,7 +88,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
8888
} else if (*nptr < 5) {
8989
return;
9090
}
91-
/* if *nptr == 5, a loop is required for judgment. */
91+
/* if *nptr == 5, we need to look-up further digits before making a decision. */
9292
break;
9393

9494
case PHP_ROUND_CEILING:
@@ -118,12 +118,16 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
118118
}
119119
/* if *nptr == 0, a loop is required for judgment. */
120120
break;
121+
122+
EMPTY_SWITCH_DEFAULT_CASE()
121123
}
122124

123125
/* Loop through the remaining digits. */
124126
size_t count = num->n_len + num->n_scale - rounded_len - 1;
125127
nptr++;
126-
while ((count > 0) && (*nptr++ == 0)) count--;
128+
while ((count > 0) && (*nptr++ == 0)) {
129+
count--;
130+
}
127131

128132
if (count > 0) {
129133
goto up;
@@ -147,6 +151,8 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
147151
return;
148152
}
149153
break;
154+
155+
EMPTY_SWITCH_DEFAULT_CASE()
150156
}
151157

152158
up:

0 commit comments

Comments
 (0)