Skip to content

Commit 47c8146

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
2 parents f31232e + d70b781 commit 47c8146

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
. Fixed bug GH-16334 (imageaffine overflow on matrix elements).
2323
(David Carlier)
2424

25+
- GMP:
26+
. Fixed floating point exception bug with gmp_pow when using
27+
large exposant values. (David Carlier).
28+
2529
- MBstring:
2630
. Fixed bug GH-16361 (mb_substr overflow on start/length arguments).
2731
(David Carlier)

ext/gmp/gmp.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,12 +1278,37 @@ ZEND_FUNCTION(gmp_pow)
12781278

12791279
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
12801280
INIT_GMP_RETVAL(gmpnum_result);
1281-
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
1281+
if (exp >= INT_MAX) {
1282+
mpz_t base_num, exp_num, mod;
1283+
mpz_init(base_num);
1284+
mpz_init(exp_num);
1285+
mpz_init(mod);
1286+
mpz_set_si(base_num, Z_LVAL_P(base_arg));
1287+
mpz_set_si(exp_num, exp);
1288+
mpz_set_ui(mod, UINT_MAX);
1289+
mpz_powm(gmpnum_result, base_num, exp_num, mod);
1290+
mpz_clear(mod);
1291+
mpz_clear(exp_num);
1292+
mpz_clear(base_num);
1293+
} else {
1294+
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
1295+
}
12821296
} else {
12831297
mpz_ptr gmpnum_base;
12841298
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
12851299
INIT_GMP_RETVAL(gmpnum_result);
1286-
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
1300+
if (exp >= INT_MAX) {
1301+
mpz_t exp_num, mod;
1302+
mpz_init(exp_num);
1303+
mpz_init(mod);
1304+
mpz_set_si(exp_num, exp);
1305+
mpz_set_ui(mod, UINT_MAX);
1306+
mpz_powm(gmpnum_result, gmpnum_base, exp_num, mod);
1307+
mpz_clear(mod);
1308+
mpz_clear(exp_num);
1309+
} else {
1310+
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
1311+
}
12871312
FREE_GMP_TEMP(temp_base);
12881313
}
12891314
}

ext/gmp/tests/gmp_pow_fpe.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
gmp_pow() floating point exception
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
$g = gmp_init(256);
8+
9+
var_dump(gmp_pow($g, PHP_INT_MAX));
10+
var_dump(gmp_pow(256, PHP_INT_MAX));
11+
?>
12+
--EXPECTF--
13+
object(GMP)#2 (1) {
14+
["num"]=>
15+
string(%d) "%s"
16+
}
17+
object(GMP)#2 (1) {
18+
["num"]=>
19+
string(%d) "%s"
20+
}

0 commit comments

Comments
 (0)