Skip to content

Commit 1410cd6

Browse files
committed
Promote warnings to exceptions in ext/ftp
Closes GH-6054
1 parent 4a84aca commit 1410cd6

8 files changed

+129
-91
lines changed

ext/ftp/ftp.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function ftp_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $r
6565
* @param resource $ftp
6666
* @param resource $fp
6767
*/
68-
function ftp_nb_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $resumpos = 0): int|false {}
68+
function ftp_nb_fget($ftp, $fp, string $remote_file, int $mode = FTP_BINARY, int $resumpos = 0): int {}
6969

7070
/** @param resource $ftp */
7171
function ftp_pasv($ftp, bool $pasv): bool {}
@@ -89,7 +89,7 @@ function ftp_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $s
8989
* @param resource $ftp
9090
* @param resource $fp
9191
*/
92-
function ftp_nb_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $startpos = 0): int|false {}
92+
function ftp_nb_fput($ftp, string $remote_file, $fp, int $mode = FTP_BINARY, int $startpos = 0): int {}
9393

9494
/** @param resource $ftp */
9595
function ftp_put($ftp, string $remote_file, string $local_file, int $mode = FTP_BINARY, int $startpos = 0): bool {}

ext/ftp/ftp_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 830fdf6caf2804d49739fc5112c7091c0f6eb4d4 */
2+
* Stub hash: 7cf8b5776e7d2ad943683d1f1c43d44b85dea7a1 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_ftp_connect, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, host, IS_STRING, 0)
@@ -86,7 +86,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_fget, 0, 3, _IS_BOOL, 0)
8686
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, resumepos, IS_LONG, 0, "0")
8787
ZEND_END_ARG_INFO()
8888

89-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_nb_fget, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
89+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_nb_fget, 0, 3, IS_LONG, 0)
9090
ZEND_ARG_INFO(0, ftp)
9191
ZEND_ARG_INFO(0, fp)
9292
ZEND_ARG_TYPE_INFO(0, remote_file, IS_STRING, 0)
@@ -127,7 +127,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_fput, 0, 3, _IS_BOOL, 0)
127127
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, startpos, IS_LONG, 0, "0")
128128
ZEND_END_ARG_INFO()
129129

130-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_nb_fput, 0, 3, MAY_BE_LONG|MAY_BE_FALSE)
130+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftp_nb_fput, 0, 3, IS_LONG, 0)
131131
ZEND_ARG_INFO(0, ftp)
132132
ZEND_ARG_TYPE_INFO(0, remote_file, IS_STRING, 0)
133133
ZEND_ARG_INFO(0, fp)

ext/ftp/php_ftp.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ PHP_MINFO_FUNCTION(ftp)
104104
}
105105

106106
#define XTYPE(xtype, mode) { \
107-
if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
108-
php_error_docref(NULL, E_WARNING, "Mode must be FTP_ASCII or FTP_BINARY"); \
109-
RETURN_FALSE; \
110-
} \
111-
xtype = mode; \
112-
}
107+
if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
108+
zend_argument_value_error(4, "must be either FTP_ASCII or FTP_BINARY"); \
109+
RETURN_THROWS(); \
110+
} \
111+
xtype = mode; \
112+
}
113113

114114

115115
/* {{{ Opens a FTP stream */
@@ -126,8 +126,8 @@ PHP_FUNCTION(ftp_connect)
126126
}
127127

128128
if (timeout_sec <= 0) {
129-
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
130-
RETURN_FALSE;
129+
zend_argument_value_error(3, "must be greater than 0");
130+
RETURN_THROWS();
131131
}
132132

133133
/* connect */
@@ -162,8 +162,8 @@ PHP_FUNCTION(ftp_ssl_connect)
162162
}
163163

164164
if (timeout_sec <= 0) {
165-
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
166-
RETURN_FALSE;
165+
zend_argument_value_error(3, "must be greater than 0");
166+
RETURN_THROWS();
167167
}
168168

