Skip to content

Commit a380dca

Browse files
committed
Fix GH-20006: Power of 0 of BcMath number causes UB
Closes GH-20007.
1 parent 4ab1324 commit a380dca

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP NEWS
55
- Core:
66
. Fix OSS-Fuzz #447521098 (Fatal error during sccp shift eval). (ilutov)
77

8+
- BcMath:
9+
. Fixed bug GH-20006 (Power of 0 of BcMath number causes UB). (nielsdos)
10+
811
- Opcache:
912
. Fixed segfault in function JIT due to NAN to bool warning. (Girgias)
1013

ext/bcmath/libbcmath/src/raise.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ bc_raise_status bc_raise(bc_num base, long exponent, bc_num *result, size_t scal
193193

194194
if (bc_is_zero(base)) {
195195
/* If the exponent is negative, it divides by 0 */
196-
return is_neg ? BC_RAISE_STATUS_DIVIDE_BY_ZERO : BC_RAISE_STATUS_OK;
196+
if (is_neg) {
197+
return BC_RAISE_STATUS_DIVIDE_BY_ZERO;
198+
}
199+
bc_free_num (result);
200+
*result = bc_copy_num(BCG(_zero_));
201+
return BC_RAISE_STATUS_OK;
197202
}
198203

199204
/* check overflow */

ext/bcmath/tests/gh20006.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-20006 (Power of 0 of BcMath number causes crash)
3+
--EXTENSIONS--
4+
bcmath
5+
--FILE--
6+
<?php
7+
$n = new BcMath\Number("0");
8+
var_dump(pow($n, 2));
9+
?>
10+
--EXPECTF--
11+
object(BcMath\Number)#%d (2) {
12+
["value"]=>
13+
string(1) "0"
14+
["scale"]=>
15+
int(0)
16+
}

0 commit comments

Comments
 (0)