From a011650a5f0c0414a5a845203b4b2e61f20ea34f Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Wed, 23 Dec 2020 17:10:56 +0700 Subject: [PATCH 1/2] IMAP: Disallow direct `new IMAPConnection()` construct Disallows constructing an `IMAPConnection` class directly with `new IMAPConnection` construct, by throwing an `Error` exception if attempted. `imap_open` is still the only way to create `IMAPConnection` objects. --- ext/imap/php_imap.c | 6 ++++++ ext/imap/tests/imap_constructor.phpt | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 ext/imap/tests/imap_constructor.phpt diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 1d909d60654a7..399b7a516576b 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -170,6 +170,11 @@ static zend_object* imap_object_create(zend_class_entry* ce) { return zobj; } +static zend_function *imap_object_get_constructor(zend_object *zobj) { + zend_throw_error(NULL, "Cannot directly construct IMAPConnection, use imap_open() instead"); + return NULL; +} + static void imap_object_destroy(zend_object *zobj) { php_imap_object *obj = imap_object_from_zend_object(zobj); @@ -483,6 +488,7 @@ PHP_MINIT_FUNCTION(imap) memcpy(&imap_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); imap_object_handlers.offset = XtOffsetOf(php_imap_object, std); + imap_object_handlers.get_constructor = imap_object_get_constructor; imap_object_handlers.dtor_obj = imap_object_destroy; imap_object_handlers.clone_obj = NULL; diff --git a/ext/imap/tests/imap_constructor.phpt b/ext/imap/tests/imap_constructor.phpt new file mode 100644 index 0000000000000..bc5da991d18d5 --- /dev/null +++ b/ext/imap/tests/imap_constructor.phpt @@ -0,0 +1,15 @@ +--TEST-- +Attempt to instantiate an IMAPConnection directly +--SKIPIF-- +getMessage(), "\n"; +} +--EXPECT-- +Exception: Cannot directly construct IMAPConnection, use imap_open() instead From adb338ef3b9a5c8180554e942862fc96f9e85ec0 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Wed, 23 Dec 2020 17:12:34 +0700 Subject: [PATCH 2/2] IMAP: Declare `IMAPConnection` class as `final` (stub+test) Updates the `IMAPConnection` class stub to make sure it has the `final` flag, and adds a test to verify it. --- ext/imap/php_imap.stub.php | 2 +- ext/imap/tests/imap_final.phpt | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 ext/imap/tests/imap_final.phpt diff --git a/ext/imap/php_imap.stub.php b/ext/imap/php_imap.stub.php index 0c597badecf98..580c79cc9bc1e 100644 --- a/ext/imap/php_imap.stub.php +++ b/ext/imap/php_imap.stub.php @@ -1,7 +1,7 @@