Skip to content

Commit 9ee204f

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
2 parents 824c816 + aa38bbe commit 9ee204f

File tree

5 files changed

+119
-35
lines changed

5 files changed

+119
-35
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ PHP NEWS
8484
. Fixed bug GH-16411 (gmp_export() can cause overflow). (cmb)
8585
. Fixed bug GH-16501 (gmp_random_bits() can cause overflow).
8686
(David Carlier)
87+
. Fixed gmp_pow() overflow bug with large base/exponents.
88+
(David Carlier)
8789

8890
- MBstring:
8991
. Fixed bug GH-16361 (mb_substr overflow on start/length arguments).

ext/gmp/gmp.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,39 +1292,27 @@ ZEND_FUNCTION(gmp_pow)
12921292
RETURN_THROWS();
12931293
}
12941294

1295+
double powmax = log((double)ZEND_LONG_MAX);
1296+
12951297
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
12961298
INIT_GMP_RETVAL(gmpnum_result);
1297-
if (exp >= INT_MAX) {
1298-
mpz_t base_num, exp_num, mod;
1299-
mpz_init(base_num);
1300-
mpz_init(exp_num);
1301-
mpz_init(mod);
1302-
mpz_set_si(base_num, Z_LVAL_P(base_arg));
1303-
mpz_set_si(exp_num, exp);
1304-
mpz_set_ui(mod, UINT_MAX);
1305-
mpz_powm(gmpnum_result, base_num, exp_num, mod);
1306-
mpz_clear(mod);
1307-
mpz_clear(exp_num);
1308-
mpz_clear(base_num);
1309-
} else {
1310-
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
1299+
if ((log(Z_LVAL_P(base_arg)) * exp) > powmax) {
1300+
zend_value_error("base and exponent overflow");
1301+
RETURN_THROWS();
13111302
}
1303+
mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
13121304
} else {
13131305
mpz_ptr gmpnum_base;
1306+
zend_ulong gmpnum;
13141307
FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base, 1);
13151308
INIT_GMP_RETVAL(gmpnum_result);
1316-
if (exp >= INT_MAX) {
1317-
mpz_t exp_num, mod;
1318-
mpz_init(exp_num);
1319-
mpz_init(mod);
1320-
mpz_set_si(exp_num, exp);
1321-
mpz_set_ui(mod, UINT_MAX);
1322-
mpz_powm(gmpnum_result, gmpnum_base, exp_num, mod);
1323-
mpz_clear(mod);
1324-
mpz_clear(exp_num);
1325-
} else {
1326-
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
1309+
gmpnum = mpz_get_ui(gmpnum_base);
1310+
if ((log(gmpnum) * exp) > powmax) {
1311+
FREE_GMP_TEMP(temp_base);
1312+
zend_value_error("base and exponent overflow");
1313+
RETURN_THROWS();
13271314
}
1315+
mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
13281316
FREE_GMP_TEMP(temp_base);
13291317
}
13301318
}

ext/gmp/tests/gmp_pow.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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"); ?>
57
--FILE--
68
<?php
79

ext/gmp/tests/gmp_pow_32bits.phpt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--TEST--
2+
gmp_pow() basic tests
3+
--EXTENSIONS--
4+
gmp
5+
--SKIPIF--
6+
<?php if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); ?>
7+
--FILE--
8+
<?php
9+
10+
var_dump(gmp_strval(gmp_pow(2,10)));
11+
var_dump(gmp_strval(gmp_pow(-2,10)));
12+
var_dump(gmp_strval(gmp_pow(-2,11)));
13+
var_dump(gmp_strval(gmp_pow("2",10)));
14+
var_dump(gmp_strval(gmp_pow("2",0)));
15+
try {
16+
gmp_pow("2", -1);
17+
} catch (ValueError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
20+
var_dump(gmp_strval(gmp_pow("-2",10)));
21+
try {
22+
gmp_pow(20,10);
23+
} catch (ValueError $exception) {
24+
echo $exception->getMessage() . "\n";
25+
}
26+
try {
27+
gmp_pow(50,10);
28+
} catch (ValueError $exception) {
29+
echo $exception->getMessage() . "\n";
30+
}
31+
try {
32+
gmp_pow(50,-5);
33+
} catch (ValueError $exception) {
34+
echo $exception->getMessage() . "\n";
35+
}
36+
try {
37+
$n = gmp_init("20");
38+
gmp_pow($n,10);
39+
} catch (ValueError $exception) {
40+
echo $exception->getMessage() . "\n";
41+
}
42+
try {
43+
$n = gmp_init("-20");
44+
gmp_pow($n,10);
45+
} catch (ValueError $exception) {
46+
echo $exception->getMessage() . "\n";
47+
}
48+
try {
49+
var_dump(gmp_pow(2,array()));
50+
} catch (TypeError $e) {
51+
echo $e->getMessage(), "\n";
52+
}
53+
54+
try {
55+
var_dump(gmp_pow(array(),10));
56+
} catch (\TypeError $e) {
57+
echo $e->getMessage() . \PHP_EOL;
58+
}
59+
60+
echo "Done\n";
61+
?>
62+
--EXPECT--
63+
string(4) "1024"
64+
string(4) "1024"
65+
string(5) "-2048"
66+
string(4) "1024"
67+
string(1) "1"
68+
gmp_pow(): Argument #2 ($exponent) must be greater than or equal to 0
69+
string(4) "1024"
70+
base and exponent overflow
71+
base and exponent overflow
72+
gmp_pow(): Argument #2 ($exponent) must be greater than or equal to 0
73+
base and exponent overflow
74+
base and exponent overflow
75+
gmp_pow(): Argument #2 ($exponent) must be of type int, array given
76+
gmp_pow(): Argument #1 ($num) must be of type GMP|string|int, array given
77+
Done

ext/gmp/tests/gmp_pow_fpe.phpt

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

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"
9+
try {
10+
gmp_pow($g, PHP_INT_MAX);
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage() . PHP_EOL;
1613
}
17-
object(GMP)#2 (1) {
18-
["num"]=>
19-
string(%d) "%s"
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;
2024
}
25+
try {
26+
gmp_pow(gmp_init(PHP_INT_MAX), 256);
27+
} catch (\ValueError $e) {
28+
echo $e->getMessage();
29+
}
30+
?>
31+
--EXPECTF--
32+
base and exponent overflow
33+
base and exponent overflow
34+
base and exponent overflow
35+
base and exponent overflow

0 commit comments

Comments
 (0)