diff --git a/NEWS b/NEWS index da14c247ee0ec..26bbf705270d1 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.4.0RC4 +- BcMath: + . Fixed bug GH-16265 (Added early return case when result is 0) + (Saki Takamachi). + - Core: . Fixed bug GH-16574 (Incorrect error "undefined method" messages). (nielsdos) diff --git a/ext/bcmath/libbcmath/src/div.c b/ext/bcmath/libbcmath/src/div.c index 35ca450ceb78c..a9c001afcc94b 100644 --- a/ext/bcmath/libbcmath/src/div.c +++ b/ext/bcmath/libbcmath/src/div.c @@ -444,6 +444,11 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale) /* Length of numerator data that can be read */ size_t numerator_readable_len = numeratorend - numeratorptr + 1; + if (divisor_len > numerator_readable_len + numerator_bottom_extension) { + *quot = bc_copy_num(BCG(_zero_)); + return true; + } + /* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */ if (divisor_len == 1 && *divisorptr == 1) { if (numerator_len == 0) { diff --git a/ext/bcmath/tests/gh16265.phpt b/ext/bcmath/tests/gh16265.phpt new file mode 100644 index 0000000000000..bc562d81df1ad --- /dev/null +++ b/ext/bcmath/tests/gh16265.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-16265 Segmentation fault (index oob) in ext/bcmath/libbcmath/src/convert.c:155 +--EXTENSIONS-- +bcmath +--INI-- +bcmath.scale=0 +--FILE-- + +--EXPECT-- +0.000000000