From 4430e73972c2ac9db0ef166a614f72a857d459b8 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 13 Apr 2025 23:10:06 +0200 Subject: [PATCH] Don't evaluate GMP comparison multiple times ZEND_THREEWAY_COMPARE evaluates its operands multiple times. --- ext/gmp/gmp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index 80c767181d50e..8cf20c90fc7a2 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -480,7 +480,8 @@ static int gmp_compare(zval *op1, zval *op2) /* {{{ */ return ZEND_UNCOMPARABLE; } - return ZEND_THREEWAY_COMPARE(mpz_cmp(gmp_op1, gmp_op2), 0); + int ret = mpz_cmp(gmp_op1, gmp_op2); /* avoid multiple evaluations */ + return ZEND_THREEWAY_COMPARE(ret, 0); } /* }}} */ @@ -1422,7 +1423,8 @@ ZEND_FUNCTION(gmp_cmp) GMP_Z_PARAM_INTO_MPZ_PTR(gmpnum_b) ZEND_PARSE_PARAMETERS_END(); - RETURN_LONG(ZEND_THREEWAY_COMPARE(mpz_cmp(gmpnum_a, gmpnum_b), 0)); + int ret = mpz_cmp(gmpnum_a, gmpnum_b); /* avoid multiple evaluations */ + RETURN_LONG(ZEND_THREEWAY_COMPARE(ret, 0)); } /* }}} */