Skip to content

Commit f669767

Browse files
committed
Forbid Serializable for enums
1 parent 624f37a commit f669767

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Enum must not implement Serializable
3+
--FILE--
4+
<?php
5+
6+
enum Foo implements Serializable {
7+
case Bar;
8+
9+
public function serialize() {
10+
return serialize('Hello');
11+
}
12+
13+
public function unserialize($data) {
14+
return unserialize($data);
15+
}
16+
}
17+
18+
var_dump(unserialize(serialize(Foo::Bar)));
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: Enums may not implement the Serializable interface in %s on line %d

Zend/zend_enum.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,24 @@ static void zend_verify_enum_magic_methods(zend_class_entry *ce)
103103
}
104104
}
105105

106+
static void zend_verify_enum_interfaces(zend_class_entry *ce)
107+
{
108+
for (uint32_t i = 0; i < ce->num_interfaces; i++) {
109+
zend_string *interface_name = ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES
110+
? ce->interfaces[i]->name
111+
: ce->interface_names[i].lc_name;
112+
113+
if (zend_string_equals_literal(interface_name, "serializable")) {
114+
zend_error_noreturn(E_COMPILE_ERROR, "Enums may not implement the Serializable interface");
115+
}
116+
}
117+
}
118+
106119
void zend_verify_enum(zend_class_entry *ce)
107120
{
108121
zend_verify_enum_properties(ce);
109122
zend_verify_enum_magic_methods(ce);
123+
zend_verify_enum_interfaces(ce);
110124
}
111125

112126
static zval *zend_enum_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
@@ -206,6 +220,8 @@ void zend_enum_add_interfaces(zend_class_entry *ce)
206220
ce->num_interfaces++;
207221
}
208222

223+
ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_LINKED));
224+
209225
ce->interface_names = erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
210226

211227
ce->interface_names[num_interfaces_before].name = zend_string_init("UnitEnum", sizeof("UnitEnum") - 1, 0);

0 commit comments

Comments
 (0)