Skip to content

Commit

Permalink
Throw ValueError instead of TypeError for malformed GMP number
Browse files Browse the repository at this point in the history
If the passed argument has correct type (string) but does not
have a well-formed value, throw ValueError instead of TypeError.

Closes GH-6572.
  • Loading branch information
nikic committed Jan 4, 2021
1 parent abe23bd commit 65f14b0
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ static zend_result convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base, ui
"Cannot convert variable to GMP, it is not an integer string");
return FAILURE;
}
zend_argument_type_error(arg_pos, "is not an integer string");
zend_argument_value_error(arg_pos, "is not an integer string");
return FAILURE;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Check for number base recognition
$test[] = gmp_init("0x4d2", 16);
try {
$test[] = gmp_init("4d2");
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
$test[] = gmp_init("4d2", 16);
Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/bug66872.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Bug #66872: Crash when passing string to gmp_testbit

try {
var_dump(gmp_testbit("abc", 1));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
72 changes: 36 additions & 36 deletions ext/gmp/tests/bug80560.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ $functions1 = [
'gmp_fact',
'gmp_sqrt',
'gmp_sqrtrem',
'gmp_root',
'gmp_rootrem',
'gmp_pow',
'gmp_perfect_square',
'gmp_perfect_power',
'gmp_prob_prime',
'gmp_sign',
'gmp_random_seed',
'gmp_popcount',
'gmp_com',
'gmp_nextprime',
];
$functions1_need_int_2 = [
'gmp_testbit',
'gmp_scan0',
'gmp_scan1',
'gmp_binomial',
'gmp_root',
'gmp_rootrem',
'gmp_pow',
];
$functions2 = [
'gmp_add',
Expand All @@ -55,7 +56,6 @@ $functions2 = [
'gmp_or',
'gmp_xor',
'gmp_hamdist',
'gmp_nextprime',
];
$functions3 = [
'gmp_powm',
Expand All @@ -65,24 +65,24 @@ echo 'Explicit base with gmp_init:', \PHP_EOL;
echo 'Hexadecimal', \PHP_EOL;
try {
var_dump(gmp_init('0X', 16));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
var_dump(gmp_init('0x', 16));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

echo 'Binary', \PHP_EOL;
try {
var_dump(gmp_init('0B', 2));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
var_dump(gmp_init('0b', 2));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage(), \PHP_EOL;
}

Expand All @@ -91,121 +91,121 @@ foreach ($functions1 as $function) {
try {
$function('0B');
echo $function, ' failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0b');
echo $function, ' failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0X');
echo $function, ' failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0x');
echo $function, ' failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
}
foreach ($functions1_need_int_2 as $function) {
try {
$function('0B', 1);
echo $function, ' failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0b', 1);
echo $function, ' failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0X', 1);
echo $function, ' failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0x', 1);
echo $function, ' failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
}
foreach ($functions2 as $function) {
try {
$function('0B', 1);
echo $function, ' arg 1 failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0b', 1);
echo $function, ' arg 1 failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0X', 1);
echo $function, ' arg 1 failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0x', 1);
echo $function, ' arg 1 failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0B');
echo $function, ' arg 2 failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0b');
echo $function, ' arg 2 failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0X');
echo $function, ' arg 2 failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0x');
echo $function, ' arg 2 failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
}
foreach ($functions3 as $function) {
try {
$function('0B', 1, 1);
echo $function, ' arg 1 failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0b', 1, 1);
echo $function, ' arg 1 failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0X', 1, 1);
echo $function, ' arg 1 failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function('0x', 1, 1);
echo $function, ' arg 1 failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0B', 1);
echo $function, ' arg 2 failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0b', 1);
echo $function, ' arg 2 failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0X', 1);
echo $function, ' arg 2 failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, '0x', 1);
echo $function, ' arg 2 failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, 1, '0B');
echo $function, ' arg 3 failed with 0B', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, 1, '0b');
echo $function, ' arg 3 failed with 0b', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, 1, '0X');
echo $function, ' arg 3 failed with 0X', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
try {
$function(1, 1, '0x');
echo $function, ' arg 3 failed with 0x', \PHP_EOL;
} catch (\TypeError) { }
} catch (\ValueError) { }
}

echo "Done\n";
Expand Down
6 changes: 3 additions & 3 deletions ext/gmp/tests/gmp_abs.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gmp_abs() basic tests

try {
var_dump(gmp_strval(gmp_abs("")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump(gmp_strval(gmp_abs("0")));
Expand All @@ -24,13 +24,13 @@ var_dump(gmp_strval(gmp_abs("0000")));
try {
// Base 8
var_dump(gmp_strval(gmp_abs("09876543")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
// Base 8
var_dump(gmp_strval(gmp_abs("-099987654")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_and.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var_dump(gmp_strval(gmp_and(4545, -20)));

try {
var_dump(gmp_strval(gmp_and("test", "no test")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_com.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var_dump(gmp_strval(gmp_com(0)));
var_dump(gmp_strval(gmp_com("0")));
try {
var_dump(gmp_strval(gmp_com("test")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump(gmp_strval(gmp_com("2394876545678")));
Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_fact.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gmp_fact() basic tests
var_dump(gmp_strval(gmp_fact(0)));
try {
var_dump(gmp_strval(gmp_fact("")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump(gmp_strval(gmp_fact("0")));
Expand Down
6 changes: 3 additions & 3 deletions ext/gmp/tests/gmp_init.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ try {

try {
var_dump(gmp_init("",36));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(gmp_init("foo",3));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
var_dump(gmp_strval(gmp_init("993247326237679187178",3)));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/gmp/tests/gmp_intval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var_dump(gmp_intval($g));

try {
var_dump(gmp_intval(""));
} catch (TypeError $e) {
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
Expand All @@ -31,7 +31,7 @@ try {
}
try {
var_dump(gmp_intval("1.0001"));
} catch (TypeError $e) {
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_mod.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gmp_mod tests()

try {
var_dump(gmp_mod("",""));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump(gmp_mod(0,1));
Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_neg.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var_dump(gmp_intval(gmp_neg("-1")));

try {
var_dump(gmp_intval(gmp_neg("")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_nextprime.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ try {
try {
$n = gmp_nextprime("");
var_dump(gmp_strval($n));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_or.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var_dump(gmp_strval(gmp_or(4545, -20)));

try {
var_dump(gmp_strval(gmp_or("test", "no test")));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gmp/tests/gmp_random_seed.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var_dump(gmp_strval(gmp_random_range(-10000, 0)));
// standard non conversion error
try {
var_dump(gmp_random_seed('not a number'));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/gmp/tests/gmp_sign.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ var_dump(gmp_sign("-34535345345"));

try {
var_dump(gmp_sign("+34534573457345"));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
$n = gmp_init("098909878976786545");
var_dump(gmp_sign($n));
} catch (\TypeError $e) {
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
Expand Down

0 comments on commit 65f14b0

Please sign in to comment.