]> granicus.if.org Git - php/commitdiff
Promote warnings to exceptions in ext/ftp
authorMáté Kocsis <kocsismate@woohoolabs.com>
Sun, 30 Aug 2020 21:53:00 +0000 (23:53 +0200)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Mon, 31 Aug 2020 10:49:17 +0000 (12:49 +0200)
Closes GH-6054

ext/ftp/ftp.stub.php
ext/ftp/ftp_arginfo.h
ext/ftp/php_ftp.c
ext/ftp/tests/004.phpt
ext/ftp/tests/005.phpt
ext/ftp/tests/ftp_get_option.phpt
ext/ftp/tests/ftp_set_option_errors.phpt
ext/ftp/tests/ftp_ssl_connect_error.phpt

index afcd2064d6e7652a55bb9668f1921164e893f2c1..17b047399572aad2e2cf7a776aaf0a12718c343f 100644 (file)
@@ -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 {}
@@ -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 {}
index 45cd0e5cf3ad390bbfd41f600f8b564cdeee48d9..b5f160f74c65a522aabd5c86cc872d07ef80fcfc 100644 (file)
@@ -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)
@@ -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)
@@ -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)
index b27fbef0128c34a6a221c24fd4b9c77e34481ff1..0a185ef27294f1b6686d92a33f67d0ac66d328d6 100644 (file)
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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);
        }
 
@@ -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;
        }
 }
@@ -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();
        }
 }
 /* }}} */
index b95c12c518d380d112bb7b1e95f7bfcbfa486395..e844f024ff35082b43081fa19f71668929b70082 100644 (file)
@@ -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");
@@ -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
index 4b46321e26bde8f3f47e761989c66956ff85ee1c..c1c749d08cdaba576a14f19e6154172deff0e407 100644 (file)
@@ -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, ''));
@@ -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)
index 06b34fc72051a56970cc1031fcabc4fcb7ea1dc9..336243d64ba75546eb0bf1cbd0ac5fdf14d124ac 100644 (file)
@@ -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
index ad91be56e8a66c0e79480dd5ae85f5bb80f85772..03c387975e0b9e5c1ff3410b49482fd01e47ce16 100644 (file)
@@ -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
index 13c8f9e9c02cb14930a02b11b9218a7e3cd8f409..61646293faac5e2a2cfabb78d22303067805fdec 100644 (file)
@@ -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--
@@ -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===