Skip to content

Promote warnings to exceptions in ext/ftp #6054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions ext/ftp/ftp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function ftp_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $r
* @param resource $ftp
* @param resource $fp
*/
function ftp_nb_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $resumpos = 0): int|false {}
function ftp_nb_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $resumpos = 0): int {}

/** @param resource $ftp */
function ftp_pasv($ftp, bool $pasv): bool {}
Expand All @@ -89,7 +89,7 @@ function ftp_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $s
* @param resource $ftp
* @param resource $fp
*/
function ftp_nb_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $startpos = 0): int|false {}
function ftp_nb_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $startpos = 0): int {}

/** @param resource $ftp */
function ftp_put($ftp, string $remote_file, string $local_file, int $mode = FTP_BINARY, int $startpos = 0): bool {}
Expand Down
6 changes: 3 additions & 3 deletions ext/ftp/ftp_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 830fdf6caf2804d49739fc5112c7091c0f6eb4d4 */
* Stub hash: 7cf8b5776e7d2ad943683d1f1c43d44b85dea7a1 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_ftp_connect, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 0)
Expand Down Expand Up @@ -86,7 +86,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_fget, 0, 3, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, resumepos, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_nb_fget, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_nb_fget, 0, 3, IS_LONG, 0)
ZEND_ARG_INFO(0, ftp)
ZEND_ARG_INFO(0, fp)
ZEND_ARG_TYPE_INFO(0, remote_file, IS_STRING, 0)
Expand Down Expand Up @@ -127,7 +127,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_fput, 0, 3, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, startpos, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_nb_fput, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_nb_fput, 0, 3, IS_LONG, 0)
ZEND_ARG_INFO(0, ftp)
ZEND_ARG_TYPE_INFO(0, remote_file, IS_STRING, 0)
ZEND_ARG_INFO(0, fp)
Expand Down
50 changes: 23 additions & 27 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ PHP_MINFO_FUNCTION(ftp)
}

#define XTYPE(xtype, mode) { \
if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
php_error_docref(NULL, E_WARNING, "Mode must be FTP_ASCII or FTP_BINARY"); \
RETURN_FALSE; \
} \
xtype = mode; \
}
if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
zend_argument_value_error(4, "must be either FTP_ASCII or FTP_BINARY"); \
RETURN_THROWS(); \
} \
xtype = mode; \
}


/* {{{ Opens a FTP stream */
Expand All @@ -126,8 +126,8 @@ PHP_FUNCTION(ftp_connect)
}

if (timeout_sec <= 0) {
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
RETURN_FALSE;
zend_argument_value_error(3, "must be greater than 0");
RETURN_THROWS();
}

/* connect */
Expand Down Expand Up @@ -162,8 +162,8 @@ PHP_FUNCTION(ftp_ssl_connect)
}

if (timeout_sec <= 0) {
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
RETURN_FALSE;
zend_argument_value_error(3, "must be greater than 0");
RETURN_THROWS();
}

/* connect */
Expand Down Expand Up @@ -825,7 +825,7 @@ PHP_FUNCTION(ftp_nb_continue)
}

if (!ftp->nb) {
php_error_docref(NULL, E_WARNING, "No nbronous transfer to continue.");
php_error_docref(NULL, E_WARNING, "No nbronous transfer to continue");
RETURN_LONG(PHP_FTP_FAILED);
}

Expand Down Expand Up @@ -1257,38 +1257,35 @@ PHP_FUNCTION(ftp_set_option)
switch (option) {
case PHP_FTP_OPT_TIMEOUT_SEC:
if (Z_TYPE_P(z_value) != IS_LONG) {
php_error_docref(NULL, E_WARNING, "Option TIMEOUT_SEC expects value of type int, %s given",
zend_zval_type_name(z_value));
RETURN_FALSE;
zend_argument_type_error(3, "must be of type int for the FTP_TIMEOUT_SEC option, %s given", zend_zval_type_name(z_value));
RETURN_THROWS();
}
if (Z_LVAL_P(z_value) <= 0) {
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
RETURN_FALSE;
zend_argument_value_error(3, "must be greater than 0 for the FTP_TIMEOUT_SEC option");
RETURN_THROWS();
}
ftp->timeout_sec = Z_LVAL_P(z_value);
RETURN_TRUE;
break;
case PHP_FTP_OPT_AUTOSEEK:
if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
php_error_docref(NULL, E_WARNING, "Option AUTOSEEK expects value of type bool, %s given",
zend_zval_type_name(z_value));
RETURN_FALSE;
zend_argument_type_error(3, "must be of type bool for the FTP_AUTOSEEK option, %s given", zend_zval_type_name(z_value));
RETURN_THROWS();
}
ftp->autoseek = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
RETURN_TRUE;
break;
case PHP_FTP_OPT_USEPASVADDRESS:
if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
php_error_docref(NULL, E_WARNING, "Option USEPASVADDRESS expects value of type bool, %s given",
zend_zval_type_name(z_value));
RETURN_FALSE;
zend_argument_type_error(3, "must be of type bool for the FTP_USEPASVADDRESS option, %s given", zend_zval_type_name(z_value));
RETURN_THROWS();
}
ftp->usepasvaddress = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
RETURN_TRUE;
break;
default:
php_error_docref(NULL, E_WARNING, "Unknown option '" ZEND_LONG_FMT "'", option);
RETURN_FALSE;
zend_argument_value_error(2, "must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
RETURN_THROWS();
break;
}
}
Expand Down Expand Up @@ -1320,9 +1317,8 @@ PHP_FUNCTION(ftp_get_option)
RETURN_BOOL(ftp->usepasvaddress);
break;
default:
php_error_docref(NULL, E_WARNING, "Unknown option '" ZEND_LONG_FMT "'", option);
RETURN_FALSE;
break;
zend_argument_value_error(2, "must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
RETURN_THROWS();
}
}
/* }}} */
Expand Down
9 changes: 6 additions & 3 deletions ext/ftp/tests/004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ require 'skipif.inc';
require 'server.inc';

