diff --git a/php_memcached.c b/php_memcached.c index 47ed2d33..ac657521 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -23,6 +23,7 @@ #include "php_memcached_private.h" #include "php_memcached_server.h" #include "g_fmt.h" +#include "zend_interfaces.h" #include #include @@ -4412,6 +4413,10 @@ PHP_MINIT_FUNCTION(memcached) memcached_ce = register_class_Memcached(); memcached_ce->create_object = php_memc_object_new; +#if PHP_VERSION_ID < 80100 + memcached_ce->serialize = zend_class_serialize_deny; + memcached_ce->unserialize = zend_class_unserialize_deny; +#endif #ifdef HAVE_MEMCACHED_PROTOCOL memcpy(&memcached_server_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); @@ -4421,6 +4426,10 @@ PHP_MINIT_FUNCTION(memcached) memcached_server_ce = register_class_MemcachedServer(); memcached_server_ce->create_object = php_memc_server_new; +#if PHP_VERSION_ID < 80100 + memcached_server_ce->serialize = zend_class_serialize_deny; + memcached_server_ce->unserialize = zend_class_unserialize_deny; +#endif #endif INIT_CLASS_ENTRY(ce, "MemcachedException", NULL); diff --git a/php_memcached.stub.php b/php_memcached.stub.php index 7e85ef32..2b254a3f 100644 --- a/php_memcached.stub.php +++ b/php_memcached.stub.php @@ -6,6 +6,7 @@ * @generate-class-entries */ +/** @not-serializable */ class Memcached { public function __construct(?string $persistent_id=null, ?callable $callback=null, ?string $connection_str=null) {} @@ -87,6 +88,7 @@ public function checkKey(string $key): bool {} } #if defined(HAVE_MEMCACHED_PROTOCOL) +/** @not-serializable */ class MemcachedServer { public function run(string $address): bool {} diff --git a/php_memcached_arginfo.h b/php_memcached_arginfo.h index e8b999de..6971c5bf 100644 --- a/php_memcached_arginfo.h +++ b/php_memcached_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 75604abd7f58655a9ebda6f0ea579840311c1f08 */ + * Stub hash: eb71af72c27ed853607afce6fd7519919f014217 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Memcached___construct, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, persistent_id, IS_STRING, 1, "null") @@ -420,6 +420,9 @@ static zend_class_entry *register_class_Memcached(void) INIT_CLASS_ENTRY(ce, "Memcached", class_Memcached_methods); class_entry = zend_register_internal_class_ex(&ce, NULL); +#if (PHP_VERSION_ID >= 80100) + class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; +#endif #if (PHP_VERSION_ID >= 80200) #if defined(HAVE_MEMCACHED_SASL) @@ -438,6 +441,9 @@ static zend_class_entry *register_class_MemcachedServer(void) INIT_CLASS_ENTRY(ce, "MemcachedServer", class_MemcachedServer_methods); class_entry = zend_register_internal_class_ex(&ce, NULL); +#if (PHP_VERSION_ID >= 80100) + class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; +#endif return class_entry; } diff --git a/php_memcached_legacy_arginfo.h b/php_memcached_legacy_arginfo.h index 66d5bb91..5b3928da 100644 --- a/php_memcached_legacy_arginfo.h +++ b/php_memcached_legacy_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 75604abd7f58655a9ebda6f0ea579840311c1f08 */ + * Stub hash: eb71af72c27ed853607afce6fd7519919f014217 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Memcached___construct, 0, 0, 0) ZEND_ARG_INFO(0, persistent_id) @@ -416,6 +416,9 @@ static zend_class_entry *register_class_Memcached(void) INIT_CLASS_ENTRY(ce, "Memcached", class_Memcached_methods); class_entry = zend_register_internal_class_ex(&ce, NULL); +#if (PHP_VERSION_ID >= 80100) + class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; +#endif return class_entry; } @@ -427,6 +430,9 @@ static zend_class_entry *register_class_MemcachedServer(void) INIT_CLASS_ENTRY(ce, "MemcachedServer", class_MemcachedServer_methods); class_entry = zend_register_internal_class_ex(&ce, NULL); +#if (PHP_VERSION_ID >= 80100) + class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; +#endif return class_entry; } diff --git a/tests/not_serializable.phpt b/tests/not_serializable.phpt new file mode 100644 index 00000000..f08ff22b --- /dev/null +++ b/tests/not_serializable.phpt @@ -0,0 +1,23 @@ +--TEST-- +Memcached objects cannot be serialized +--SKIPIF-- + +--FILE-- +getMessage(), PHP_EOL; +} + +try { + // Before PHP 8.1, only the C: format reaches the handler that throws + unserialize((PHP_VERSION_ID < 80100 ? 'C' : 'O') . ':9:"Memcached":0:{}'); +} catch (Exception $e) { + echo $e->getMessage(), PHP_EOL; +} +--EXPECT-- +Serialization of 'Memcached' is not allowed +Unserialization of 'Memcached' is not allowed