Skip to content

Fix GH-16890: array_sum() with GMP can loose precision (LLP64) #16891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
/* Needed for gmp_random() */
#include "ext/random/php_random.h"

#ifndef mpz_fits_si_p
# define mpz_fits_si_p mpz_fits_slong_p
#endif

#define GMP_ROUND_ZERO 0
#define GMP_ROUND_PLUSINF 1
#define GMP_ROUND_MINUSINF 2
Expand Down Expand Up @@ -292,7 +296,7 @@ static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int typ
return SUCCESS;
case _IS_NUMBER:
gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
if (mpz_fits_slong_p(gmpnum)) {
if (mpz_fits_si_p(gmpnum)) {
ZVAL_LONG(writeobj, mpz_get_si(gmpnum));
} else {
ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
Expand Down
11 changes: 11 additions & 0 deletions ext/gmp/tests/gh16890.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
GH-16890 (array_sum() with GMP can loose precision (LLP64))
--EXTENSIONS--
gmp
--FILE--
<?php
$large_int_string = (string) (PHP_INT_MAX - 1);
var_dump(array_sum([new GMP($large_int_string), 1]) === PHP_INT_MAX);
?>
--EXPECT--
bool(true)
Loading