Skip to content

Commit

Permalink
Don't leak attributes on internal classes
Browse files Browse the repository at this point in the history
Also add zend_hash_release() API to complement zend_array_release(),
because the latter is specific to non-persistent zval arrays.
  • Loading branch information
nikic committed Jun 5, 2020
1 parent 8a7756c commit d3eeeb6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Zend/zend_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ static zend_always_inline void zend_hash_iterators_update(HashTable *ht, HashPos
}
}

/* For regular arrays (non-persistent, storing zvals). */
static zend_always_inline void zend_array_release(zend_array *array)
{
if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
Expand All @@ -333,6 +334,17 @@ static zend_always_inline void zend_array_release(zend_array *array)
}
}

/* For general hashes (possibly persistent, storing any kind of value). */
static zend_always_inline void zend_hash_release(zend_array *array)
{
if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
if (GC_DELREF(array) == 0) {
zend_hash_destroy(array);
pefree(array, GC_FLAGS(array) & IS_ARRAY_PERSISTENT);
}
}
}

END_EXTERN_C()

#define ZEND_INIT_SYMTABLE(ht) \
Expand Down
13 changes: 8 additions & 5 deletions Zend/zend_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ ZEND_API void destroy_zend_class(zval *zv)
zend_string_release_ex(prop_info->doc_comment, 0);
}
if (prop_info->attributes) {
zend_array_release(prop_info->attributes);
zend_hash_release(prop_info->attributes);
}
zend_type_release(prop_info->type, /* persistent */ 0);
}
Expand All @@ -337,7 +337,7 @@ ZEND_API void destroy_zend_class(zval *zv)
zend_string_release_ex(c->doc_comment, 0);
}
if (c->attributes) {
zend_array_release(c->attributes);
zend_hash_release(c->attributes);
}
}
} ZEND_HASH_FOREACH_END();
Expand All @@ -358,7 +358,7 @@ ZEND_API void destroy_zend_class(zval *zv)
zend_string_release_ex(ce->info.user.doc_comment, 0);
}
if (ce->attributes) {
zend_array_release(ce->attributes);
zend_hash_release(ce->attributes);
}

if (ce->num_traits > 0) {
Expand Down Expand Up @@ -412,7 +412,7 @@ ZEND_API void destroy_zend_class(zval *zv)
zend_string_release_ex(c->doc_comment, 1);
}
if (c->attributes) {
zend_array_release(c->attributes);
zend_hash_release(c->attributes);
}
}
free(c);
Expand All @@ -428,6 +428,9 @@ ZEND_API void destroy_zend_class(zval *zv)
if (ce->properties_info_table) {
free(ce->properties_info_table);
}
if (ce->attributes) {
zend_hash_release(ce->attributes);
}
free(ce);
break;
}
Expand Down Expand Up @@ -497,7 +500,7 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
zend_string_release_ex(op_array->doc_comment, 0);
}
if (op_array->attributes) {
zend_array_release(op_array->attributes);
zend_hash_release(op_array->attributes);
}
if (op_array->live_range) {
efree(op_array->live_range);
Expand Down

0 comments on commit d3eeeb6

Please sign in to comment.