Skip to content

Commit 1b5c62f

Browse files
committed
Fix GMP comparison object handler
gmp_cmp() doesn't return false anymore in PHP 8 but will throw an Error if compared to a non numeric string or another type of object. Closes GH-6553
1 parent af7445b commit 1b5c62f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ext/gmp/gmp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,14 @@ static int gmp_compare(zval *op1, zval *op2) /* {{{ */
429429
zval result;
430430

431431
gmp_cmp(&result, op1, op2);
432-
if (Z_TYPE(result) == IS_FALSE) {
432+
433+
/* An error/exception occurs if one of the operands is not a numeric string
434+
* or an object which is different from GMP */
435+
if (EG(exception)) {
433436
return 1;
434437
}
438+
/* result can only be a zend_long if gmp_cmp hasn't thrown an Error */
439+
ZEND_ASSERT(Z_TYPE(result) == IS_LONG);
435440
return Z_LVAL(result);
436441
}
437442
/* }}} */

ext/gmp/tests/comparison_invalid.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Invalid comparison with a GMP object
3+
--SKIPIF--
4+
<?php if (!extension_loaded("gmp")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
try {
9+
var_dump("hapfegfbu" > gmp_init(0));
10+
} catch (\Error $e) {
11+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
12+
}
13+
14+
try {
15+
var_dump((new DateTime()) > gmp_init(0));
16+
} catch (\Error $e) {
17+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
18+
}
19+
20+
?>
21+
--EXPECT--
22+
TypeError: main(): Argument #2 is not an integer string
23+
TypeError: main(): Argument #2 must be of type GMP|string|int, DateTime given

0 commit comments

Comments
 (0)