Skip to content
Merged
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
27 changes: 3 additions & 24 deletions msgpack_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,10 @@ int msgpack_unserialize_map_item(msgpack_unpack_data *unpack, zval **container,
}

/* found Enum does not contain specified case */
zend_class_constant *constant_ptr = zend_hash_find_ptr(
&ce->constants_table,
zend_class_constant *constant = zend_hash_find_ptr(
CE_CONSTANTS_TABLE(ce),
Z_STR_P(val));
if (constant_ptr == NULL) {
if (constant == NULL || !(ZEND_CLASS_CONST_FLAGS(constant) & ZEND_CLASS_CONST_IS_CASE)) {
MSGPACK_WARNING(
"[msgpack] (%s) Enum case %s does not exist in Enum %s",
__FUNCTION__, Z_STRVAL_P(val), ZSTR_VAL(ce->name));
Expand All @@ -735,27 +735,6 @@ int msgpack_unserialize_map_item(msgpack_unpack_data *unpack, zval **container,
return 0;
}

/* found Enum property is not a case but a constant */
zval *constant = &constant_ptr->value;
if (Z_TYPE_P(constant) == IS_OBJECT) {
zend_object *obj = Z_OBJ_P(constant);
if (!instanceof_function(obj->ce, ce)) {
MSGPACK_WARNING(
"[msgpack] (%s) %s::%s is not an Enum case but a constant",
__FUNCTION__, ZSTR_VAL(ce->name), Z_STRVAL_P(val));

MSGPACK_UNSERIALIZE_FINISH_MAP_ITEM(unpack, key, val);
return 0;
}
} else {
MSGPACK_WARNING(
"[msgpack] (%s) %s::%s is not an Enum case but a constant",
__FUNCTION__, ZSTR_VAL(ce->name), Z_STRVAL_P(val));

MSGPACK_UNSERIALIZE_FINISH_MAP_ITEM(unpack, key, val);
return 0;
}

zend_object *enum_instance = zend_enum_get_case(ce, Z_STR_P(val));
ZVAL_OBJ_COPY(*container, enum_instance);
#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/issue186.2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ OK
--EXPECTF--
Test

Warning: [msgpack] (msgpack_unserialize_map_item) TestEnum::B is not an Enum case but a constant in %s/issue186.2.php on line 11
Warning: [msgpack] (msgpack_unserialize_map_item) Enum case B does not exist in Enum TestEnum in %s/issue186.2.php on line 11
OK
2 changes: 1 addition & 1 deletion tests/issue186.3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ OK
--EXPECTF--
Test

Warning: [msgpack] (msgpack_unserialize_map_item) TestEnum::B is not an Enum case but a constant in %s/issue186.3.php on line 15
Warning: [msgpack] (msgpack_unserialize_map_item) Enum case B does not exist in Enum TestEnum in %s/issue186.3.php on line 15
OK
27 changes: 27 additions & 0 deletions tests/issue186.4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Issue #182 (unknown enum case)
--SKIPIF--
<?php
if (!extension_loaded("msgpack")) {
exit('skip because msgpack extension is missing');
}
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
exit('skip Enum tests in PHP older than 8.1.0');
}
?>
--FILE--
Test
<?php
enum TestEnum: string
{
case A = 'a';
case B = 'b';
}

$data = file_get_contents(__DIR__.'/issue186.ser.txt');
$unserilized = msgpack_unserialize($data);
?>
OK
--EXPECTF--
Test
OK
Loading