Skip to content

Commit

Permalink
Emit deprecation notice when serializing resources
Browse files Browse the repository at this point in the history
PHP itself is converting many resources to objects that throw an Error on
serialization attempts starting in php 8.0.
Continue to represent resources as null in the serialized data.

Fixes #300
  • Loading branch information
TysonAndre committed Nov 23, 2020
1 parent 78217db commit 15a7961
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .azure/job.yml
Expand Up @@ -13,8 +13,6 @@ jobs:
vmImage: ${{ parameters.vmImage }}
steps:
# E.g. ondrej/php sometimes updates the latest version
- script: |
displayName: 'Refresh apt-get'
- script: |
sudo apt-get install -y valgrind
displayName: 'Install valgrind'
Expand Down
3 changes: 3 additions & 0 deletions package.xml
Expand Up @@ -44,6 +44,9 @@
<license uri="https://github.com/igbinary/igbinary/blob/master/COPYING">BSD-3-Clause</license>
<notes>
* Improve memory efficiency when unserializing packed arrays (e.g. lists) - certain checks are no longer needed.
* Emit a deprecation notice when serializing resources.
PHP itself is converting many resources to objects that throw an Error on serialization attempts.
Continue to represent resources as null in the serialized data.
</notes>
<contents>
<dir name="/">
Expand Down
14 changes: 14 additions & 0 deletions src/php7/igbinary.c
Expand Up @@ -1818,6 +1818,17 @@ inline static int igbinary_serialize_object(struct igbinary_serialize_data *igsd
}
}
/* }}} */
/* {{{ igbinary_warn_serialize_resource */
static ZEND_COLD int igbinary_warn_serialize_resource(zval *z) {
const char *resource_type;
resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z));
if (!resource_type) {
resource_type = "Unknown";
}
php_error_docref(NULL, E_DEPRECATED, "Cannot serialize resource(%s) and resources may be converted to objects that cannot be serialized in future php releases. Serializing the value as null instead", resource_type);
return EG(exception) != NULL;
}
/* }}} */
/* {{{ igbinary_serialize_zval */
/** Serialize zval. */
static int igbinary_serialize_zval(struct igbinary_serialize_data *igsd, zval *z) {
Expand All @@ -1843,6 +1854,9 @@ static int igbinary_serialize_zval(struct igbinary_serialize_data *igsd, zval *z
}
switch (Z_TYPE_P(z)) {
case IS_RESOURCE:
if (igbinary_warn_serialize_resource(z)) {
return 1;
}
return igbinary_serialize_null(igsd);
case IS_OBJECT:
return igbinary_serialize_object(igsd, z);
Expand Down
2 changes: 1 addition & 1 deletion src/php7/igbinary.h
Expand Up @@ -18,7 +18,7 @@ struct zval;
/** Binary protocol version of igbinary. */
#define IGBINARY_FORMAT_VERSION 0x00000002

#define PHP_IGBINARY_VERSION "3.1.7dev"
#define PHP_IGBINARY_VERSION "3.2.0dev"

/* Macros */

Expand Down
10 changes: 9 additions & 1 deletion tests/igbinary_023.phpt
Expand Up @@ -22,7 +22,15 @@ test('resource', $res, false);

fclose($res);

--EXPECT--
test('resource', $res, false);

--EXPECTF--
Deprecated: igbinary_serialize(): Cannot serialize resource(stream) and resources may be converted to objects that cannot be serialized in future php releases. Serializing the value as null instead in %sigbinary_023.php on line 4
resource
00
OK

Deprecated: igbinary_serialize(): Cannot serialize resource(Unknown) and resources may be converted to objects that cannot be serialized in future php releases. Serializing the value as null instead in %sigbinary_023.php on line 4
resource
00
OK

0 comments on commit 15a7961

Please sign in to comment.