summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Peter Banyard <girgias@php.net>2020-12-29 17:50:37 +0100
committerGeorge Peter Banyard <girgias@php.net>2021-01-04 14:47:50 +0100
commit1b5c62facdaaf0bb55fb2ed2b1aa8a1aca81e32b (patch)
tree057ef2702010fd933f0519916e4647617d23c7c7
parentaf7445b9ac3bda9985b5b3843d5165f0bec8ec4b (diff)
downloadphp-git-1b5c62facdaaf0bb55fb2ed2b1aa8a1aca81e32b.tar.gz
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
-rw-r--r--ext/gmp/gmp.c7
-rw-r--r--ext/gmp/tests/comparison_invalid.phpt23
2 files changed, 29 insertions, 1 deletions
diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c
index b7ba99fbdc..0f4b317fd0 100644
--- a/ext/gmp/gmp.c
+++ b/ext/gmp/gmp.c
@@ -429,9 +429,14 @@ 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 */
+ ZEND_ASSERT(Z_TYPE(result) == IS_LONG);
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 0000000000..8c0dfb9f18
--- /dev/null
+++ b/ext/gmp/tests/comparison_invalid.phpt
@@ -0,0 +1,23 @@
+--TEST--
+Invalid comparison with a GMP object
+--SKIPIF--
+<?php if (!extension_loaded("gmp")) print "skip"; ?>
+--FILE--
+<?php
+
+try {
+ var_dump("hapfegfbu" > 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