Skip to content

ext/gmp: gmp_invert addressing todo. #13654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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