From 5408f1e5a68fead0057c95df57cfb1ff0cc3fe81 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 30 Dec 2013 14:37:32 +0100 Subject: [PATCH] Throw warning when converting invalid string to GMP --- ext/gmp/gmp.c | 16 ++++++++++++---- ext/gmp/tests/003.phpt | 3 ++- ext/gmp/tests/005.phpt | 2 ++ ext/gmp/tests/006.phpt | 2 ++ ext/gmp/tests/010.phpt | 2 ++ ext/gmp/tests/012.phpt | 2 ++ ext/gmp/tests/013.phpt | 5 +++++ ext/gmp/tests/027.phpt | 4 ++++ ext/gmp/tests/029.phpt | 2 ++ ext/gmp/tests/030.phpt | 2 ++ ext/gmp/tests/031.phpt | 2 ++ ext/gmp/tests/032.phpt | 2 ++ ext/gmp/tests/040.phpt | 6 ++++++ ext/gmp/tests/gmp_nextprime.phpt | 2 ++ 14 files changed, 47 insertions(+), 5 deletions(-) diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index 8835f052468..c02b462bab2 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -718,14 +718,14 @@ static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC) { switch (Z_TYPE_P(val)) { case IS_LONG: - case IS_BOOL: - case IS_CONSTANT: { + case IS_BOOL: { mpz_set_si(gmpnumber, gmp_get_long(val)); return SUCCESS; } case IS_STRING: { char *numstr = Z_STRVAL_P(val); int skip_lead = 0; + int ret; if (Z_STRLEN_P(val) > 2) { if (numstr[0] == '0') { @@ -739,10 +739,18 @@ static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC) } } - return mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), base); + ret = mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), base); + if (-1 == ret) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "Unable to convert variable to GMP - string is not an integer"); + return FAILURE; + } + + return SUCCESS; } default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to convert variable to GMP - wrong type"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "Unable to convert variable to GMP - wrong type"); return FAILURE; } } diff --git a/ext/gmp/tests/003.phpt b/ext/gmp/tests/003.phpt index 379833024d0..5179b73a1b6 100644 --- a/ext/gmp/tests/003.phpt +++ b/ext/gmp/tests/003.phpt @@ -30,7 +30,8 @@ Check for number base recognition printf("%s\n", gmp_strval($test[$i])); } ?> ---EXPECT-- +--EXPECTF-- +Warning: gmp_init(): Unable to convert variable to GMP - string is not an integer in %s on line %d 1234 1234 10011010010 diff --git a/ext/gmp/tests/005.phpt b/ext/gmp/tests/005.phpt index 4ae0cb750a4..79fd73ecf80 100644 --- a/ext/gmp/tests/005.phpt +++ b/ext/gmp/tests/005.phpt @@ -36,6 +36,8 @@ echo "Done\n"; --EXPECTF-- Warning: gmp_strval() expects at least 1 parameter, 0 given in %s on line %d NULL + +Warning: gmp_strval(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) Warning: gmp_strval() expects parameter 2 to be long, string given in %s on line %d diff --git a/ext/gmp/tests/006.phpt b/ext/gmp/tests/006.phpt index 740760631d4..e1d9df67db7 100644 --- a/ext/gmp/tests/006.phpt +++ b/ext/gmp/tests/006.phpt @@ -28,6 +28,8 @@ NULL Warning: gmp_sub() expects exactly 2 parameters, 1 given in %s on line %d NULL + +Warning: gmp_sub(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) Warning: gmp_sub() expects exactly 2 parameters, 3 given in %s on line %d diff --git a/ext/gmp/tests/010.phpt b/ext/gmp/tests/010.phpt index e3f85ec44f1..12e7cad2b32 100644 --- a/ext/gmp/tests/010.phpt +++ b/ext/gmp/tests/010.phpt @@ -27,6 +27,8 @@ NULL Warning: gmp_mod() expects exactly 2 parameters, 1 given in %s on line %d NULL + +Warning: gmp_mod(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) object(GMP)#%d (1) { ["num"]=> diff --git a/ext/gmp/tests/012.phpt b/ext/gmp/tests/012.phpt index 8582ba1fd37..8ca3471cb8c 100644 --- a/ext/gmp/tests/012.phpt +++ b/ext/gmp/tests/012.phpt @@ -28,6 +28,8 @@ int(0) int(-1) int(1) int(1) + +Warning: gmp_neg(): Unable to convert variable to GMP - string is not an integer in %s on line %d int(0) int(0) int(0) diff --git a/ext/gmp/tests/013.phpt b/ext/gmp/tests/013.phpt index 06c2d818d55..bb35891f2a3 100644 --- a/ext/gmp/tests/013.phpt +++ b/ext/gmp/tests/013.phpt @@ -22,6 +22,7 @@ var_dump(gmp_abs(array())); echo "Done\n"; ?> --EXPECTF-- +Warning: gmp_abs(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" string(1) "0" string(1) "0" @@ -31,7 +32,11 @@ string(1) "0" string(21) "111111111111111111111" string(21) "111111111111111111111" string(1) "0" + +Warning: gmp_abs(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" + +Warning: gmp_abs(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" Warning: gmp_abs() expects exactly 1 parameter, 0 given in %s on line %d diff --git a/ext/gmp/tests/027.phpt b/ext/gmp/tests/027.phpt index 71204db98b3..1efdc28c6f4 100644 --- a/ext/gmp/tests/027.phpt +++ b/ext/gmp/tests/027.phpt @@ -25,7 +25,11 @@ int(1) int(0) int(1) int(-1) + +Warning: gmp_sign(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) + +Warning: gmp_init(): Unable to convert variable to GMP - string is not an integer in %s on line %d int(0) Warning: gmp_sign() expects exactly 1 parameter, 2 given in %s on line %d diff --git a/ext/gmp/tests/029.phpt b/ext/gmp/tests/029.phpt index 1b86e47fd97..9bc401031b2 100644 --- a/ext/gmp/tests/029.phpt +++ b/ext/gmp/tests/029.phpt @@ -31,6 +31,8 @@ string(5) "40994" string(3) "515" string(4) "3333" string(4) "4544" + +Warning: gmp_and(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" string(4) "1536" string(15) "424703623692768" diff --git a/ext/gmp/tests/030.phpt b/ext/gmp/tests/030.phpt index 633af41e6fa..035f070bd75 100644 --- a/ext/gmp/tests/030.phpt +++ b/ext/gmp/tests/030.phpt @@ -31,6 +31,8 @@ string(6) "517363" string(10) "2342341163" string(2) "-1" string(3) "-19" + +Warning: gmp_or(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" string(15) "987657876576252" string(21) "987658441719689394144" diff --git a/ext/gmp/tests/031.phpt b/ext/gmp/tests/031.phpt index 90ce1759da6..1e0c1b4694a 100644 --- a/ext/gmp/tests/031.phpt +++ b/ext/gmp/tests/031.phpt @@ -26,6 +26,8 @@ echo "Done\n"; --EXPECTF-- string(2) "-1" string(2) "-1" + +Warning: gmp_com(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" string(14) "-2394876545679" string(3) "110" diff --git a/ext/gmp/tests/032.phpt b/ext/gmp/tests/032.phpt index 327c979da6c..2b0d29a6206 100644 --- a/ext/gmp/tests/032.phpt +++ b/ext/gmp/tests/032.phpt @@ -31,6 +31,8 @@ string(6) "476369" string(10) "2342340648" string(5) "-3334" string(5) "-4563" + +Warning: gmp_xor(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" string(15) "987657876574716" string(21) "987658017016065701376" diff --git a/ext/gmp/tests/040.phpt b/ext/gmp/tests/040.phpt index 9cc497edc6c..29640ba7049 100644 --- a/ext/gmp/tests/040.phpt +++ b/ext/gmp/tests/040.phpt @@ -37,7 +37,13 @@ NULL Warning: gmp_init(): Bad base for conversion: -1 (should be between 2 and %d) in %s on line %d bool(false) + +Warning: gmp_init(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) + +Warning: gmp_init(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) + +Warning: gmp_init(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" Done diff --git a/ext/gmp/tests/gmp_nextprime.phpt b/ext/gmp/tests/gmp_nextprime.phpt index 5683c8c31fd..da89221c0af 100644 --- a/ext/gmp/tests/gmp_nextprime.phpt +++ b/ext/gmp/tests/gmp_nextprime.phpt @@ -34,6 +34,8 @@ string(6) "100003" Warning: gmp_nextprime(): Unable to convert variable to GMP - wrong type in %s on line %d string(1) "0" + +Warning: gmp_nextprime(): Unable to convert variable to GMP - string is not an integer in %s on line %d string(1) "0" Warning: gmp_nextprime(): Unable to convert variable to GMP - wrong type in %s on line %d