summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAyesh Karunaratne <ayesh@ayesh.me>2020-12-23 02:58:51 +0700
committerNikita Popov <nikita.ppv@gmail.com>2020-12-23 09:54:38 +0100
commit012439b78e5dbb41ae90fbdf09ab1d8da50bb9fc (patch)
tree71afb849ff4b9bab32f82d49e62f7fc6b97094f4
parentc6a8f201b15821f16d720664208a13795245ceee (diff)
downloadphp-git-012439b78e5dbb41ae90fbdf09ab1d8da50bb9fc.tar.gz
FTP: Disallow direct `FTPConnection` construction
Similar to other resource to object migrations, `FTPConnection` class is not allowed to be constructed with `new FTPConnection`. Related to b4503fbf882e490f16d85915e83173bd1e414e84. Closes GH-6533.
-rw-r--r--ext/ftp/php_ftp.c6
-rw-r--r--ext/ftp/tests/ftp_constructor.phpt15
2 files changed, 21 insertions, 0 deletions
diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c
index 30bffbdd8c..88a3cd02e7 100644
--- a/ext/ftp/php_ftp.c
+++ b/ext/ftp/php_ftp.c
@@ -82,6 +82,11 @@ static zend_object* ftp_object_create(zend_class_entry* ce) {
return zobj;
}
+static zend_function *ftp_object_get_constructor(zend_object *zobj) {
+ zend_throw_error(NULL, "Cannot directly construct FTPConnection, use ftp_connect() or ftp_ssl_connect() instead");
+ return NULL;
+}
+
static void ftp_object_destroy(zend_object *zobj) {
php_ftp_object *obj = ftp_object_from_zend_object(zobj);
@@ -114,6 +119,7 @@ PHP_MINIT_FUNCTION(ftp)
memcpy(&ftp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
ftp_object_handlers.offset = XtOffsetOf(php_ftp_object, std);
+ ftp_object_handlers.get_constructor = ftp_object_get_constructor;
ftp_object_handlers.dtor_obj = ftp_object_destroy;
ftp_object_handlers.clone_obj = NULL;
diff --git a/ext/ftp/tests/ftp_constructor.phpt b/ext/ftp/tests/ftp_constructor.phpt
new file mode 100644
index 0000000000..881677f6a5
--- /dev/null
+++ b/ext/ftp/tests/ftp_constructor.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Attempt to instantiate an FTPConnection directly
+--SKIPIF--
+<?php
+require 'skipif.inc';
+--FILE--
+<?php
+
+try {
+ new FTPConnection();
+} catch (Error $ex) {
+ echo "Exception: ", $ex->getMessage(), "\n";
+}
+--EXPECT--
+Exception: Cannot directly construct FTPConnection, use ftp_connect() or ftp_ssl_connect() instead