169169
/* connect */
@@ -825,7 +825,7 @@ PHP_FUNCTION(ftp_nb_continue)
825825
}
826826

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

@@ -1257,38 +1257,35 @@ PHP_FUNCTION(ftp_set_option)
12571257
switch (option) {
12581258
case PHP_FTP_OPT_TIMEOUT_SEC:
12591259
if (Z_TYPE_P(z_value) != IS_LONG) {
1260-
php_error_docref(NULL, E_WARNING, "Option TIMEOUT_SEC expects value of type int, %s given",
1261-
zend_zval_type_name(z_value));
1262-
RETURN_FALSE;
1260+
zend_argument_type_error(3, "must be of type int for the FTP_TIMEOUT_SEC option, %s given", zend_zval_type_name(z_value));
1261+
RETURN_THROWS();
12631262
}
12641263
if (Z_LVAL_P(z_value) <= 0) {
1265-
php_error_docref(NULL, E_WARNING, "Timeout has to be greater than 0");
1266-
RETURN_FALSE;
1264+
zend_argument_value_error(3, "must be greater than 0 for the FTP_TIMEOUT_SEC option");
1265+
RETURN_THROWS();
12671266
}
12681267
ftp->timeout_sec = Z_LVAL_P(z_value);
12691268
RETURN_TRUE;
12701269
break;
12711270
case PHP_FTP_OPT_AUTOSEEK:
12721271
if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
1273-
php_error_docref(NULL, E_WARNING, "Option AUTOSEEK expects value of type bool, %s given",
1274-
zend_zval_type_name(z_value));
1275-
RETURN_FALSE;
1272+
zend_argument_type_error(3, "must be of type bool for the FTP_AUTOSEEK option, %s given", zend_zval_type_name(z_value));
1273+
RETURN_THROWS();
12761274
}
12771275
ftp->autoseek = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
12781276
RETURN_TRUE;
12791277
break;
12801278
case PHP_FTP_OPT_USEPASVADDRESS:
12811279
if (Z_TYPE_P(z_value) != IS_TRUE && Z_TYPE_P(z_value) != IS_FALSE) {
1282-
php_error_docref(NULL, E_WARNING, "Option USEPASVADDRESS expects value of type bool, %s given",
1283-
zend_zval_type_name(z_value));
1284-
RETURN_FALSE;
1280+
zend_argument_type_error(3, "must be of type bool for the FTP_USEPASVADDRESS option, %s given", zend_zval_type_name(z_value));
1281+
RETURN_THROWS();
12851282
}
12861283
ftp->usepasvaddress = Z_TYPE_P(z_value) == IS_TRUE ? 1 : 0;
12871284
RETURN_TRUE;
12881285
break;
12891286
default:
1290-
php_error_docref(NULL, E_WARNING, "Unknown option '" ZEND_LONG_FMT "'", option);
1291-
RETURN_FALSE;
1287+
zend_argument_value_error(2, "must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
1288+
RETURN_THROWS();
12921289
break;
12931290
}
12941291
}
@@ -1320,9 +1317,8 @@ PHP_FUNCTION(ftp_get_option)
13201317
RETURN_BOOL(ftp->usepasvaddress);
13211318
break;
13221319
default:
1323-
php_error_docref(NULL, E_WARNING, "Unknown option '" ZEND_LONG_FMT "'", option);
1324-
RETURN_FALSE;
1325-
break;
1320+
zend_argument_value_error(2, "must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS");
1321+
RETURN_THROWS();
13261322
}
13271323
}
13281324
/* }}} */

ext/ftp/tests/004.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ require 'skipif.inc';
99
require 'server.inc';
1010

1111
// Negative timeout
12-
var_dump(ftp_connect('127.0.0.1', 0, -3));
12+
try {
13+
ftp_connect('127.0.0.1', 0, -3);
14+
} catch (ValueError $exception) {
15+
echo $exception->getMessage() . "\n";
16+
}
1317

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

