Skip to content

Commit 6f908a0

Browse files
committed
Check Serialization magic methods structure
Closes GH-5441
1 parent 5bf01fc commit 6f908a0

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
__serialize declaration
3+
--FILE--
4+
<?php
5+
class Foo {
6+
static function __serialize($arguments) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Warning: The magic method Foo::__serialize() must have public visibility and cannot be static in %s on line %d
11+
12+
Fatal error: Method Foo::__serialize() cannot take arguments in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
__unserialize declaration
3+
--FILE--
4+
<?php
5+
class Foo {
6+
static function __unserialize($data, $value) {}
7+
}
8+
?>
9+
--EXPECTF--
10+
Warning: The magic method Foo::__unserialize() must have public visibility and cannot be static in %s on line %d
11+
12+
Fatal error: Method Foo::__unserialize() must take exactly 1 argument in %s on line %d

Zend/zend_API.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,18 @@ ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce,
20232023
} else if (name_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1 &&
20242024
!memcmp(lcname, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && fptr->common.num_args != 0) {
20252025
zend_error(error_type, "Method %s::__debugInfo() cannot take arguments", ZSTR_VAL(ce->name));
2026+
} else if (
2027+
name_len == sizeof("__serialize") - 1
2028+
&& !memcmp(lcname, "__serialize", sizeof("__serialize") - 1)
2029+
&& fptr->common.num_args != 0
2030+
) {
2031+
zend_error(error_type, "Method %s::__serialize() cannot take arguments", ZSTR_VAL(ce->name));
2032+
} else if (
2033+
name_len == sizeof("__unserialize") - 1
2034+
&& !memcmp(lcname, "__unserialize", sizeof("__unserialize") - 1)
2035+
&& fptr->common.num_args != 1
2036+
) {
2037+
zend_error(error_type, "Method %s::__unserialize() must take exactly 1 argument", ZSTR_VAL(ce->name));
20262038
}
20272039
}
20282040
/* }}} */

Zend/zend_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6171,6 +6171,10 @@ void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_boo
61716171
} else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
61726172
zend_check_magic_method_attr(fn_flags, ce, "__debugInfo", 0);
61736173
ce->__debugInfo = (zend_function *) op_array;
6174+
} else if (zend_string_equals_literal(lcname, "__serialize")) {
6175+
zend_check_magic_method_attr(fn_flags, ce, "__serialize", 0);
6176+
} else if (zend_string_equals_literal(lcname, "__unserialize")) {
6177+
zend_check_magic_method_attr(fn_flags, ce, "__unserialize", 0);
61746178
}
61756179

61766180
zend_string_release_ex(lcname, 0);

0 commit comments

Comments
 (0)