Skip to content

Commit f1d34e5

Browse files
committed
tests with GMP objects instances too
1 parent 19774a7 commit f1d34e5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ext/gmp/gmp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,13 @@ ZEND_FUNCTION(gmp_invert)
15931593
FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a, 1);
15941594
FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a, 2);
15951595

1596+
if (!mpz_cmp_ui(gmpnum_b, 0)) {
1597+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
1598+
FREE_GMP_TEMP(temp_a);
1599+
FREE_GMP_TEMP(temp_b);
1600+
RETURN_THROWS();
1601+
}
1602+
15961603
INIT_GMP_RETVAL(gmpnum_result);
15971604
res = mpz_invert(gmpnum_result, gmpnum_a, gmpnum_b);
15981605
FREE_GMP_TEMP(temp_a);

ext/gmp/tests/gmp_invert.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ try {
1515
echo $e->getMessage() . \PHP_EOL;
1616
}
1717

18+
try {
19+
$zero = new GMP(0);
20+
var_dump(gmp_invert(5, $zero));
21+
} catch (\DivisionByZeroError $e) {
22+
echo $e->getMessage() . \PHP_EOL;
23+
}
24+
1825
var_dump(gmp_strval(gmp_invert(0,28347)));
1926
var_dump(gmp_strval(gmp_invert(-12,456456)));
2027
var_dump(gmp_strval(gmp_invert(234234,-435345)));
@@ -48,6 +55,7 @@ string(7) "2293131"
4855
string(1) "0"
4956
string(4) "5827"
5057
Division by zero
58+
Division by zero
5159
string(1) "0"
5260
string(1) "0"
5361
string(1) "0"

0 commit comments

Comments
 (0)