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
32 changes: 32 additions & 0 deletions Zend/tests/attributes/033_not_serializable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
#[NotSerializable]
--FILE--
<?php

#[NotSerializable]
class C {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will need to be tests about extending a NonSerializable class, which also needs to determine if this works or not if the child class implements __serialize()/__unserialize()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to make it final? Why would anyone extend this attribute class?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attribute class is final. This is about classes that have the attribute applied.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry, my bad. Great.


class D extends C {}

try {
serialize(new C());
} catch (Throwable $ex) {
echo $ex->getMessage() . "\n";
}

try {
var_dump(unserialize('O:1:"C":0:{}'));
} catch (Throwable $ex) {
echo $ex->getMessage() . "\n";
}

try {
serialize(new D());
} catch (Throwable $ex) {
echo $ex->getMessage() . "\n";
}

--EXPECTF--
Serialization of 'C' is not allowed
Unserialization of 'C' is not allowed
Serialization of 'D' is not allowed
23 changes: 23 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ZEND_API zend_class_entry *zend_ce_allow_dynamic_properties;
ZEND_API zend_class_entry *zend_ce_sensitive_parameter;
ZEND_API zend_class_entry *zend_ce_sensitive_parameter_value;
ZEND_API zend_class_entry *zend_ce_override;
ZEND_API zend_class_entry *zend_ce_not_serializable;

static zend_object_handlers attributes_object_handlers_sensitive_parameter_value;

Expand Down Expand Up @@ -80,6 +81,19 @@ static void validate_allow_dynamic_properties(
scope->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
}

static void validate_not_serializable(
zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
if (scope->ce_flags & ZEND_ACC_TRAIT) {
zend_error_noreturn(E_ERROR, "Cannot apply #[NotSerializable] to trait");
}
if (scope->ce_flags & ZEND_ACC_INTERFACE) {
zend_error_noreturn(E_ERROR, "Cannot apply #[NotSerializable] to interface");
}

scope->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
}

ZEND_METHOD(Attribute, __construct)
{
zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL;
Expand Down Expand Up @@ -132,6 +146,11 @@ ZEND_METHOD(SensitiveParameterValue, __debugInfo)
RETURN_EMPTY_ARRAY();
}

ZEND_METHOD(NotSerializable, __construct)
{
ZEND_PARSE_PARAMETERS_NONE();
}

static HashTable *attributes_sensitive_parameter_value_get_properties_for(zend_object *zobj, zend_prop_purpose purpose)
{
return NULL;
Expand Down Expand Up @@ -380,6 +399,10 @@ void zend_register_attribute_ce(void)

zend_ce_override = register_class_Override();
zend_mark_internal_attribute(zend_ce_override);

zend_ce_not_serializable = register_class_NotSerializable();
attr = zend_mark_internal_attribute(zend_ce_not_serializable);
attr->validator = validate_not_serializable;
}

void zend_attributes_shutdown(void)
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern ZEND_API zend_class_entry *zend_ce_allow_dynamic_properties;
extern ZEND_API zend_class_entry *zend_ce_sensitive_parameter;
extern ZEND_API zend_class_entry *zend_ce_sensitive_parameter_value;
extern ZEND_API zend_class_entry *zend_ce_override;
extern ZEND_API zend_class_entry *zend_ce_not_serializable;

typedef struct {
zend_string *name;
Expand Down
9 changes: 9 additions & 0 deletions Zend/zend_attributes.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ final class Override
{
public function __construct() {}
}

/**
* @strict-properties
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class NotSerializable
{
public function __construct() {}
}
29 changes: 28 additions & 1 deletion Zend/zend_attributes_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ext/standard/tests/serialize/ref_to_failed_serialize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ References to objects for which Serializable::serialize() returned NULL should u
--FILE--
<?php

class NotSerializable implements Serializable {
class MyNotSerializable implements Serializable {
public function serialize() {
return null;
}
Expand All @@ -12,7 +12,7 @@ class NotSerializable implements Serializable {
}
}

$obj = new NotSerializable();
$obj = new MyNotSerializable();
$data = [$obj, $obj];
var_dump($s = serialize($data));
var_dump(unserialize($s));
Expand Down