Skip to content

Commit 11ae405

Browse files
committed
PHPC-2088: Fix memory leak in php_phongo_zval_to_bson_value
The bson_t used to hold the intermediary result between zval conversion and bson_value_t extraction must be freed. This also refactors the function to use a stack-allocated zval for the data envelope.
1 parent 035cb9d commit 11ae405

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/phongo_bson_encode.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,25 +494,24 @@ void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t*
494494

495495
/* Converts the argument to a bson_value_t. If the object is an instance of
496496
* MongoDB\BSON\Serializable, the return value of bsonSerialize() will be
497-
* used. */
497+
* used. It is the caller's responsibility to call bson_value_destroy. */
498498
void php_phongo_zval_to_bson_value(zval* data, php_phongo_bson_flags_t flags, bson_value_t* value) /* {{{ */
499499
{
500500
bson_iter_t iter;
501501
bson_t bson = BSON_INITIALIZER;
502+
zval data_object;
502503

503-
zval* data_object = ecalloc(1, sizeof(zval));
504-
505-
array_init_size(data_object, 1);
506-
add_assoc_zval(data_object, "data", data);
504+
array_init_size(&data_object, 1);
505+
add_assoc_zval(&data_object, "data", data);
507506

508507
Z_TRY_ADDREF_P(data);
509508

510-
php_phongo_zval_to_bson(data_object, flags, &bson, NULL);
509+
php_phongo_zval_to_bson(&data_object, flags, &bson, NULL);
511510

512511
if (bson_iter_init_find(&iter, &bson, "data")) {
513512
bson_value_copy(bson_iter_value(&iter), value);
514513
}
515514

516-
zval_ptr_dtor(data_object);
517-
efree(data_object);
515+
bson_destroy(&bson);
516+
zval_ptr_dtor(&data_object);
518517
} /* }}} */

0 commit comments

Comments
 (0)