@@ -1288,12 +1288,37 @@ ZEND_FUNCTION(gmp_pow)
1288
1288
1289
1289
if (Z_TYPE_P (base_arg ) == IS_LONG && Z_LVAL_P (base_arg ) >= 0 ) {
1290
1290
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
+ }
1292
1306
} else {
1293
1307
mpz_ptr gmpnum_base ;
1294
1308
FETCH_GMP_ZVAL (gmpnum_base , base_arg , temp_base , 1 );
1295
1309
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
+ }
1297
1322
FREE_GMP_TEMP (temp_base );
1298
1323
}
1299
1324
}
0 commit comments