[php-src] master: Merge branch 'PHP-8.6'

From: Date: Tue, 29 Sep 2026 19:55:03 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141737@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-09-29T15:47:50-04:00 Commit: https://github.com/php/php-src/commit/b19f17e125760a4d6aafa1a8e521962e7194037d Raw diff: https://github.com/php/php-src/commit/b19f17e125760a4d6aafa1a8e521962e7194037d.diff Merge branch 'PHP-8.6' * PHP-8.6: ext/bcmath: Clear the sign of BcMath\Number results that truncate to zero Changed paths: A ext/bcmath/tests/number/methods/calc_methods_zero_result_sign.phpt M ext/bcmath/bcmath.c M ext/bcmath/libbcmath/src/div.c M ext/bcmath/libbcmath/src/raise.c Diff: diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 0e0a9a23b94a..dc693ad65e91 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -1058,6 +1058,9 @@ static zend_always_inline void bcmath_number_add_internal( } *ret = bc_add(n1, n2, *scale); (*ret)->n_scale = MIN(*scale, (*ret)->n_scale); + if (bc_is_zero(*ret)) { + (*ret)->n_sign = PLUS; + } bc_rm_trailing_zeros(*ret); } @@ -1070,6 +1073,9 @@ static zend_always_inline void bcmath_number_sub_internal( } *ret = bc_sub(n1, n2, *scale); (*ret)->n_scale = MIN(*scale, (*ret)->n_scale); + if (bc_is_zero(*ret)) { + (*ret)->n_sign = PLUS; + } bc_rm_trailing_zeros(*ret); } @@ -1086,6 +1092,9 @@ static zend_always_inline zend_result bcmath_number_mul_internal( } *ret = bc_multiply(n1, n2, *scale); (*ret)->n_scale = MIN(*scale, (*ret)->n_scale); + if (bc_is_zero(*ret)) { + (*ret)->n_sign = PLUS; + } bc_rm_trailing_zeros(*ret); return SUCCESS; } diff --git a/ext/bcmath/libbcmath/src/div.c b/ext/bcmath/libbcmath/src/div.c index 24ec9a64d77f..56db164e4448 100644 --- a/ext/bcmath/libbcmath/src/div.c +++ b/ext/bcmath/libbcmath/src/div.c @@ -350,7 +350,11 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale) /* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */ if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) { bc_divide_by_one(numerator, quot, quot_scale); - (*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS; + if (bc_is_zero(*quot)) { + (*quot)->n_sign = PLUS; + } else { + (*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS; + } return true; } @@ -405,7 +409,11 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale) /* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */ if (divisor_size == 1 && *divisorptr == 1) { bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale); - (*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS; + if (bc_is_zero(*quot)) { + (*quot)->n_sign = PLUS; + } else { + (*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS; + } return true; } diff --git a/ext/bcmath/libbcmath/src/raise.c b/ext/bcmath/libbcmath/src/raise.c index 959ba924e57f..ef39148327f8 100644 --- a/ext/bcmath/libbcmath/src/raise.c +++ b/ext/bcmath/libbcmath/src/raise.c @@ -254,6 +254,9 @@ bc_raise_status bc_raise(bc_num base, long exponent, bc_num *result, size_t scal bc_free_num (result); *result = power; (*result)->n_scale = MIN(scale, (*result)->n_scale); + if (bc_is_zero(*result)) { + (*result)->n_sign = PLUS; + } } return BC_RAISE_STATUS_OK; } diff --git a/ext/bcmath/tests/number/methods/calc_methods_zero_result_sign.phpt b/ext/bcmath/tests/number/methods/calc_methods_zero_result_sign.phpt new file mode 100644 index 000000000000..1871f0e3ca2c --- /dev/null +++ b/ext/bcmath/tests/number/methods/calc_methods_zero_result_sign.phpt @@ -0,0 +1,35 @@ +--TEST-- +BcMath\Number calc methods return an unsigned zero when the result truncates to zero +--EXTENSIONS-- +bcmath +--FILE-- +<?php +$cases = [ + ['div', '-0.001', '1', 0], + ['div', '0.001', '-1', 0], + ['div', '-0.001', '10', 2], + ['div', '-0.001', '0.1', 1], + ['add', '-0.001', '-0.001', 0], + ['sub', '-0.001', '0.001', 0], + ['mul', '-0.001', '1', 0], + ['pow', '-0.1', 3, 2], +]; + +foreach ($cases as [$method, $num, $arg, $scale]) { + $ret = (new BcMath\Number($num))->$method($arg, $scale); + echo "{$num} {$method} {$arg}: {$ret} ", $ret <=> 0, ' ', var_export($ret == 0, true), "\n"; +} + +[$quot, $rem] = (new BcMath\Number('-0.001'))->divmod('1', 0); +echo "-0.001 divmod 1: {$quot} ", $quot <=> 0, ' ', var_export($quot == 0, true), "\n"; +?> +--EXPECT-- +-0.001 div 1: 0 0 true +0.001 div -1: 0 0 true +-0.001 div 10: 0.00 0 true +-0.001 div 0.1: 0.0 0 true +-0.001 add -0.001: 0 0 true +-0.001 sub 0.001: 0 0 true +-0.001 mul 1: 0 0 true +-0.1 pow 3: 0.00 0 true +-0.001 divmod 1: 0 0 true

« previous php.cvs (#141737) next »