Skip to content

Commit

Permalink
ext/gmp: gmp_invert addressing todo. (#13654)
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen authored Mar 9, 2024
1 parent 3c74f4a commit bb1ef4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1585,11 +1585,15 @@ ZEND_FUNCTION(gmp_invert)
RETURN_THROWS();
}

if (Z_TYPE_P(b_arg) == IS_LONG && Z_LVAL_P(b_arg) == 0) {
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
RETURN_THROWS();
}

FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a, 1);
FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a, 2);

// TODO Early check if b_arg IS_LONG?
if (0 == mpz_cmp_ui(gmpnum_b, 0)) {
if (!mpz_cmp_ui(gmpnum_b, 0)) {
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
FREE_GMP_TEMP(temp_a);
FREE_GMP_TEMP(temp_b);
Expand Down
8 changes: 8 additions & 0 deletions ext/gmp/tests/gmp_invert.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ try {
echo $e->getMessage() . \PHP_EOL;
}

try {
$zero = new GMP(0);
var_dump(gmp_invert(5, $zero));
} catch (\DivisionByZeroError $e) {
echo $e->getMessage() . \PHP_EOL;
}

var_dump(gmp_strval(gmp_invert(0,28347)));
var_dump(gmp_strval(gmp_invert(-12,456456)));
var_dump(gmp_strval(gmp_invert(234234,-435345)));
Expand Down Expand Up @@ -48,6 +55,7 @@ string(7) "2293131"
string(1) "0"
string(4) "5827"
Division by zero
Division by zero
string(1) "0"
string(1) "0"
string(1) "0"
Expand Down

0 comments on commit bb1ef4f

Please sign in to comment.