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
10 changes: 10 additions & 0 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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 @@ -187,6 +192,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--
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 (ValueError $e) {
echo $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--
ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ftp_connect(): Argument #2 ($port) must be between 0 and 65535
ftp_connect(): Argument #2 ($port) must be between 0 and 65535
bool(true)
bool(true)
Loading