php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79955 converting large floats to int leads to overflow or the arithmetical unit
Submitted: 2020-08-11 20:20 UTC Modified: 2020-08-11 20:35 UTC
From: peter dot fargas at informatik-handwerk dot de Assigned: cmb (profile)
Status: Not a bug Package: Math related
PHP Version: 8.0.0beta1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: peter dot fargas at informatik-handwerk dot de
New email:
PHP Version: OS:

 

 [2020-08-11 20:20 UTC] peter dot fargas at informatik-handwerk dot de
Description:
------------
(int](PHP_MAX_INT + 1) returns \PHP_INT_MIN.
Increasing the argument passed, zero result can be achieved.

Same goes for the \intval() function.

This happens for all current PHP versions - reported by https://3v4l.org/iDnt4

7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.9
8.0.0alpha1 - beta1



Test script:
---------------
<?php

echo '---- INTMAX vs INTMAX+1'. \PHP_EOL;
var_dump(\PHP_INT_MAX);
var_dump(\PHP_INT_MAX+1);
echo '---- INTMAX to float and back'. \PHP_EOL;
var_dump(\intval((float)\PHP_INT_MAX));
var_dump((int)((float)\PHP_INT_MAX));
echo '---- INTMAX+1 to float and back'. \PHP_EOL;
var_dump(\intval(\PHP_INT_MAX+1));
var_dump((int)(\PHP_INT_MAX+1));
var_dump(\PHP_INT_MIN);
echo '---- ...rendezvous point is...'. \PHP_EOL;
var_dump(\intval(18446744073709551615));

Expected result:
----------------
one of:
- \PHP_MAX_INT (just as the documentation states), a sign stable result
- type error
- zero

Actual result:
--------------
overflow or the arithmetical unit


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-11 20:35 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-08-11 20:35 UTC] cmb@php.net
From the docs[1]:

| If PHP encounters a number beyond the bounds of the integer
| type, it will be interpreted as a float instead. Also, an
| operation which results in a number beyond the bounds of the
| integer type will return a float instead.

and (float to int conversion):

| If the float is beyond the boundaries of integer (usually +/-
| 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on
| 64-bit platforms other than Windows prior to PHP 7), the result is
| undefined, since the float doesn't have enough precision to give
| an exact integer result. No warning, not even a notice will be
| issued when this happens!

[1] <https://www.php.net/manual/en/language.types.integer.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC