Skip to content

Fix wrong argument number in fsockopen/pfsockopen timeout error - #23338

Open
lacatoire wants to merge 3 commits into
php:masterfrom
lacatoire:fix/fsockopen-timeout-arg-num
Open

Fix wrong argument number in fsockopen/pfsockopen timeout error#23338
lacatoire wants to merge 3 commits into
php:masterfrom
lacatoire:fix/fsockopen-timeout-arg-num

Conversation

@lacatoire

Copy link
Copy Markdown
Member

php_fsockopen_stream() called zend_argument_value_error(6, ...) for the $timeout parameter, but $timeout is the 5th argument. Because get_function_arg_name(func, arg_num) returns NULL when arg_num exceeds the actual argument count, the error message reported the wrong number and dropped the parameter name.

Before:

ValueError: fsockopen(): Argument #6 must be -1 or between 0 and 4294967295

After:

ValueError: fsockopen(): Argument #5 ($timeout) must be -1 or between 0 and 4294967295

The same fix applies to pfsockopen(), which delegates to the same internal helper.

A regression test is included; it does not require network connectivity since the ValueError is raised before any connection attempt.

php_fsockopen_stream() called zend_argument_value_error(6, ...) for the
$timeout parameter, but $timeout is the 5th argument. This caused the
error message to show "Argument php#6" with no parameter name, since
get_function_arg_name() returns NULL when arg_num exceeds the actual
argument count.

Reproduce:
  fsockopen('localhost', 80, $err, $errstr, -2.0);
  // Before: ValueError: fsockopen(): Argument php#6 must be -1 or between...
  // After:  ValueError: fsockopen(): Argument php#5 ($timeout) must be -1...

@NickSdot NickSdot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch for the failing ext/standard/tests/streams/gh14780.phpt:

Expand
Subject: [PATCH] fix: adjusted existing test
---
Index: ext/standard/tests/streams/gh14780.phpt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/ext/standard/tests/streams/gh14780.phpt b/ext/standard/tests/streams/gh14780.phpt
--- a/ext/standard/tests/streams/gh14780.phpt	(revision c767049dbac5a52115c667504b111d2050d8079c)
+++ b/ext/standard/tests/streams/gh14780.phpt	(date 1787064960856)
@@ -10,29 +10,29 @@
 $err = null;
 try {
 	pfsockopen('udp://127.0.0.1', '63844', $code, $err, (PHP_INT_MAX/100000)+1);
-} catch (\ValueError $e) {
-	echo $e->getMessage() . PHP_EOL;
+} catch (Throwable $e) {
+	echo $e::class, ': ', $e->getMessage(), "\n";
 }
 try {
 	pfsockopen('udp://127.0.0.1', '63844', $code, $err, (PHP_INT_MIN/100000)-1);
-} catch (\ValueError $e) {
-	echo $e->getMessage() . PHP_EOL;
+} catch (Throwable $e) {
+	echo $e::class, ': ', $e->getMessage(), "\n";
 }
 var_dump(pfsockopen('udp://127.0.0.1', '63844', $code, $err, -1));
 try {
 	pfsockopen('udp://127.0.0.1', '63844', $code, $err, NAN);
-} catch (\ValueError $e) {
-	echo $e->getMessage() . PHP_EOL;
+} catch (Throwable $e) {
+	echo $e::class, ': ', $e->getMessage(), "\n";
 }
 try {
 	pfsockopen('udp://127.0.0.1', '63844', $code, $err, INF);
-} catch (\ValueError $e) {
-	echo $e->getMessage();
+} catch (Throwable $e) {
+	echo $e::class, ': ', $e->getMessage(), "\n";
 }
 ?>
 --EXPECTF--
-pfsockopen(): Argument #6 must be -1 or between 0 and %s
-pfsockopen(): Argument #6 must be -1 or between 0 and %s
+ValueError: pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
+ValueError: pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
 resource(%d) of type (persistent stream)
-pfsockopen(): Argument #6 must be -1 or between 0 and %s
-pfsockopen(): Argument #6 must be -1 or between 0 and %s
+ValueError: pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
+ValueError: pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s

Comment thread ext/standard/tests/network/fsockopen_timeout_out_of_range.phpt Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants