Skip to content

Commit 45140e5

Browse files
committed
Revert "ext/gmp: gmp_pow fix FPE with large values."
This reverts commit e0a0e21.
1 parent 97b0318 commit 45140e5

File tree

5 files changed

+35
-119
lines changed

5 files changed

+35
-119
lines changed

NEWS

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ PHP NEWS
134134
. Fixed bug GH-16411 (gmp_export() can cause overflow). (cmb)
135135
. Fixed bug GH-16501 (gmp_random_bits() can cause overflow).
136136
(David Carlier)
137-
. Fixed gmp_pow() overflow bug with large base/exponents.
138-
(David Carlier)
139137
. Fixed segfaults and other issues related to operator overloading with
140138
GMP objects. (Girgias)
141139

ext/gmp/gmp.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,27 +1350,39 @@ ZEND_FUNCTION(gmp_pow)
13501350
RETURN_THROWS();
13511351
}
13521352

1353-
double powmax = log((double)ZEND_LONG_MAX);
1354-
13551353
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
13561354
INIT_GMP_RETVAL(gmpnum_result);
1357-
if ((log(Z_LVAL_P(base_arg)) * exp) > powmax) {
1358-
zend_value_error("base and exponent overflow");
1359-
RETURN_THROWS();
1355+
if (exp >= INT_MAX) {
1356+
mpz_t base_num, exp_num, mod;
1357+
mpz_init(base_num);
1358+
mpz_init(exp_num);
1359+
mpz_init(mod);
1360+
mpz_set_si(base_num, Z_LVAL_P(base_arg));
1361+
mpz_set_si(exp_num, exp);
1362+
mpz_set_ui(mod, UINT_MAX);
1363+
mpz_powm(gmpnum_result, base_num, exp_num, mod);
1364+
mpz_clear(mod);
1365+
mpz_clear(exp_num);
1366+
mpz_clear(base_num);
1367+
} else {
1368+
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
13601369
}
1361-
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
13621370
} else {
13631371
mpz_ptr gmpnum_base;
1364-
zend_ulong gmpnum;
13651372
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
13661373
INIT_GMP_RETVAL(gmpnum_result);
1367-
gmpnum = mpz_get_ui(gmpnum_base);
1368-
if ((log(gmpnum) * exp) > powmax) {
1369-
FREE_GMP_TEMP(temp_base);
1370-
zend_value_error("base and exponent overflow");
1371-
RETURN_THROWS();
1374+
if (exp >= INT_MAX) {
1375+
mpz_t exp_num, mod;
1376+
mpz_init(exp_num);
1377+
mpz_init(mod);
1378+
mpz_set_si(exp_num, exp);
1379+
mpz_set_ui(mod, UINT_MAX);
1380+
mpz_powm(gmpnum_result, gmpnum_base, exp_num, mod);
1381+
mpz_clear(mod);
1382+
mpz_clear(exp_num);
1383+
} else {
1384+
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
13721385
}
1373-
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
13741386
FREE_GMP_TEMP(temp_base);
13751387
}
13761388
}

ext/gmp/tests/gmp_pow.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
gmp_pow() basic tests
33
--EXTENSIONS--
44
gmp
5-
--SKIPIF--
6-
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
75
--FILE--
86
<?php
97

ext/gmp/tests/gmp_pow_32bits.phpt

Lines changed: 0 additions & 77 deletions
This file was deleted.

ext/gmp/tests/gmp_pow_fpe.phpt

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,15 @@ gmp
66
<?php
77
$g = gmp_init(256);
88

9-
try {
10-
gmp_pow($g, PHP_INT_MAX);
11-
} catch (\ValueError $e) {
12-
echo $e->getMessage() . PHP_EOL;
13-
}
14-
try {
15-
gmp_pow(256, PHP_INT_MAX);
16-
} catch (\ValueError $e) {
17-
echo $e->getMessage() . PHP_EOL;
18-
}
19-
20-
try {
21-
gmp_pow(gmp_add(gmp_mul(gmp_init(PHP_INT_MAX), gmp_init(PHP_INT_MAX)), 3), 256);
22-
} catch (\ValueError $e) {
23-
echo $e->getMessage() . PHP_EOL;
24-
}
25-
try {
26-
gmp_pow(gmp_init(PHP_INT_MAX), 256);
27-
} catch (\ValueError $e) {
28-
echo $e->getMessage();
29-
}
9+
var_dump(gmp_pow($g, PHP_INT_MAX));
10+
var_dump(gmp_pow(256, PHP_INT_MAX));
3011
?>
3112
--EXPECTF--
32-
base and exponent overflow
33-
base and exponent overflow
34-
base and exponent overflow
35-
base and exponent overflow
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)