-
Notifications
You must be signed in to change notification settings - Fork 8k
Open
Labels
Description
Description
We observed behaviour difference in ip2long() between AIX and LINUX when handling IPv4 addresses with leading zeros in one or more of their octets.
On AIX, IP address strings with leading zeros are accepted and processed successfully by ip2long().
On LINUX, same input is considered invalid and ip2long() returns FALSE.
The following code:
<?php
$tests = [
'192.168.42.42',
'192.168.042.42',
'010.000.000.001',
'127.0.0.1',
];
foreach ($tests as $ip) {
$val = ip2long($ip);
if ($val === false) {
echo "$ip => ip2long() returned FALSE\n";
} else {
echo "$ip => ip2long() = $val, normalized = " . long2ip($val) . "\n";
}
}Resulted in this output:
192.168.42.42 => ip2long() = -1062720982, normalized = 192.168.42.42
192.168.042.42 => ip2long() = -1062720982, normalized = 192.168.42.42
010.000.000.001 => ip2long() = 167772161, normalized = 10.0.0.1
127.0.0.1 => ip2long() = 2130706433, normalized = 127.0.0.1
But I expected this output instead:
192.168.42.42 => ip2long() = -1062720982, normalized = 192.168.42.42
192.168.042.42 => ip2long() returned FALSE
010.000.000.001 => ip2long() returned FALSE
127.0.0.1 => ip2long() = 2130706433, normalized = 127.0.0.1
To maintain same behaviour as LINUX, we convert back the IP(ip) into a string and verify that it matches the original string. If not, the IP is considered invalid and returns FALSE. I will submit these changes via a PR. Please let me know your suggestions.
PHP Version
PHP 8.5.1 (cli) (built: Jan 20 2026 09:33:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.1, Copyright (c) Zend Technologies
with Zend OPcache v8.5.1, Copyright (c), by Zend Technologies
Operating System
AIX 7.2
Reactions are currently unavailable