Skip to content

Commit 7f1659a

Browse files
AyeshGirgias
authored andcommitted
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.
1 parent 4050589 commit 7f1659a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

ext/imap/php_imap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ static zend_object* imap_object_create(zend_class_entry* ce) {
170170
return zobj;
171171
}
172172

173+
static zend_function *imap_object_get_constructor(zend_object *zobj) {
174+
zend_throw_error(NULL, "Cannot directly construct IMAPConnection, use imap_open() instead");
175+
return NULL;
176+
}
177+
173178
static void imap_object_destroy(zend_object *zobj) {
174179
php_imap_object *obj = imap_object_from_zend_object(zobj);
175180

@@ -483,6 +488,7 @@ PHP_MINIT_FUNCTION(imap)
483488

484489
memcpy(&imap_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
485490
imap_object_handlers.offset = XtOffsetOf(php_imap_object, std);
491+
imap_object_handlers.get_constructor = imap_object_get_constructor;
486492
imap_object_handlers.dtor_obj = imap_object_destroy;
487493
imap_object_handlers.clone_obj = NULL;
488494

ext/imap/tests/imap_constructor.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Attempt to instantiate an IMAPConnection directly
3+
--SKIPIF--
4+
<?php
5+
extension_loaded('imap') or die('skip imap extension not available in this build');
6+
--FILE--
7+
<?php
8+
9+
try {
10+
new IMAPConnection();
11+
} catch (Error $ex) {
12+
echo "Exception: ", $ex->getMessage(), "\n";
13+
}
14+
--EXPECT--
15+
Exception: Cannot directly construct IMAPConnection, use imap_open() instead

0 commit comments

Comments
 (0)