|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-04-11 16:20 UTC] vedad at kajtaz dot net
Description:
------------
The long2ip() signature in documentation is:
string long2ip ( string $proper_address )
Yet, as of PHP 7.1 (unlike PHP 7.0) TypeError is thrown with strict_types=1 when a string is provided:
PHP Fatal error: Uncaught TypeError: long2ip() expects parameter 1 to be integer, string given in ...
Related bug reports: #65017 and #71100
Test script:
---------------
declare(strict_types=1);
long2ip('2130706433');
Actual result:
--------------
PHP Fatal error: Uncaught TypeError: long2ip() expects parameter 1 to be integer, string given in ...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 19 23:00:01 2025 UTC |
The problem is that IP addresses in the high range (e.g. "192.168.0.1") result in a positive number which does not fit in an INT32_MAX. I don't see how this can be solved without added unsigned support to PHP, so I think the commit changing the argument from a string to an integer should be reverted. Test code: <?php declare(strict_types=0); error_reporting(E_ALL); $n = (int)"3232242954"; // Will be truncated to INT32_MAX echo "192.168.29.10 = " . gettype($n) . " = $n\n"; echo "Test 1: '" . long2ip($n) . "'\n"; echo "Test 2: '" . long2ip("3232242954") . "'\n"; echo "Test 3: '" . long2ip("173632452") . "'\n"; /* Result: 192.168.29.10 = integer = 2147483647 Test 1: '127.255.255.255' PHP Warning: long2ip() expects parameter 1 to be integer, string given in t.php on line 7 Warning: long2ip() expects parameter 1 to be integer, string given in t.php on line 7 Test 2: '' Test 3: '10.89.107.196' */They changed the signature of long2ip(), but didn't update the documentation. This applies to all platforms. The original bug report proposes to fix the documentation to match the implementation. I propose to revert the change and keep the documentation as it is, because the change breaks old code on 32-bit PHP. However confusing the function name is, it always has been like this. If the change is not reverted, please also update the migration guide and add this backward incompatible change to the list. Work around for 32-bit PHP code: function string2ip($s) { if ($s > PHP_INT_MAX) $s = 2 * PHP_INT_MIN + $s; return long2ip($s); }I don't have 32bit env , just one question what does ip2long behavior in this case? ie: var_dump(ip2long("192.168.0.1"));I got one 32bit box, and : var_dump(ip2long("192.168.29.10")); output: int(-1062724342) and : var_dump(long2ip((-1062724342))); output the correct value.. so in my opinion I don't think we should revert the fix here.