php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29737 ip2long() works not as documented
Submitted: 2004-08-18 16:55 UTC Modified: 2005-04-07 18:02 UTC
From: belikoviv at is dot lg dot ua Assigned:
Status: Closed Package: Network related
PHP Version: 5.0.0 OS: Windows 2000 SP4; Fedora Core 2
Private report: No CVE-ID:
 [2004-08-18 16:55 UTC] belikoviv at is dot lg dot ua
Description:
------------
Documentation (from 16-08-2004) says:
"Note: ip2long() will return -1 for the IP 255.255.255.255"
Instead it returns FALSE.


Reproduce code:
---------------
#!/usr/local/bin/php
<?php
$ip = "255.255.255.255";
$long = ip2long ( $ip );//  if ( $long === FALSE ) $long = -1;
echo $ip, "\t", $long, "\t", long2ip ( $long ), "\n";
?>


Expected result:
----------------
255.255.255.255 -1 255.255.255.255


Actual result:
--------------
255.255.255.255  0.0.0.0


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-19 07:57 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2004-08-19 10:47 UTC] belikoviv at is dot lg dot ua
I think that problem not in documentation, but in function itself.

Address 255.255.255.255 is _valid_ address, and function must return 0xffffffff (-1), not FALSE.

In versions before 5.0 ip2long was return -1 in both situations - as error and in case of addres=255.255.255.255. This is not good solution, and, as I think, authors rewrite function to return FALSE in case of invalid IP and return -1 in case of IP=255.255.255.255. And this behavior of function was written in documentation, so documentation _is_ correct.

Sorry for so many words in bad English :(
 [2004-08-19 14:59 UTC] tony2001@php.net
This is already documented and the function works as the documentation states. If you don't like how it was implemented, you are free to send a patch and/or to open a feature request. This is not a bug.
 [2004-08-19 15:19 UTC] derick@php.net
Also a duplicate of #25487.

The function should NEVER return FALSE, but it should return -1 for 255.255.255.255; if it does not do this, or it is not documented as such. The bug is open.
 [2004-08-19 15:30 UTC] vrana@php.net
http://www.php.net/Changelog-5.php#5.0.0RC3 :

Fixed ip2long() to return FALSE if an IP address passed to this function is not valid. (Derick).

The function really returns FALSE for 255.255.255.255 and for invalid addresses and this is what is documented. If it's not correct, change this bug to Network related, fix the source and then change it back to Documentation problem.

If the behavior is correct, leave the bug closed.
 [2004-08-19 15:57 UTC] belikoviv at is dot lg dot ua
Since version 5.0 ip2long returns FALSE on invalid address. It is documented, and it is correct.

But ip2long returns FALSE on 255.255.255.255 - it is _not_ correct, because 255.255.255.255 is _valid_ address.

Documentation says that ip2long returns -1 on 255.255.255.255 - it is _correct_, because unsigned 0xffffffff is signed -1.

Unfortunatelly, I cannot fix this error by myself...
 [2004-08-19 16:04 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-08-19 16:04 UTC] tony2001@php.net
Fixed in CVS, thanks.
 [2004-08-19 18:18 UTC] belikoviv at is dot lg dot ua
Sorry, but problem not fixed - I try php5-win32-latest.zip and php5.0-win32-200408190830.zip. Both snapshots return FALSE on address 255.255.255.255.
 [2005-04-06 12:16 UTC] belikoviv at is dot lg dot ua
Function ip2long() fixed long time ago, but now I see incorrect note in documentation (at bottom of the chapter):

"Note:  ip2long() will return -1 (PHP 4) or FALSE (PHP 5) for the IP 255.255.255.255"

This is incorrect. ip2long returns -1 in both PHP4 and PHP5 for IP 255.255.255.255. Value "-1" is _correct_ value for 255.255.255.255, so that note must be deleted from documentation. Note about PHP4 is exist near the top of chapter.
 [2005-04-06 15:23 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"ip2long() will return FALSE for the IP 255.255.255.255 in PHP 5 <= 5.0.2. It was fixed in PHP 5.0.3 where it returns -1 (same as PHP 4)."
 [2005-04-06 15:27 UTC] vrana@php.net
The function is returning -1 also for any substring of "255.255.255.255" (e.g. "" or "255.") but it should return FALSE.

memcmp(Z_STRVAL_PP(str), "255.255.255.255", Z_STRLEN_PP(str)) should be replaced by strcmp(Z_STRVAL_PP(str), "255.255.255.255").
 [2005-04-07 18:02 UTC] sniper@php.net
It behaves the same in PHP 4. No bug here.

 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 10:02:06 2014 UTC