Skip to content

Commit ef03609

Browse files
committed
Fixed GH-17275: Fixed the calculation logic of dividend scale (#17279)
Fixes #17275 Closes #17279
1 parent f4fb77e commit ef03609

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88
(Saki Takamachi)
99
. Fixed bug GH-17064 (Correctly round rounding mode with zero edge case).
1010
(Saki Takamachi)
11+
. Fixed bug GH-17275 (Fixed the calculation logic of dividend scale).
12+
(Saki Takamachi)
1113

1214
- Core:
1315
. Fixed bug OSS-Fuzz #382922236 (Duplicate dynamic properties in hooked object

ext/bcmath/libbcmath/src/div.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,30 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
427427
return true;
428428
}
429429

430+
/* Length of numerator data that can be read */
431+
size_t numerator_readable_len = numeratorend - numeratorptr + 1;
432+
430433
/* set scale to numerator */
431434
if (numerator_scale > scale) {
432435
size_t scale_diff = numerator_scale - scale;
433436
if (numerator_bottom_extension > scale_diff) {
434437
numerator_bottom_extension -= scale_diff;
435438
} else {
436439
numerator_bottom_extension = 0;
437-
numeratorend -= scale_diff > numerator_top_extension ? scale_diff - numerator_top_extension : 0;
440+
if (EXPECTED(numerator_readable_len > scale_diff)) {
441+
numerator_readable_len -= scale_diff;
442+
numeratorend -= scale_diff;
443+
} else {
444+
numerator_readable_len = 0;
445+
numeratorend = numeratorptr;
446+
}
438447
}
439448
numerator_top_extension = MIN(numerator_top_extension, scale);
440449
} else {
441450
numerator_bottom_extension += scale - numerator_scale;
442451
}
443452
numerator_scale = scale;
444453

445-
/* Length of numerator data that can be read */
446-
size_t numerator_readable_len = numeratorend - numeratorptr + 1;
447-
448454
if (divisor_len > numerator_readable_len + numerator_bottom_extension) {
449455
*quot = bc_copy_num(BCG(_zero_));
450456
return true;

ext/bcmath/tests/gh17275.phpt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-17275 Incorrect result of bcdiv function
3+
--EXTENSIONS--
4+
bcmath
5+
--FILE--
6+
<?php
7+
var_dump(
8+
bcdiv('0.03772321', '9650.0', 8),
9+
bcdiv('0.03772321', '9650.0', 9),
10+
);
11+
?>
12+
--EXPECT--
13+
string(10) "0.00000390"
14+
string(11) "0.000003909"

0 commit comments

Comments
 (0)