Skip to content
Draft
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
53 changes: 53 additions & 0 deletions Zend/tests/attributes/no_serialize/001_base.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
#[\NoSerialize]: Basic test.
--FILE--
<?php

class Base {
#[\NoSerialize]
public $b1 = 'a';
protected $b2 = 'b';
private $b3 = 'c';
}

class Child extends Base {
public $b1 = 'b';

function __construct (
#[\NoSerialize]
public $c = 'c',
public $d = 'd',
) {}
}

class Unserializable {
#[\NoSerialize]
public $a;
public $b;
}

$base = new Base;
echo serialize($base), PHP_EOL;
echo serialize(new Child), PHP_EOL;
$base->b1 = 'b';
var_dump(unserialize(serialize($base)));
var_dump(unserialize('O:14:"Unserializable":2:{s:1:"a";s:1:"a";s:1:"b";s:1:"b";}'));

?>
--EXPECTF--
O:4:"Base":2:{s:5:"%0*%0b2";s:1:"b";s:8:"%0Base%0b3";s:1:"c";}
O:5:"Child":4:{s:2:"b1";s:1:"b";s:5:"%0*%0b2";s:1:"b";s:8:"%0Base%0b3";s:1:"c";s:1:"d";s:1:"d";}
object(Base)#%d (3) {
["b1"]=>
string(1) "a"
["b2":protected]=>
string(1) "b"
["b3":"Base":private]=>
string(1) "c"
}
object(Unserializable)#2 (2) {
["a"]=>
string(1) "a"
["b"]=>
string(1) "b"
}
19 changes: 19 additions & 0 deletions Zend/tests/attributes/no_serialize/002-warnings.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
#[\NoSerialize]: Warnings test.
--FILE--
<?php

class Base {
#[\NoSerialize]
static public $b1 = 'a';

#[\NoSerialize]
protected $b2 {
get => 1;
}
}
?>
--EXPECTF--
Warning: Static property Base::$b1 is not serializable in %s on line %d

Warning: Virtual property Base::$b2 is not serializable in %s on line %d
4 changes: 4 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ZEND_API zend_class_entry *zend_ce_override;
ZEND_API zend_class_entry *zend_ce_deprecated;
ZEND_API zend_class_entry *zend_ce_nodiscard;
ZEND_API zend_class_entry *zend_ce_delayed_target_validation;
ZEND_API zend_class_entry *zend_ce_no_serialize;

static zend_object_handlers attributes_object_handlers_sensitive_parameter_value;

Expand Down Expand Up @@ -606,6 +607,9 @@ void zend_register_attribute_ce(void)

zend_ce_delayed_target_validation = register_class_DelayedTargetValidation();
attr = zend_mark_internal_attribute(zend_ce_delayed_target_validation);

zend_ce_no_serialize = register_class_NoSerialize();
zend_mark_internal_attribute(zend_ce_no_serialize);
}

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 @@ -51,6 +51,7 @@ extern ZEND_API zend_class_entry *zend_ce_override;
extern ZEND_API zend_class_entry *zend_ce_deprecated;
extern ZEND_API zend_class_entry *zend_ce_nodiscard;
extern ZEND_API zend_class_entry *zend_ce_delayed_target_validation;
extern ZEND_API zend_class_entry *zend_ce_no_serialize;

typedef struct {
zend_string *name;
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_attributes.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ public function __construct(?string $message = null) {}
*/
#[Attribute(Attribute::TARGET_ALL)]
final class DelayedTargetValidation {}

/**
* @strict-properties
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
final class NoSerialize
{
}
17 changes: 16 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.

25 changes: 25 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8000,6 +8000,16 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
if (override_attribute) {
prop->flags |= ZEND_ACC_OVERRIDE;
}

zend_attribute *no_serialize_attribute = zend_get_attribute_str(prop->attributes, "noserialize", sizeof("noserialize")-1);
if (no_serialize_attribute) {
if (prop->flags & ZEND_ACC_VIRTUAL) {
zend_error(E_COMPILE_WARNING,
"Virtual property %s::$%s is not serializable",
ZSTR_VAL(scope->name), ZSTR_VAL(name));
}
prop->flags |= ZEND_ACC_NO_SERIALIZE;
}
}
}
}
Expand Down Expand Up @@ -8989,6 +8999,21 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
if (override_attribute) {
info->flags |= ZEND_ACC_OVERRIDE;
}

zend_attribute *no_serialize_attribute = zend_get_attribute_str(info->attributes, "noserialize", sizeof("noserialize")-1);
if (no_serialize_attribute) {
if (info->flags & ZEND_ACC_STATIC) {
zend_error(E_COMPILE_WARNING,
"Static property %s::$%s is not serializable",
ZSTR_VAL(ce->name), ZSTR_VAL(name));
}
if (info->flags & ZEND_ACC_VIRTUAL) {
zend_error(E_COMPILE_WARNING,
"Virtual property %s::$%s is not serializable",
ZSTR_VAL(ce->name), ZSTR_VAL(name));
}
info->flags |= ZEND_ACC_NO_SERIALIZE;
}
}

CG(context).active_property_info_name = old_active_property_info_name;
Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ typedef struct _zend_oparray_context {
/* has #[\Override] attribute | | | */
#define ZEND_ACC_OVERRIDE (1 << 28) /* | X | X | */
/* | | | */
/* Property Flags (unused: 13-27,29...) | | | */
/* Property Flags (unused: 13-27,30...) | | | */
/* =========== | | | */
/* | | | */
/* Promoted property / parameter | | | */
Expand All @@ -275,6 +275,8 @@ typedef struct _zend_oparray_context {
#define ZEND_ACC_PROTECTED_SET (1 << 11) /* | | X | */
#define ZEND_ACC_PRIVATE_SET (1 << 12) /* | | X | */
/* | | | */
#define ZEND_ACC_NO_SERIALIZE (1 << 29) /* | | X | */
/* | | | */
/* Class Flags (unused: 31) | | | */
/* =========== | | | */
/* | | | */
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
count = ce->default_properties_count;
for (i = 0; i < ce->default_properties_count; i++) {
prop_info = ce->properties_info_table[i];
if (!prop_info) {
if (!prop_info || prop_info->flags & ZEND_ACC_NO_SERIALIZE) {
count--;
continue;
}
Expand All @@ -1263,7 +1263,7 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
smart_str_appendl(buf, ":{", 2);
for (i = 0; i < ce->default_properties_count; i++) {
prop_info = ce->properties_info_table[i];
if (!prop_info) {
if (!prop_info || prop_info->flags & ZEND_ACC_NO_SERIALIZE) {
continue;
}
prop = OBJ_PROP(obj, prop_info->offset);
Expand Down