php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80732 The sum of two large integers lead to errors when overflow occurs.
Submitted: 2021-02-11 16:30 UTC Modified: 2021-02-11 18:12 UTC
From: 809721719 at qq dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.15 OS: MacOS 10.14 (64bit)
Private report: No CVE-ID: None
 [2021-02-11 16:30 UTC] 809721719 at qq dot com
Description:
------------
The addition of two integers exceeds the maximum value of the integer. After an overflow occurs, the resulting value is distorted.
I test in C/C++/Go and got different results from PHP, but they get the same results.

Test script:
---------------
var_dump(intval(9223372036854775807 + 1));
// int(-9223372036854775808)
var_dump(intval(9223372036854775807 + 2));
// int(-9223372036854775808)
var_dump(intval(9223372036854775807 + 3));
// int(-9223372036854775808)

Expected result:
----------------
int(-9223372036854775808)
int(-9223372036854775807)
int(-9223372036854775806)

Actual result:
--------------
int(-9223372036854775808)
int(-9223372036854775808)
int(-9223372036854775808)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-11 16:57 UTC] 809721719 at qq dot com
-Summary: large integer operating addition error when overflow +Summary: The addition of two large integers lead to errors when overflow occurs.
 [2021-02-11 16:57 UTC] 809721719 at qq dot com
Modification summary
 [2021-02-11 17:15 UTC] 809721719 at qq dot com
-Summary: The addition of two large integers lead to errors when overflow occurs. +Summary: The sum of two large integers lead to errors when overflow occurs.
 [2021-02-11 17:15 UTC] 809721719 at qq dot com
Summary is modified.
 [2021-02-11 18:12 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-02-11 18:12 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

https://www.php.net/manual/en/language.types.integer.php
> If PHP encounters a number beyond the bounds of the int type, it will be
> interpreted as a float instead. Also, an operation which results in a number
> beyond the bounds of the int type will return a float instead.

> When converting from float to int, the number will be rounded towards zero.
>
> If the float is beyond the boundaries of int (usually +/- 2.15e+9 = 2^31 on
> 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is
> undefined, since the float doesn't have enough precision to give an exact int
> result. No warning, not even a notice will be issued when this happens!
 [2021-02-12 11:06 UTC] 809721719 at qq dot com
Thanks for helping!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC