php / php/php-src

Behaviour difference in ip2long() between AIX and Linux for IPs with leading zeros

Open
#21,295 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ip2long() examples on AIX and Linux, comparing the handling of IPv4 octets with leading zeros and the resulting long2ip() output. Trace the ip2long() implementation and its platform-dependent parsing, then verify that the expected invalid inputs return FALSE while the other examples retain their shown results.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.