Skip to content

PHPC-1471: Clean up reference handling in Javascript scope serialisation #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ zend_bool phongo_writeconcernerror_init(zval* return_value, bson_t* bson TSRMLS_
} \
} while (0);

#define PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_debug, props) \
do { \
if (is_debug) { \
zend_hash_destroy((props)); \
FREE_HASHTABLE(props); \
} \
} while (0);

#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME(zv) (Z_TYPE(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE(zv)->name) : zend_get_type_by_const(Z_TYPE(zv)))
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zvp) PHONGO_ZVAL_CLASS_OR_TYPE_NAME(*(zvp))

Expand Down
52 changes: 21 additions & 31 deletions src/BSON/Javascript.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,16 @@ static PHP_METHOD(Javascript, jsonSerialize)
php_phongo_bson_state state;

PHONGO_BSON_INIT_STATE(state);
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
zval_ptr_dtor(&state.zchild);
return;
}

#if PHP_VERSION_ID >= 70000
Z_ADDREF(state.zchild);
ADD_ASSOC_ZVAL_EX(return_value, "$scope", &state.zchild);
ADD_ASSOC_ZVAL_EX(return_value, "$scope", &state.zchild);
#else
Z_ADDREF_P(state.zchild);
ADD_ASSOC_ZVAL_EX(return_value, "$scope", state.zchild);
ADD_ASSOC_ZVAL_EX(return_value, "$scope", state.zchild);
#endif
}

zval_ptr_dtor(&state.zchild);
}
} /* }}} */

Expand All @@ -250,22 +249,21 @@ static PHP_METHOD(Javascript, serialize)
#if PHP_VERSION_ID >= 70000
if (intern->scope && intern->scope->len) {
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
zval_ptr_dtor(&state.zchild);
return;
}
Z_ADDREF(state.zchild);
} else {
ZVAL_NULL(&state.zchild);
}
#else
if (intern->scope && intern->scope->len) {
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
zval_ptr_dtor(&state.zchild);
return;
}
Z_ADDREF_P(state.zchild);
} else {
MAKE_STD_ZVAL(state.zchild);
ZVAL_NULL(state.zchild);
Z_ADDREF_P(state.zchild);
}
#endif

Expand All @@ -289,7 +287,6 @@ static PHP_METHOD(Javascript, serialize)

smart_str_free(&buf);
zval_ptr_dtor(&retval);
zval_ptr_dtor(&state.zchild);
} /* }}} */

/* {{{ proto void MongoDB\BSON\Javascript::unserialize(string $serialized)
Expand Down Expand Up @@ -490,17 +487,12 @@ HashTable* php_phongo_javascript_get_properties_hash(zval* object, bool is_debug
php_phongo_bson_state state;

PHONGO_BSON_INIT_STATE(state);
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
Z_ADDREF(state.zchild);
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &state.zchild);
} else {
zval scope;

ZVAL_NULL(&scope);
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &scope);
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
zval_ptr_dtor(&state.zchild);
goto failure;
}

zval_ptr_dtor(&state.zchild);
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &state.zchild);
} else {
zval scope;

Expand All @@ -520,18 +512,12 @@ HashTable* php_phongo_javascript_get_properties_hash(zval* object, bool is_debug
php_phongo_bson_state state;

PHONGO_BSON_INIT_STATE(state);
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
Z_ADDREF_P(state.zchild);
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
} else {
zval* scope;

MAKE_STD_ZVAL(scope);
ZVAL_NULL(scope);
zend_hash_update(props, "scope", sizeof("scope"), &scope, sizeof(scope), NULL);
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
zval_ptr_dtor(&state.zchild);
goto failure;
}

zval_ptr_dtor(&state.zchild);
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
} else {
zval* scope;

Expand All @@ -543,6 +529,10 @@ HashTable* php_phongo_javascript_get_properties_hash(zval* object, bool is_debug
#endif

return props;

failure:
PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_debug, props);
return NULL;
} /* }}} */

static HashTable* php_phongo_javascript_get_debug_info(zval* object, int* is_temp TSRMLS_DC) /* {{{ */
Expand Down