Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ PHP 8.7 UPGRADE NOTES
curl_setopt() return false, instead of being silently interpreted as a
combination of redirect types.

- FTP:
. ftp_connect() and ftp_ssl_connect() now throw a ValueError when $port is
outside the 0-65535 range, instead of silently truncating it to its low
16 bits (e.g. 65536 previously connected to port 21).

- PDO:
. PDOStatement::bindColumn() now throws a ValueError when the column name is
not present in the result set. It previously reported the condition
Expand Down
10 changes: 10 additions & 0 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ PHP_FUNCTION(ftp_connect)
RETURN_THROWS();
}

if (port < 0 || port > 65535) {
zend_argument_value_error(2, "must be between 0 and 65535");
RETURN_THROWS();
}

if (timeout_sec <= 0) {
zend_argument_value_error(3, "must be greater than 0");
RETURN_THROWS();
Expand Down Expand Up @@ -186,6 +191,11 @@ PHP_FUNCTION(ftp_ssl_connect)
RETURN_THROWS();
}

if (port < 0 || port > 65535) {
zend_argument_value_error(2, "must be between 0 and 65535");
RETURN_THROWS();
}

if (timeout_sec <= 0) {
zend_argument_value_error(3, "must be greater than 0");
RETURN_THROWS();
Expand Down
31 changes: 31 additions & 0 deletions ext/ftp/tests/ftp_connect_port_range.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Comment thread
lacatoire marked this conversation as resolved.
Resolved
ftp_connect(): $port values outside 0-65535 must throw ValueError, not alias onto a valid port
--EXTENSIONS--
ftp
--FILE--
<?php
foreach ([-1, 65536, 65536 + 2121, PHP_INT_MIN, PHP_INT_MAX] as $port) {
try {
ftp_connect('127.0.0.1', $port);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}

// Port 0 must be accepted (maps to default FTP port 21 in ftp_open)
// We expect false (connection refused), not ValueError.
$result = @ftp_connect('127.0.0.1', 0);
var_dump($result === false || is_object($result)); // true either way

// Port 65535 must be accepted (last valid port).
$result = @ftp_connect('127.0.0.1', 65535);
var_dump($result === false || is_object($result)); // true either way
?>
--EXPECT--
ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535
bool(true)
bool(true)
Loading