Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "php_memcached_private.h"
#include "php_memcached_server.h"
#include "g_fmt.h"
#include "zend_interfaces.h"

#include <ctype.h>
#include <limits.h>
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions php_memcached.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down Expand Up @@ -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 {}
Expand Down
8 changes: 7 additions & 1 deletion php_memcached_arginfo.h
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion php_memcached_legacy_arginfo.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/not_serializable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Memcached objects cannot be serialized
--SKIPIF--
<?php
if (!extension_loaded("memcached")) die("skip memcached is not loaded\n");
?>
--FILE--
<?php
try {
serialize(new Memcached());
} catch (Exception $e) {
echo $e->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