Skip to content

Commit

Permalink
Merge pull request #48 from remicollet/issue-php73
Browse files Browse the repository at this point in the history
Fix for PHP 7.3 and Array/Object recursion protection
  • Loading branch information
jbboehr committed Jun 29, 2018
2 parents 7b398ca + a6b22a3 commit 14fc707
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ config.status
config.sub
configure
configure.in
configure.ac
install-sh
libtool
ltmain.sh
Expand Down
43 changes: 43 additions & 0 deletions mustache_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,19 @@ static zend_always_inline void mustache_data_from_array_zval(mustache::Data * no

data_hash = HASH_OF(current);

#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}

data_count = zend_hash_num_elements(data_hash);
Expand Down Expand Up @@ -309,8 +318,13 @@ static zend_always_inline void mustache_data_from_array_zval(mustache::Data * no
}
} ZEND_HASH_FOREACH_END();

#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
#endif
Expand Down Expand Up @@ -417,12 +431,22 @@ static zend_always_inline void mustache_data_from_object_properties_zval(mustach
data_hash = Z_OBJ_HT_P(current)->get_properties(current TSRMLS_CC);
}
if( data_hash != NULL && zend_hash_num_elements(data_hash) > 0 ) {
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}


ZEND_HASH_FOREACH_KEY_VAL_IND(data_hash, key_nindex, key, data_entry) {
(void)key_nindex; /* avoid [-Wunused-but-set-variable] */
if( key && ZSTR_LEN(key) && ZSTR_VAL(key)[0] ) { // skip private/protected
Expand Down Expand Up @@ -451,8 +475,13 @@ static zend_always_inline void mustache_data_from_object_properties_zval(mustach
}
} ZEND_HASH_FOREACH_END();

#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
}
Expand Down Expand Up @@ -519,10 +548,19 @@ static zend_always_inline void mustache_data_from_object_functions_zval(mustache
data_hash = &ce->function_table;
}
if( data_hash != NULL && zend_hash_num_elements(data_hash) > 0 ) {
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}

ZEND_HASH_FOREACH_KEY_VAL_IND(data_hash, key_nindex, key, data_entry) {
Expand All @@ -542,8 +580,13 @@ static zend_always_inline void mustache_data_from_object_functions_zval(mustache
}
} ZEND_HASH_FOREACH_END();

#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
}
Expand Down

0 comments on commit 14fc707

Please sign in to comment.