2730
Warning: ftp_login(): Not logged in. in %s on line %d

ext/ftp/tests/005.phpt

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,40 @@ var_dump(ftp_chdir($ftp, '~'));
2020
var_dump(ftp_chmod($ftp, 0666, 'x'));
2121
var_dump(ftp_delete($ftp, 'x'));
2222
var_dump(ftp_exec($ftp, 'x'));
23-
var_dump(ftp_fget($ftp, STDOUT, 'x', 0));
24-
var_dump(ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));
25-
var_dump(ftp_get($ftp, 'x', 'y', 0));
23+
try {
24+
ftp_fget($ftp, STDOUT, 'x', 0);
25+
} catch (ValueError $exception) {
26+
echo $exception->getMessage() . "\n";
27+
}
28+
29+
try {
30+
ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0);
31+
} catch (ValueError $exception) {
32+
echo $exception->getMessage() . "\n";
33+
}
34+
35+
try {
36+
ftp_get($ftp, 'x', 'y', 0);
37+
} catch (ValueError $exception) {
38+
echo $exception->getMessage() . "\n";
39+
}
40+
2641
var_dump(ftp_mdtm($ftp, 'x'));
2742
var_dump(ftp_mkdir($ftp, 'x'));
2843
var_dump(ftp_nb_continue($ftp));
29-
var_dump(ftp_nb_fget($ftp, STDOUT, 'x', 0));
30-
var_dump(ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));
44+
45+
try {
46+
ftp_nb_fget($ftp, STDOUT, 'x', 0);
47+
} catch (ValueError $exception) {
48+
echo $exception->getMessage() . "\n";
49+
}
50+
51+
try {
52+
ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0);
53+
} catch (ValueError $exception) {
54+
echo $exception->getMessage() . "\n";
55+
}
56+
3157
var_dump(ftp_systype($ftp));
3258
var_dump(ftp_pwd($ftp));
3359
var_dump(ftp_size($ftp, ''));
@@ -38,49 +64,39 @@ var_dump(ftp_rmdir($ftp, ''));
3864
bool(true)
3965
bool(false)
4066

41-
Warning: ftp_cdup(): Command not implemented (1). in %s005.php on line 11
42-
bool(false)
43-
44-
Warning: ftp_chdir(): Command not implemented (2). in %s005.php on line 12
45-
bool(false)
46-
47-
Warning: ftp_chmod(): Command not implemented (3). in %s005.php on line 13
48-
bool(false)
49-
50-
Warning: ftp_delete(): Command not implemented (4). in %s005.php on line 14
67+
Warning: ftp_cdup(): Command not implemented (1). in %s005.php on line %d
5168
bool(false)
5269

53-
Warning: ftp_exec(): Command not implemented (5). in %s005.php on line 15
70+
Warning: ftp_chdir(): Command not implemented (2). in %s005.php on line %d
5471
bool(false)
5572

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

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

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

66-
Warning: ftp_mkdir(): Command not implemented (7). in %s005.php on line 20
86+
Warning: ftp_mkdir(): Command not implemented (7). in %s005.php on line %d
6787
bool(false)
6888

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

