diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index 5bccdeca5c357..ec445f387fd4f 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -429,9 +429,13 @@ static int gmp_compare(zval *op1, zval *op2) /* {{{ */ zval result; gmp_cmp(&result, op1, op2); - if (Z_TYPE(result) == IS_FALSE) { + + /* An error/exception occurs if one of the operands is not a numeric string + * or an object which is different from GMP */ + if (EG(exception)) { return 1; } + /* result can only be a zend_long if gmp_cmp hasn't thrown an Error */ return Z_LVAL(result); } /* }}} */ diff --git a/ext/gmp/tests/comparison_invalid.phpt b/ext/gmp/tests/comparison_invalid.phpt new file mode 100644 index 0000000000000..8c0dfb9f187e4 --- /dev/null +++ b/ext/gmp/tests/comparison_invalid.phpt @@ -0,0 +1,23 @@ +--TEST-- +Invalid comparison with a GMP object +--SKIPIF-- + +--FILE-- + gmp_init(0)); +} catch (\Error $e) { + echo $e::class, ': ', $e->getMessage(), \PHP_EOL; +} + +try { + var_dump((new DateTime()) > gmp_init(0)); +} catch (\Error $e) { + echo $e::class, ': ', $e->getMessage(), \PHP_EOL; +} + +?> +--EXPECT-- +TypeError: main(): Argument #2 is not an integer string +TypeError: main(): Argument #2 must be of type GMP|string|int, DateTime given