The command `mongoc_database_command_simple` leaks memory, when passing in a reply object created by bson_new.
The bson_t* reply output argument of mongoc_database_command_simple must be a pointer to an uninitialized bson_t, and cannot be the result of bson_new.
The available options are:
1. Use a bson_t on the stack:
bson_t reply;
|
bson_t cmd;
|
mongoc_database_t* db;
|
bson_error_t err;
|
|
bson_init (&cmd);
|
BCON_APPEND (&cmd, "ping", BCON_INT32(1));
|
db = mongoc_client_get_database (client, "admin");
|
if (!mongoc_database_command_simple (db, &cmd, NULL /* read prefs */, &reply, &err)) {
|
MONGOC_ERROR ("database_command_simple error: %s", err.message);
|
}
|
bson_destroy (&cmd);
|
bson_destroy (&reply);
|
mongoc_database_destroy (db);
|
2. Manually heap allocate a bson_t
bson_t* reply;
|
bson_t cmd;
|
mongoc_database_t* db;
|
bson_error_t err;
|
|
reply = bson_malloc0 (sizeof (bson_t));
|
bson_init (&cmd);
|
BCON_APPEND (&cmd, "ping", BCON_INT32(1));
|
db = mongoc_client_get_database (client, "admin");
|
if (!mongoc_database_command_simple (db, &cmd, NULL /* read prefs */, reply, &err)) {
|
MONGOC_ERROR ("database_command_simple error: %s", err.message);
|
}
|
|
bson_destroy (&cmd);
|
bson_destroy (reply);
|
bson_free (reply);
|
mongoc_database_destroy (db);
|
The expectation around how reply should be passed is inconsistently documented. I have updated the description of CDRIVER-3368 to call out mongoc_database_command_simple as one such example.
|