php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79843 Running intval to string value that is supposed to be INF returns 0
Submitted: 2020-07-13 09:46 UTC Modified: 2020-07-15 09:46 UTC
From: taira dot 815 dot terashima at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Strings related
PHP Version: 7.4.8 OS: Ubuntu18.04
Private report: No CVE-ID: None
 [2020-07-13 09:46 UTC] taira dot 815 dot terashima at gmail dot com
Description:
------------
Running function intval using a string value more than a certain number (maybe DOUBLE MAX) returns 0.

Test script:
---------------
<?php
// It returns 0. (This string value is 2^1024)

$number = intval('179769313486231570772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216');

echo $number;

Expected result:
----------------
INF

Actual result:
--------------
0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-13 09:59 UTC] cmb@php.net
For numbers exceeding PHP_INT_MAX, intval() is supposed to return
PHP_INT_MAX; see <https://3v4l.org/tf7EJ>.
 [2020-07-15 01:13 UTC] taira dot 815 dot terashima at gmail dot com
Oh, I see.
Sorry, One digit missing.
If Add 0 to number's last, result were 0.
 [2020-07-15 01:14 UTC] taira dot 815 dot terashima at gmail dot com
See this.
<https://3v4l.org/1Pi3j>
 [2020-07-15 07:46 UTC] nikic@php.net
@cmb: float -> int conversion does not perform a saturating cast, it performs a truncating cast. A good way to think of it is that we first cast the float to a bigint and then cast the bigint to a smaller integer size. As usual with integer casts, this will simply take the low bits and discard the high bits. The same happens with float -> int casts, just with more thorny details ;)
 [2020-07-15 09:46 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-07-15 09:46 UTC] cmb@php.net
Thanks for the explanation, @nikic.

Anyhow, the PHP manual says[1]:

| 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), 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#language.types.integer.casting.from-float>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC