@@ -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}
0 commit comments