From e88e04909c127f2e525465c523f71f308063dd7c Mon Sep 17 00:00:00 2001 From: Reshma V Kumar Date: Wed, 25 Feb 2026 11:43:37 -0500 Subject: [PATCH] ext/standard: Fix ip2long in AIX to treat IPs with leading zeros as invalid like LINUX close GH-21296 --- NEWS | 1 + ext/standard/basic_functions.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/NEWS b/NEWS index c4c7f989638c..c2767fd3c30c 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,7 @@ PHP NEWS - Standard: . Fixed bug GH-21689 (version_compare() incorrectly handles versions ending with a dot). (timwolla) + . Fixed ip2long() leading zeros handling inconsistency on AIX. (ayappanec) 07 May 2026, PHP 8.4.21 diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index cf54fbbe651c..a1bf86d0bcfa 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -598,6 +598,20 @@ PHP_FUNCTION(ip2long) if (addr_len == 0 || inet_pton(AF_INET, addr, &ip) != 1) { RETURN_FALSE; } +#ifdef _AIX + /* + AIX accepts IP strings with extraneous 0 (192.168.042.42 will be treated as + 192.168.42.42), while Linux doesn't. + For consistency, we convert back the IP to a string and check if it is equal to + the original string. If not, the IP should be considered invalid. + */ + char str[INET_ADDRSTRLEN]; + const char* result = inet_ntop(AF_INET, &ip, str, sizeof(str)); + ZEND_ASSERT(result != NULL); + if (strcmp(addr, result) != 0) { + RETURN_FALSE; + } +#endif RETURN_LONG(ntohl(ip.s_addr)); } /* }}} */