// Negative timeout
var_dump(ftp_connect('127.0.0.1', 0, -3));
try {
ftp_connect('127.0.0.1', 0, -3);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

$ftp = ftp_connect('127.0.0.1', $port);
if (!$ftp) die("Couldn't connect to the server");
Expand All @@ -20,8 +24,7 @@ var_dump(ftp_login($ftp, 'user', 'bogus'));
var_dump(ftp_quit($ftp));
?>
--EXPECTF--
Warning: ftp_connect(): Timeout has to be greater than 0 in %s on line %d
bool(false)
ftp_connect(): Argument #3 ($timeout) must be greater than 0
bool(true)

Warning: ftp_login(): Not logged in. in %s on line %d
Expand Down
76 changes: 46 additions & 30 deletions ext/ftp/tests/005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,40 @@ var_dump(ftp_chdir($ftp, '~'));
var_dump(ftp_chmod($ftp, 0666, 'x'));
var_dump(ftp_delete($ftp, 'x'));
var_dump(ftp_exec($ftp, 'x'));
var_dump(ftp_fget($ftp, STDOUT, 'x', 0));
var_dump(ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));
var_dump(ftp_get($ftp, 'x', 'y', 0));
try {
ftp_fget($ftp, STDOUT, 'x', 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
ftp_get($ftp, 'x', 'y', 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

var_dump(ftp_mdtm($ftp, 'x'));
var_dump(ftp_mkdir($ftp, 'x'));
var_dump(ftp_nb_continue($ftp));
var_dump(ftp_nb_fget($ftp, STDOUT, 'x', 0));
var_dump(ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));

try {
ftp_nb_fget($ftp, STDOUT, 'x', 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

var_dump(ftp_systype($ftp));
var_dump(ftp_pwd($ftp));
var_dump(ftp_size($ftp, ''));
Expand All @@ -38,49 +64,39 @@ var_dump(ftp_rmdir($ftp, ''));
bool(true)
bool(false)

Warning: ftp_cdup(): Command not implemented (1). in %s005.php on line 11
bool(false)

Warning: ftp_chdir(): Command not implemented (2). in %s005.php on line 12
bool(false)

Warning: ftp_chmod(): Command not implemented (3). in %s005.php on line 13
bool(false)

Warning: ftp_delete(): Command not implemented (4). in %s005.php on line 14
Warning: ftp_cdup(): Command not implemented (1). in %s005.php on line %d
bool(false)

Warning: ftp_exec(): Command not implemented (5). in %s005.php on line 15
Warning: ftp_chdir(): Command not implemented (2). in %s005.php on line %d
bool(false)

Warning: ftp_fget(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 16
Warning: ftp_chmod(): Command not implemented (3). in %s005.php on line %d
bool(false)

Warning: ftp_fput(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 17
Warning: ftp_delete(): Command not implemented (4). in %s005.php on line %d
bool(false)

Warning: ftp_get(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 18
Warning: ftp_exec(): Command not implemented (5). in %s005.php on line %d
bool(false)
ftp_fget(): Argument #4 ($mode) must be either FTP_ASCII or FTP_BINARY
ftp_fput(): Argument #4 ($mode) must be either FTP_ASCII or FTP_BINARY
ftp_get(): Argument #4 ($mode) must be either FTP_ASCII or FTP_BINARY
int(-1)

Warning: ftp_mkdir(): Command not implemented (7). in %s005.php on line 20
Warning: ftp_mkdir(): Command not implemented (7). in %s005.php on line %d
bool(false)

Warning: ftp_nb_continue(): No nbronous transfer to continue. in %s005.php on line 21
Warning: ftp_nb_continue(): No nbronous transfer to continue in %s005.php on line %d
int(0)
ftp_nb_fget(): Argument #4 ($mode) must be either FTP_ASCII or FTP_BINARY
ftp_nb_fput(): Argument #4 ($mode) must be either FTP_ASCII or FTP_BINARY

Warning: ftp_nb_fget(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 22
bool(false)

Warning: ftp_nb_fput(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 23
bool(false)

Warning: ftp_systype(): Command not implemented (8). in %s005.php on line 24
Warning: ftp_systype(): Command not implemented (8). in %s005.php on line %d
bool(false)

Warning: ftp_pwd(): Command not implemented (9). in %s005.php on line 25
Warning: ftp_pwd(): Command not implemented (9). in %s005.php on line %d
bool(false)
int(-1)

Warning: ftp_rmdir(): Command not implemented (11). in %s005.php on line 27
Warning: ftp_rmdir(): Command not implemented (11). in %s005.php on line %d
bool(false)
12 changes: 8 additions & 4 deletions ext/ftp/tests/ftp_get_option.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ $ftp or die("Couldn't connect to the server");
var_dump(ftp_get_option($ftp, FTP_TIMEOUT_SEC));
var_dump(ftp_get_option($ftp, FTP_AUTOSEEK));
var_dump(ftp_get_option($ftp, FTP_USEPASVADDRESS));
var_dump(ftp_get_option($ftp, FOO_BAR));

try {
ftp_get_option($ftp, FOO_BAR);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

?>
--EXPECTF--
int(%d)
bool(true)
bool(true)

Warning: ftp_get_option(): Unknown option '10' in %s on line %d
bool(false)
ftp_get_option(): Argument #2 ($option) must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS
50 changes: 33 additions & 17 deletions ext/ftp/tests/ftp_set_option_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,40 @@ $ftp = ftp_connect('127.0.0.1', $port);
ftp_login($ftp, 'user', 'pass');
$ftp or die("Couldn't connect to the server");

var_dump(ftp_set_option($ftp, FTP_TIMEOUT_SEC, 0));
var_dump(ftp_set_option($ftp, FTP_TIMEOUT_SEC, '0'));
var_dump(ftp_set_option($ftp, FTP_USEPASVADDRESS, ['1']));
var_dump(ftp_set_option($ftp, FTP_AUTOSEEK, 'true'));
var_dump(ftp_set_option($ftp, FOO_BAR, 1));
?>
--EXPECTF--
Warning: ftp_set_option(): Timeout has to be greater than 0 in %s on line %d
bool(false)
try {
ftp_set_option($ftp, FTP_TIMEOUT_SEC, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

try {
ftp_set_option($ftp, FTP_TIMEOUT_SEC, '0');
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

Warning: ftp_set_option(): Option TIMEOUT_SEC expects value of type int, string given in %s on line %d
bool(false)
try {
ftp_set_option($ftp, FTP_USEPASVADDRESS, ['1']);
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

Warning: ftp_set_option(): Option USEPASVADDRESS expects value of type bool, array given in %s on line %d
bool(false)
try {
ftp_set_option($ftp, FTP_AUTOSEEK, 'true');
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

Warning: ftp_set_option(): Option AUTOSEEK expects value of type bool, string given in %s on line %d
bool(false)
try {
ftp_set_option($ftp, FOO_BAR, 1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

Warning: ftp_set_option(): Unknown option '10' in %s on line %d
bool(false)
?>
--EXPECT--
ftp_set_option(): Argument #3 ($value) must be greater than 0 for the FTP_TIMEOUT_SEC option
ftp_set_option(): Argument #3 ($value) must be of type int for the FTP_TIMEOUT_SEC option, string given
ftp_set_option(): Argument #3 ($value) must be of type bool for the FTP_USEPASVADDRESS option, array given
ftp_set_option(): Argument #3 ($value) must be of type bool for the FTP_AUTOSEEK option, string given
ftp_set_option(): Argument #2 ($option) must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS
13 changes: 8 additions & 5 deletions ext/ftp/tests/ftp_ssl_connect_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ echo "*** Testing ftp_ssl_connect() function : error conditions ***\n";
echo "\n-- Testing ftp_ssl_connect() function on failure --\n";
var_dump(ftp_ssl_connect('totes.invalid'));

echo "\n-- Testing ftp_ssl_connect() function timeout warning for value 0 --\n";
ftp_ssl_connect('totes.invalid', 21, 0);
echo "\n-- Testing ftp_ssl_connect() function timeout exception for value 0 --\n";
try {
ftp_ssl_connect('totes.invalid', 21, 0);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

echo "===DONE===\n";
--EXPECTF--
Expand All @@ -24,7 +28,6 @@ echo "===DONE===\n";
Warning: ftp_ssl_connect(): php_network_getaddresses: getaddrinfo failed: %s in %s on line %d
bool(false)

-- Testing ftp_ssl_connect() function timeout warning for value 0 --

Warning: ftp_ssl_connect(): Timeout has to be greater than 0 in %s on line %d
-- Testing ftp_ssl_connect() function timeout exception for value 0 --
ftp_ssl_connect(): Argument #3 ($timeout) must be greater than 0
===DONE===