From 5a86f9e9247db8d8de3313ed6a74b44049e3bbcf Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 14 Feb 2024 21:18:18 +0100 Subject: [PATCH 1/5] Changed return type of long2ip to string --- ext/standard/basic_functions.c | 8 +++----- ext/standard/basic_functions.stub.php | 2 +- ext/standard/basic_functions_arginfo.h | 4 ++-- ext/standard/tests/network/ip_x86_64.phpt | 4 ++++ 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index d62951b63ff26..d9de973041904 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -643,11 +643,9 @@ PHP_FUNCTION(long2ip) ip = (zend_ulong)sip; myaddr.s_addr = htonl(ip); - if (inet_ntop(AF_INET, &myaddr, str, sizeof(str))) { - RETURN_STRING(str); - } else { - RETURN_FALSE; - } + inet_ntop(AF_INET, &myaddr, str, sizeof(str)); + + RETURN_STRING(str); } /* }}} */ diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index ed73d16d6d0b9..899d29ea3b078 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -1945,7 +1945,7 @@ function constant(string $name): mixed {} function ip2long(string $ip): int|false {} /** @refcount 1 */ -function long2ip(int $ip): string|false {} +function long2ip(int $ip): string {} /** * @return string|array|false diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 4efdcb54546b6..7a1812d7ca565 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 0bd0ac5d23881670cac81cda3e274cbee1e9a8dc */ + * Stub hash: 1350cc5169dbd48df08513f01c10d5706d47b8d4 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -381,7 +381,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ip2long, 0, 1, MAY_BE_LONG|MAY_B ZEND_ARG_TYPE_INFO(0, ip, IS_STRING, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_long2ip, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_long2ip, 0, 1, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, ip, IS_LONG, 0) ZEND_END_ARG_INFO() diff --git a/ext/standard/tests/network/ip_x86_64.phpt b/ext/standard/tests/network/ip_x86_64.phpt index dd81bde55c89b..16e855188aafa 100644 --- a/ext/standard/tests/network/ip_x86_64.phpt +++ b/ext/standard/tests/network/ip_x86_64.phpt @@ -26,6 +26,8 @@ var_dump(ip2long("777.777.777.777")); var_dump(ip2long("111.111.111.111")); var_dump(long2ip(-110000)); +var_dump(long2ip(PHP_INT_MAX)); +var_dump(long2ip(PHP_INT_MIN)); echo "Done\n"; ?> @@ -46,4 +48,6 @@ bool(false) bool(false) int(1869573999) string(13) "255.254.82.80" +string(15) "255.255.255.255" +string(7) "0.0.0.0" Done From a82af94bb4238627335bb40bcca4f6d786e79eba Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 14 Feb 2024 23:38:54 +0100 Subject: [PATCH 2/5] Improve return type of long2ip --- Zend/Optimizer/zend_func_infos.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h index 16971fc178ea7..d1799ad0de821 100644 --- a/Zend/Optimizer/zend_func_infos.h +++ b/Zend/Optimizer/zend_func_infos.h @@ -468,7 +468,7 @@ static const func_info_t func_infos[] = { FN("array_rand", MAY_BE_LONG|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING), F1("base64_encode", MAY_BE_STRING), F1("base64_decode", MAY_BE_STRING|MAY_BE_FALSE), - F1("long2ip", MAY_BE_STRING|MAY_BE_FALSE), + F1("long2ip", MAY_BE_STRING), F1("getenv", MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE), F1("getopt", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_FALSE|MAY_BE_FALSE), #if defined(HAVE_NANOSLEEP) From 3fe3027e1d828131e306ea5dfc3ebe8949d604ee Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Thu, 15 Feb 2024 00:38:50 +0100 Subject: [PATCH 3/5] Added ASSERT to function --- ext/standard/basic_functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index d9de973041904..f8921f27dc0f3 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -643,7 +643,7 @@ PHP_FUNCTION(long2ip) ip = (zend_ulong)sip; myaddr.s_addr = htonl(ip); - inet_ntop(AF_INET, &myaddr, str, sizeof(str)); + ZEND_ASSERT(inet_ntop(AF_INET, &myaddr, str, sizeof(str))); RETURN_STRING(str); } From f338ace94c4cca8497730ee9915b524fe44e1b7d Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Thu, 15 Feb 2024 21:26:07 +0100 Subject: [PATCH 4/5] Removed assert --- ext/standard/basic_functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index f8921f27dc0f3..d9de973041904 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -643,7 +643,7 @@ PHP_FUNCTION(long2ip) ip = (zend_ulong)sip; myaddr.s_addr = htonl(ip); - ZEND_ASSERT(inet_ntop(AF_INET, &myaddr, str, sizeof(str))); + inet_ntop(AF_INET, &myaddr, str, sizeof(str)); RETURN_STRING(str); } From 161de888becb0a6c638a7c46e370ab71458d3c2d Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Thu, 15 Feb 2024 22:57:09 +0100 Subject: [PATCH 5/5] Reverted assert --- ext/standard/basic_functions.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index d9de973041904..ed4309818275a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -643,7 +643,8 @@ PHP_FUNCTION(long2ip) ip = (zend_ulong)sip; myaddr.s_addr = htonl(ip); - inet_ntop(AF_INET, &myaddr, str, sizeof(str)); + const char* result = inet_ntop(AF_INET, &myaddr, str, sizeof(str)); + ZEND_ASSERT(result != NULL); RETURN_STRING(str); }