Skip to content

Commit 8700b7d

Browse files
committed
Merge pull request #1063
2 parents de20fe2 + 67d8db5 commit 8700b7d

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

php_phongo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ zend_bool phongo_writeconcernerror_init(zval* return_value, bson_t* bson TSRMLS_
198198
} \
199199
} while (0);
200200

201+
#define PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_debug, props) \
202+
do { \
203+
if (is_debug) { \
204+
zend_hash_destroy((props)); \
205+
FREE_HASHTABLE(props); \
206+
} \
207+
} while (0);
208+
201209
#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)))
202210
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zvp) PHONGO_ZVAL_CLASS_OR_TYPE_NAME(*(zvp))
203211

src/BSON/Javascript.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,16 @@ static PHP_METHOD(Javascript, jsonSerialize)
215215
php_phongo_bson_state state;
216216

217217
PHONGO_BSON_INIT_STATE(state);
218-
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
218+
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
219+
zval_ptr_dtor(&state.zchild);
220+
return;
221+
}
222+
219223
#if PHP_VERSION_ID >= 70000
220-
Z_ADDREF(state.zchild);
221-
ADD_ASSOC_ZVAL_EX(return_value, "$scope", &state.zchild);
224+
ADD_ASSOC_ZVAL_EX(return_value, "$scope", &state.zchild);
222225
#else
223-
Z_ADDREF_P(state.zchild);
224-
ADD_ASSOC_ZVAL_EX(return_value, "$scope", state.zchild);
226+
ADD_ASSOC_ZVAL_EX(return_value, "$scope", state.zchild);
225227
#endif
226-
}
227-
228-
zval_ptr_dtor(&state.zchild);
229228
}
230229
} /* }}} */
231230

@@ -250,22 +249,21 @@ static PHP_METHOD(Javascript, serialize)
250249
#if PHP_VERSION_ID >= 70000
251250
if (intern->scope && intern->scope->len) {
252251
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
252+
zval_ptr_dtor(&state.zchild);
253253
return;
254254
}
255-
Z_ADDREF(state.zchild);
256255
} else {
257256
ZVAL_NULL(&state.zchild);
258257
}
259258
#else
260259
if (intern->scope && intern->scope->len) {
261260
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
261+
zval_ptr_dtor(&state.zchild);
262262
return;
263263
}
264-
Z_ADDREF_P(state.zchild);
265264
} else {
266265
MAKE_STD_ZVAL(state.zchild);
267266
ZVAL_NULL(state.zchild);
268-
Z_ADDREF_P(state.zchild);
269267
}
270268
#endif
271269

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

290288
smart_str_free(&buf);
291289
zval_ptr_dtor(&retval);
292-
zval_ptr_dtor(&state.zchild);
293290
} /* }}} */
294291

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

492489
PHONGO_BSON_INIT_STATE(state);
493-
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
494-
Z_ADDREF(state.zchild);
495-
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &state.zchild);
496-
} else {
497-
zval scope;
498-
499-
ZVAL_NULL(&scope);
500-
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &scope);
490+
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
491+
zval_ptr_dtor(&state.zchild);
492+
goto failure;
501493
}
502494

503-
zval_ptr_dtor(&state.zchild);
495+
zend_hash_str_update(props, "scope", sizeof("scope") - 1, &state.zchild);
504496
} else {
505497
zval scope;
506498

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

522514
PHONGO_BSON_INIT_STATE(state);
523-
if (php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
524-
Z_ADDREF_P(state.zchild);
525-
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
526-
} else {
527-
zval* scope;
528-
529-
MAKE_STD_ZVAL(scope);
530-
ZVAL_NULL(scope);
531-
zend_hash_update(props, "scope", sizeof("scope"), &scope, sizeof(scope), NULL);
515+
if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
516+
zval_ptr_dtor(&state.zchild);
517+
goto failure;
532518
}
533519

534-
zval_ptr_dtor(&state.zchild);
520+
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
535521
} else {
536522
zval* scope;
537523

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

545531
return props;
532+
533+
failure:
534+
PHONGO_GET_PROPERTY_HASH_FREE_PROPS(is_debug, props);
535+
return NULL;
546536
} /* }}} */
547537

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

0 commit comments

Comments
 (0)