Skip to content

Commit e88e049

Browse files
reshmavkdevnexen
authored andcommitted
ext/standard: Fix ip2long in AIX to treat IPs with leading zeros as invalid like LINUX
close GH-21296
1 parent f79f921 commit e88e049

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ PHP NEWS
2121
- Standard:
2222
. Fixed bug GH-21689 (version_compare() incorrectly handles versions ending
2323
with a dot). (timwolla)
24+
. Fixed ip2long() leading zeros handling inconsistency on AIX. (ayappanec)
2425

2526
07 May 2026, PHP 8.4.21
2627

ext/standard/basic_functions.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,20 @@ PHP_FUNCTION(ip2long)
598598
if (addr_len == 0 || inet_pton(AF_INET, addr, &ip) != 1) {
599599
RETURN_FALSE;
600600
}
601+
#ifdef _AIX
602+
/*
603+
AIX accepts IP strings with extraneous 0 (192.168.042.42 will be treated as
604+
192.168.42.42), while Linux doesn't.
605+
For consistency, we convert back the IP to a string and check if it is equal to
606+
the original string. If not, the IP should be considered invalid.
607+
*/
608+
char str[INET_ADDRSTRLEN];
609+
const char* result = inet_ntop(AF_INET, &ip, str, sizeof(str));
610+
ZEND_ASSERT(result != NULL);
611+
if (strcmp(addr, result) != 0) {
612+
RETURN_FALSE;
613+
}
614+
#endif
601615
RETURN_LONG(ntohl(ip.s_addr));
602616
}
603617
/* }}} */

0 commit comments

Comments
 (0)