From a142f10aa113c67cd478b3ef054963c84480a7a7 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 14 Apr 2025 11:19:14 +0200 Subject: [PATCH] Don't evaluate GMP comparison multiple times (#18321) 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 80c767181d5..8cf20c90fc7 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)); } /* }}} */