Skip to content

Commit cfcf5cf

Browse files
committed
Fix GH-16890: array_sum() with GMP can loose precision (LLP64)
We must use `mpz_fits_si_p()` instead of `mpz_fits_slong_p()` since the latter is not suitable for LLP64 data models. libgmp, however, does not define `mpz_fits_si_p()` (which is an mpir addition), so we use `mpz_fits_slong_p()` there which should be fine. Closes GH-16891.
1 parent ac57b81 commit cfcf5cf

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ PHP NEWS
3030
- GD:
3131
. Fixed GH-16776 (imagecreatefromstring overflow). (David Carlier)
3232

33+
- GMP:
34+
. Fixed bug GH-16890 (array_sum() with GMP can loose precision (LLP64)).
35+
(cmb)
36+
3337
- Hash:
3438
. Fixed GH-16711: Segfault in mhash(). (Girgias)
3539

ext/gmp/gmp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
/* Needed for gmp_random() */
3333
#include "ext/random/php_random.h"
3434

35+
#ifndef mpz_fits_si_p
36+
# define mpz_fits_si_p mpz_fits_slong_p
37+
#endif
38+
3539
#define GMP_ROUND_ZERO 0
3640
#define GMP_ROUND_PLUSINF 1
3741
#define GMP_ROUND_MINUSINF 2
@@ -292,7 +296,7 @@ static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int typ
292296
return SUCCESS;
293297
case _IS_NUMBER:
294298
gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
295-
if (mpz_fits_slong_p(gmpnum)) {
299+
if (mpz_fits_si_p(gmpnum)) {
296300
ZVAL_LONG(writeobj, mpz_get_si(gmpnum));
297301
} else {
298302
ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));

ext/gmp/tests/gh16890.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-16890 (array_sum() with GMP can loose precision (LLP64))
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
$large_int_string = (string) (PHP_INT_MAX - 1);
8+
var_dump(array_sum([new GMP($large_int_string), 1]) === PHP_INT_MAX);
9+
?>
10+
--EXPECT--
11+
bool(true)

0 commit comments

Comments
 (0)