Skip to content

Commit 8c89c1c

Browse files
committed
PHPC-135: Fix memory leak var_dump()ing Command
1 parent 5e48dff commit 8c89c1c

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/MongoDB/Command.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ PHP_METHOD(Command, __construct)
5454
zend_error_handling error_handling;
5555
zval *document;
5656
bson_t *bson = bson_new();
57+
(void)return_value; (void)return_value_ptr; (void)return_value_used;
5758

5859

5960
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -106,7 +107,7 @@ static void php_phongo_command_free_object(void *object TSRMLS_DC) /* {{{ */
106107
zend_object_value php_phongo_command_create_object(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
107108
{
108109
zend_object_value retval;
109-
php_phongo_command_t *intern;
110+
php_phongo_command_t *intern = NULL;
110111

111112
intern = (php_phongo_command_t *)ecalloc(1, sizeof *intern);
112113

@@ -122,24 +123,25 @@ zend_object_value php_phongo_command_create_object(zend_class_entry *class_type
122123
HashTable *php_phongo_command_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
123124
{
124125
php_phongo_command_t *intern;
125-
zval *retval = NULL;
126+
zval retval = zval_used_for_init;
126127

127128

128-
*is_temp = 0;
129+
*is_temp = 1;
129130
intern = (php_phongo_command_t *)zend_object_store_get_object(object TSRMLS_CC);
130131

131-
MAKE_STD_ZVAL(retval);
132-
array_init(retval);
132+
array_init_size(&retval, 1);
133133

134134
if (intern->bson) {
135135
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
136136

137137
MAKE_STD_ZVAL(state.zchild);
138138
bson_to_zval(bson_get_data(intern->bson), intern->bson->len, &state);
139-
add_assoc_zval_ex(retval, ZEND_STRS("command"), state.zchild);
139+
add_assoc_zval_ex(&retval, ZEND_STRS("command"), state.zchild);
140+
} else {
141+
add_assoc_null_ex(&retval, ZEND_STRS("command"));
140142
}
141143

142-
return Z_ARRVAL_P(retval);
144+
return Z_ARRVAL(retval);
143145

144146
} /* }}} */
145147

@@ -148,7 +150,7 @@ HashTable *php_phongo_command_get_debug_info(zval *object, int *is_temp TSRMLS_D
148150
/* {{{ PHP_MINIT_FUNCTION */
149151
PHP_MINIT_FUNCTION(Command)
150152
{
151-
(void)type; /* We don't care if we are loaded via dl() or extension= */
153+
(void)type;(void)module_number;
152154
zend_class_entry ce;
153155

154156
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "Command", php_phongo_command_me);

tests/standalone/manager-executeCommand-001.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ $manager = new MongoDB\Driver\Manager(MONGODB_URI);
1010

1111
$command = new MongoDB\Driver\Command(array('ping' => 1));
1212
$result = $manager->executeCommand(DATABASE_NAME, $command);
13+
var_dump($command);
1314

1415
var_dump($result instanceof MongoDB\Driver\CommandResult);
1516

@@ -29,6 +30,13 @@ var_dump($server->getPort());
2930
===DONE===
3031
<?php exit(0); ?>
3132
--EXPECTF--
33+
object(MongoDB\Driver\Command)#%d (1) {
34+
["command"]=>
35+
array(1) {
36+
["ping"]=>
37+
int(1)
38+
}
39+
}
3240
bool(true)
3341
Dumping response document:
3442
array(1) {

0 commit comments

Comments
 (0)