Skip to content

Commit dfdae42

Browse files
authored
Fix wrong argument number and UB in fsockopen timeout error (#23431)
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 #6" with no parameter name, since get_function_arg_name() returns NULL when arg_num exceeds the actual argument count. The format string also contained ZEND_ULONG_FMT (which expects a zend_ulong) but received a double expression, causing undefined behavior. This is fixed by casting to uint64_t instead.
1 parent 3ab8be6 commit dfdae42

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ PHP NEWS
104104
. Fixed read buffer compaction in php_stream_filter_flush(). (crystarm)
105105
. Fixed bug GH-22410 (Incorrect float behavior with large numbers).
106106
(arshidkv12)
107+
. Fixed GH-23338 (fsockopen()/pfsockopen() ValueError reported wrong
108+
argument number for $timeout). (lacatoire)
107109

108110
- SimpleXML:
109111
. Fixed writing to a dimension of the object returned by attributes() not

ext/standard/fsock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
107107
efree(hashkey);
108108
}
109109

110-
zend_argument_value_error(6, "must be -1 or between 0 and " ZEND_ULONG_FMT, ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
110+
zend_argument_value_error(5, "must be -1 or between 0 and %" PRIu64, (uint64_t) ((double) PHP_TIMEOUT_ULL_MAX / 1000000.0));
111111
RETURN_THROWS();
112112
} else {
113113
#ifndef PHP_WIN32
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
fsockopen() and pfsockopen(): ValueError for $timeout reports correct argument number and name
3+
--FILE--
4+
<?php
5+
try {
6+
fsockopen('localhost', 80, $errno, $errstr, -2.0);
7+
} catch (Throwable $e) {
8+
echo $e::class, ': ', $e->getMessage(), "\n";
9+
}
10+
11+
try {
12+
pfsockopen('localhost', 80, $errno, $errstr, -2.0);
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), "\n";
15+
}
16+
?>
17+
--EXPECTF--
18+
ValueError: fsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
19+
ValueError: pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s

ext/standard/tests/streams/gh14780.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ try {
3131
}
3232
?>
3333
--EXPECTF--
34-
pfsockopen(): Argument #6 must be -1 or between 0 and %s
35-
pfsockopen(): Argument #6 must be -1 or between 0 and %s
34+
pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
35+
pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
3636
resource(%d) of type (persistent stream)
37-
pfsockopen(): Argument #6 must be -1 or between 0 and %s
38-
pfsockopen(): Argument #6 must be -1 or between 0 and %s
37+
pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s
38+
pfsockopen(): Argument #5 ($timeout) must be -1 or between 0 and %s

0 commit comments

Comments
 (0)