[php-src] PHP-8.4: Clamp scale in bc_is_zero_for_scale to n_scale
| From: | Ilia Alshanetsky | Date: | Sun, 06 Sep 2026 14:17:01 +0000 |
| Subject: | [php-src] PHP-8.4: Clamp scale in bc_is_zero_for_scale to n_scale | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141094@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-09-06T10:03:46-04:00
Commit: https://github.com/php/php-src/commit/979c827cdc393a164a2fbfeb524ec76ada3ef78c
Raw diff: https://github.com/php/php-src/commit/979c827cdc393a164a2fbfeb524ec76ada3ef78c.diff
Clamp scale in bc_is_zero_for_scale to n_scale
Number::compare() may request a scale greater than the stored scale of a
zero result when the opposite operand has a larger scale. The helper then
walked past the shorter digit buffer.
Clamp the requested scale to the digits stored in the number.
Closes GH-23185
Changed paths:
A ext/bcmath/tests/bc_is_zero_for_scale_clamp.phpt
M NEWS
M ext/bcmath/libbcmath/src/zero.c
Diff:
diff --git a/NEWS b/NEWS
index ab8e20c75077..1cc265f02e8d 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,10 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.4.26
+- BCMath:
+ . Fixed out-of-bounds read in bc_is_zero_for_scale() when scale exceeds
+ n_scale. (Ilia Alshanetsky)
+
- Core:
. Fixed bug GH-15375 (Nested "yield from" skips items after a valid() or
next() call on the inner generator). (iliaal)
diff --git a/ext/bcmath/libbcmath/src/zero.c b/ext/bcmath/libbcmath/src/zero.c
index 550ea97df674..e488069bae17 100644
--- a/ext/bcmath/libbcmath/src/zero.c
+++ b/ext/bcmath/libbcmath/src/zero.c
@@ -45,6 +45,10 @@ bool bc_is_zero_for_scale(bc_num num, size_t scale)
return true;
}
+ if (scale > num->n_scale) {
+ scale = num->n_scale;
+ }
+
/* Initialize */
count = num->n_len + scale;
nptr = num->n_value;
diff --git a/ext/bcmath/tests/bc_is_zero_for_scale_clamp.phpt
b/ext/bcmath/tests/bc_is_zero_for_scale_clamp.phpt
new file mode 100644
index 000000000000..887fabb92916
--- /dev/null
+++ b/ext/bcmath/tests/bc_is_zero_for_scale_clamp.phpt
@@ -0,0 +1,12 @@
+--TEST--
+bc_is_zero_for_scale clamps scale to n_scale (Number::compare opposite signs)
+--EXTENSIONS--
+bcmath
+--FILE--
+<?php
+$shortZero = (new BcMath\Number('1.0'))->sub('1.0');
+$longNegative = new BcMath\Number('-0.' . str_repeat('0', 64) . '1');
+var_dump($shortZero->compare($longNegative, 64));
+?>
+--EXPECT--
+int(0)