Skip to content

PHPC-1043: phongo_execute_query() may leak cursor struct on error #698

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
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
17 changes: 13 additions & 4 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,24 @@ void phongo_throw_exception_from_bson_error_t(bson_error_t *error TSRMLS_DC)
{
zend_throw_exception(phongo_exception_from_mongoc_domain(error->domain, error->code), error->message, error->code TSRMLS_CC);
}

static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain, const char *message, void *user_data)
{
phongo_char *dt;
struct timeval tv;
time_t t;
phongo_long tu;
phongo_char *dt;

PHONGO_TSRMLS_FETCH_FROM_CTX(user_data);
(void)user_data;

dt = php_format_date((char *) ZEND_STRL("Y-m-d\\TH:i:sP"), time(NULL), 0 TSRMLS_CC);
gettimeofday(&tv, NULL);
t = tv.tv_sec;
tu = tv.tv_usec;

dt = php_format_date((char *) ZEND_STRL("Y-m-d\\TH:i:s"), t, 0 TSRMLS_CC);

fprintf(MONGODB_G(debug_fd), "[%s] %10s: %-8s> %s\n", ZSTR_VAL(dt), log_domain, mongoc_log_level_str(log_level), message);
fprintf(MONGODB_G(debug_fd), "[%s.%06lu+00:00] %10s: %-8s> %s\n", ZSTR_VAL(dt), tu, log_domain, mongoc_log_level_str(log_level), message);
fflush(MONGODB_G(debug_fd));
efree(dt);
}
Expand Down Expand Up @@ -612,7 +620,7 @@ static int phongo_execute_parse_options(mongoc_client_t* client, int server_id,
return true;
}

bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, zval *options, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
bool phongo_execute_bulk_write(mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, zval *options, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
{
bson_error_t error;
int success;
Expand Down Expand Up @@ -759,6 +767,7 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z

if (server_id > 0 && !mongoc_cursor_set_hint(cursor, server_id)) {
phongo_throw_exception(PHONGO_ERROR_MONGOC_FAILED TSRMLS_CC, "%s", "Could not set cursor server_id");
mongoc_cursor_destroy(cursor);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void phongo_readconcern_init (zval *return_value, const
void phongo_readpreference_init (zval *return_value, const mongoc_read_prefs_t *read_prefs TSRMLS_DC);
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
mongoc_bulk_operation_t* phongo_bulkwrite_init (zend_bool ordered);
bool phongo_execute_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, zval *zwriteConcern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
bool phongo_execute_bulk_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, zval *zwriteConcern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
int phongo_execute_command (mongoc_client_t *client, php_phongo_command_type_t type, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
int phongo_execute_query (mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ static PHP_METHOD(Manager, executeBulkWrite)
intern = Z_MANAGER_OBJ_P(getThis());
bulk = Z_BULKWRITE_OBJ_P(zbulk);

phongo_execute_write(intern->client, namespace, bulk, options, -1, return_value, return_value_used TSRMLS_CC);
phongo_execute_bulk_write(intern->client, namespace, bulk, options, -1, return_value, return_value_used TSRMLS_CC);
} /* }}} */

/* {{{ proto MongoDB\Driver\ReadConcern MongoDB\Driver\Manager::getReadConcern()
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static PHP_METHOD(Server, executeBulkWrite)

bulk = Z_BULKWRITE_OBJ_P(zbulk);

phongo_execute_write(intern->client, namespace, bulk, options, intern->server_id, return_value, return_value_used TSRMLS_CC);
phongo_execute_bulk_write(intern->client, namespace, bulk, options, intern->server_id, return_value, return_value_used TSRMLS_CC);
} /* }}} */

/* {{{ proto string MongoDB\Driver\Server::getHost()
Expand Down
2 changes: 0 additions & 2 deletions tests/manager/manager-debug-001.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
--TEST--
MongoDB\Driver\Manager: Writing debug log files
--SKIPIF--
<?php if (defined("HHVM_VERSION_ID")) exit("skip HHVM uses HHVM's logging functionality"); ?>
--FILE--
<?php

Expand Down
4 changes: 1 addition & 3 deletions tests/manager/manager-debug-002.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
--TEST--
MongoDB\Driver\Manager: mongodb.debug=stderr
--SKIPIF--
<?php if (defined("HHVM_VERSION_ID")) exit("skip HHVM uses HHVM's logging functionality"); ?>
MongoDB\Driver\Manager: mongodb.debug=stderr (connection string and version)
--INI--
mongodb.debug=stderr
--FILE--
Expand Down
13 changes: 13 additions & 0 deletions tests/manager/manager-debug-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
MongoDB\Driver\Manager: mongodb.debug=stderr (date format)
--INI--
mongodb.debug=stderr
--FILE--
<?php
$manager = new MongoDB\Driver\Manager;
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
[%r(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6}\+00:00)%r]%A
===DONE===%A