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
19 changes: 17 additions & 2 deletions src/php7/igbinary.c
Original file line number Diff line number Diff line change
Expand Up @@ -3171,7 +3171,11 @@ zend_always_inline static int igbinary_unserialize_object(struct igbinary_unseri
zval user_func;
zval retval;
zval args[1];
#if PHP_VERSION_ID >= 80600
zend_string *user_func_name;
#else
const char* user_func_name;
#endif

/* Try to find class directly */
if (EXPECTED((ce = zend_lookup_class(class_name)) != NULL)) {
Expand All @@ -3181,14 +3185,22 @@ zend_always_inline static int igbinary_unserialize_object(struct igbinary_unseri

user_func_name = PG(unserialize_callback_func);
/* Check for unserialize callback */
#if PHP_VERSION_ID >= 80600
if ((user_func_name == NULL) || (ZSTR_LEN(user_func_name) == 0)) {
#else
if ((user_func_name == NULL) || (user_func_name[0] == '\0')) {
#endif
incomplete_class = 1;
ce = PHP_IC_ENTRY;
break;
}

/* Call unserialize callback */
#if PHP_VERSION_ID >= 80600
ZVAL_STR(&user_func, zend_string_dup(user_func_name, false));
#else
ZVAL_STRING(&user_func, user_func_name);
#endif
ZVAL_STR(&args[0], class_name);
if (call_user_function(CG(function_table), NULL, &user_func, &retval, 1, args) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "defined (%s) but not found", Z_STRVAL(user_func));
Expand All @@ -3199,21 +3211,24 @@ zend_always_inline static int igbinary_unserialize_object(struct igbinary_unseri
}
/* FIXME: always safe? */
zval_ptr_dtor(&retval);
zval_ptr_dtor_str(&user_func);

/* User function call may have raised an exception */
if (EG(exception)) {
zend_string_release_ex(class_name, 0);
zval_ptr_dtor_str(&user_func);
return 1;
}

/* The callback function may have defined the class */
ce = zend_lookup_class(class_name);
if (!ce) {
php_error_docref(NULL, E_WARNING, "Function %s() hasn't defined the class it was called for", PG(unserialize_callback_func));
/* Keep using our own copy of the name: the callback may have
* changed unserialize_callback_func, releasing the ini string */
php_error_docref(NULL, E_WARNING, "Function %s() hasn't defined the class it was called for", Z_STRVAL(user_func));
incomplete_class = true;
ce = PHP_IC_ENTRY;
}
zval_ptr_dtor_str(&user_func);
} while (0);

if (IGBINARY_IS_NOT_UNSERIALIZABLE(ce)) {
Expand Down
38 changes: 38 additions & 0 deletions tests/igbinary_102.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Object test, unserialize_callback_func (empty value, callback changing the setting)
--SKIPIF--
<?php if (PHP_VERSION_ID < 80600) echo "skip requires php >= 8.6\n"; ?>
--INI--
error_reporting=E_ALL
unserialize_callback_func=
--FILE--
<?php
if(!extension_loaded('igbinary')) {
dl('igbinary.' . PHP_SHLIB_SUFFIX);
}

// TODO undo workaround when #[AllowDynamicProperties] is added to __PHP_Incomplete_Class
error_reporting(E_ALL & ~E_DEPRECATED);

// An empty unserialize_callback_func means "no callback", it must not be
// called as if it were a function name.
var_dump(ini_get('unserialize_callback_func'));
var_dump(get_class(igbinary_unserialize(pack('H*', '0000000217034f626a140211016106011101620602'))));

// The callback is free to change unserialize_callback_func while it runs.
// The warning has to name the callback that was actually called.
function reassigning_autoload($classname) {
echo "Called for $classname\n";
ini_set('unserialize_callback_func', 'some_other_callback');
}

ini_set('unserialize_callback_func', 'reassigning_autoload');
var_dump(get_class(igbinary_unserialize(pack('H*', '0000000217034f626b140211016106011101620602'))));
?>
--EXPECTF--
string(0) ""
string(22) "__PHP_Incomplete_Class"
Called for Obk

Warning: igbinary_unserialize(): Function reassigning_autoload() hasn't defined the class it was called for in %sigbinary_102.php on line %d
string(22) "__PHP_Incomplete_Class"