Skip to content

Commit

Permalink
Fix an unserialize-related warning
Browse files Browse the repository at this point in the history
This should fix `Warning: unserialize(): Extra data starting at offset 8 of 72 bytes in unserialize.php on line 4`.
On the flip side, it's not longer possible in PHP8.3 and above, when using
Snuffleupagus, to have other extensions hooking unserialize().
  • Loading branch information
jvoisin committed Jun 25, 2023
1 parent 709d850 commit 78668b6
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/sp_unserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ static zend_string *sp_do_hash_hmac_sha256(char* restrict data, size_t data_len,
return hex_digest;
}

// ------------------

PHP_FUNCTION(sp_serialize) {
zif_handler orig_handler;

Expand Down Expand Up @@ -130,11 +128,16 @@ PHP_FUNCTION(sp_unserialize) {
}
} else { status = 1; }

zif_handler orig_handler;
zif_handler orig_handler = zend_hash_str_find_ptr(SPG(sp_internal_functions_hook), ZEND_STRL("unserialize"));
if (0 == status) {
if ((orig_handler = zend_hash_str_find_ptr(SPG(sp_internal_functions_hook), ZEND_STRL("unserialize")))) {
#if PHP_VERSION_ID >= 80300
// PHP8.3 gives a warning about trailing data in unserialize strings.
php_unserialize_with_options(return_value, buf, buf_len - 64, opts, "unserialize");
#else
if ((orig_handler)) {
orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
#endif
} else {
const sp_config_unserialize *config_unserialize = &(SPCFG(unserialize));
if (config_unserialize->dump) {
Expand All @@ -143,9 +146,14 @@ PHP_FUNCTION(sp_unserialize) {
}
if (true == config_unserialize->simulation) {
sp_log_simulation("unserialize", "Invalid HMAC for %s", serialized_str);
if ((orig_handler = zend_hash_str_find_ptr(SPG(sp_internal_functions_hook), ZEND_STRL("unserialize")))) {
#if PHP_VERSION_ID >= 80300
// PHP8.3 gives a warning about trailing data in unserialize strings.
php_unserialize_with_options(return_value, buf, buf_len - 64, opts, "unserialize");
#else
if ((orig_handler)) {
orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
#endif
} else {
sp_log_drop("unserialize", "Invalid HMAC for %s", serialized_str);
}
Expand Down

0 comments on commit 78668b6

Please sign in to comment.