Skip to content

Commit

Permalink
New safe_unserialize() utility function
Browse files Browse the repository at this point in the history
When given invalid data, unserialize() throws a PHP notice; this
function relies on error_convert_to_exception() custom error handler to
throw an Exception instead.
  • Loading branch information
dregad committed Jun 20, 2018
1 parent b3b5cc4 commit 21d1375
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/utility_api.php
Expand Up @@ -295,3 +295,27 @@ function get_font_path() {
}
return $t_font_path;
}

/**
* unserialize() with Exception instead of PHP notice.
*
* When given invalid data, unserialize() throws a PHP notice; this function
* relies on a custom error handler to throw an Exception instead.
*
* @param string $p_string The serialized string.
* @return mixed The converted value
*
* @throws ErrorException
*/
function safe_unserialize( $p_string ) {
set_error_handler( 'error_convert_to_exception' );
try {
$t_data = unserialize( $p_string );
}
catch( ErrorException $e ) {
restore_error_handler();
throw $e;
}
restore_error_handler();
return $t_data;
}

0 comments on commit 21d1375

Please sign in to comment.