72-
Warning: ftp_nb_fget(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 22
73-
bool(false)
74-
75-
Warning: ftp_nb_fput(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 23
76-
bool(false)
77-
78-
Warning: ftp_systype(): Command not implemented (8). in %s005.php on line 24
94+
Warning: ftp_systype(): Command not implemented (8). in %s005.php on line %d
7995
bool(false)
8096

81-
Warning: ftp_pwd(): Command not implemented (9). in %s005.php on line 25
97+
Warning: ftp_pwd(): Command not implemented (9). in %s005.php on line %d
8298
bool(false)
8399
int(-1)
84100

85-
Warning: ftp_rmdir(): Command not implemented (11). in %s005.php on line 27
101+
Warning: ftp_rmdir(): Command not implemented (11). in %s005.php on line %d
86102
bool(false)

ext/ftp/tests/ftp_get_option.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ $ftp or die("Couldn't connect to the server");
1616
var_dump(ftp_get_option($ftp, FTP_TIMEOUT_SEC));
1717
var_dump(ftp_get_option($ftp, FTP_AUTOSEEK));
1818
var_dump(ftp_get_option($ftp, FTP_USEPASVADDRESS));
19-
var_dump(ftp_get_option($ftp, FOO_BAR));
19+
20+
try {
21+
ftp_get_option($ftp, FOO_BAR);
22+
} catch (ValueError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
25+
2026
?>
2127
--EXPECTF--
2228
int(%d)
2329
bool(true)
2430
bool(true)
25-
26-
Warning: ftp_get_option(): Unknown option '10' in %s on line %d
27-
bool(false)
31+
ftp_get_option(): Argument #2 ($option) must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS

ext/ftp/tests/ftp_set_option_errors.phpt

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,40 @@ $ftp = ftp_connect('127.0.0.1', $port);
1313
ftp_login($ftp, 'user', 'pass');
1414
$ftp or die("Couldn't connect to the server");
1515

16-
var_dump(ftp_set_option($ftp, FTP_TIMEOUT_SEC, 0));
17-
var_dump(ftp_set_option($ftp, FTP_TIMEOUT_SEC, '0'));
18-
var_dump(ftp_set_option($ftp, FTP_USEPASVADDRESS, ['1']));
19-
var_dump(ftp_set_option($ftp, FTP_AUTOSEEK, 'true'));
20-
var_dump(ftp_set_option($ftp, FOO_BAR, 1));
21-
?>
22-
--EXPECTF--
23-
Warning: ftp_set_option(): Timeout has to be greater than 0 in %s on line %d
24-
bool(false)
16+
try {
17+
ftp_set_option($ftp, FTP_TIMEOUT_SEC, 0);
18+
} catch (ValueError $exception) {
19+
echo $exception->getMessage() . "\n";
20+
}
21+
22+
try {
23+
ftp_set_option($ftp, FTP_TIMEOUT_SEC, '0');
24+
} catch (TypeError $exception) {
25+
echo $exception->getMessage() . "\n";
26+
}
2527

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

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

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

35-
Warning: ftp_set_option(): Unknown option '10' in %s on line %d
36-
bool(false)
46+
?>
47+
--EXPECT--
48+
ftp_set_option(): Argument #3 ($value) must be greater than 0 for the FTP_TIMEOUT_SEC option
49+
ftp_set_option(): Argument #3 ($value) must be of type int for the FTP_TIMEOUT_SEC option, string given
50+
ftp_set_option(): Argument #3 ($value) must be of type bool for the FTP_USEPASVADDRESS option, array given
51+
ftp_set_option(): Argument #3 ($value) must be of type bool for the FTP_AUTOSEEK option, string given
52+
ftp_set_option(): Argument #2 ($option) must be either FTP_TIMEOUT_SEC, FTP_AUTOSEEK, or FTP_USEPASVADDRESS

ext/ftp/tests/ftp_ssl_connect_error.phpt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ echo "*** Testing ftp_ssl_connect() function : error conditions ***\n";
1212
echo "\n-- Testing ftp_ssl_connect() function on failure --\n";
1313
var_dump(ftp_ssl_connect('totes.invalid'));
1414

15-
echo "\n-- Testing ftp_ssl_connect() function timeout warning for value 0 --\n";
16-
ftp_ssl_connect('totes.invalid', 21, 0);
15+
echo "\n-- Testing ftp_ssl_connect() function timeout exception for value 0 --\n";
16+
try {
17+
ftp_ssl_connect('totes.invalid', 21, 0);
18+
} catch (ValueError $exception) {
19+
echo $exception->getMessage() . "\n";
20+
}
1721

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

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

0 commit comments

Comments
 (0)