Skip to content

Commit 79c71c9

Browse files
committed
Merge branch 'PHP-8.4'
2 parents 8475d5f + 61d34b3 commit 79c71c9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

ext/gmp/gmp.c

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

12891289
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
12901290
INIT_GMP_RETVAL(gmpnum_result);
1291-
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
1291+
if (exp >= INT_MAX) {
1292+
mpz_t base_num, exp_num, mod;
1293+
mpz_init(base_num);
1294+
mpz_init(exp_num);
1295+
mpz_init(mod);
1296+
mpz_set_si(base_num, Z_LVAL_P(base_arg));
1297+
mpz_set_si(exp_num, exp);
1298+
mpz_set_ui(mod, UINT_MAX);
1299+
mpz_powm(gmpnum_result, base_num, exp_num, mod);
1300+
mpz_clear(mod);
1301+
mpz_clear(exp_num);
1302+
mpz_clear(base_num);
1303+
} else {
1304+
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
1305+
}
12921306
} else {
12931307
mpz_ptr gmpnum_base;
12941308
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
12951309
INIT_GMP_RETVAL(gmpnum_result);
1296-
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
1310+
if (exp >= INT_MAX) {
1311+
mpz_t exp_num, mod;
1312+
mpz_init(exp_num);
1313+
mpz_init(mod);
1314+
mpz_set_si(exp_num, exp);
1315+
mpz_set_ui(mod, UINT_MAX);
1316+
mpz_powm(gmpnum_result, gmpnum_base, exp_num, mod);
1317+
mpz_clear(mod);
1318+
mpz_clear(exp_num);
1319+
} else {
1320+
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
1321+
}
12971322
FREE_GMP_TEMP(temp_base);
12981323
}
12991324
}

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)