Skip to content

Instantly share code, notes, and snippets.

@kamulos
Created June 25, 2021 14:35
Show Gist options
  • Save kamulos/92b9a270c9e6e36e512cfa43ec470c26 to your computer and use it in GitHub Desktop.
Save kamulos/92b9a270c9e6e36e512cfa43ec470c26 to your computer and use it in GitHub Desktop.
Memory leak in mongoc_database_command_simple when passing in a reply object created by bson_new
#include <mongoc.h>
#include <bson.h>
void execute_ping(mongoc_client_t *client)
{
bson_error_t error;
bson_t *reply = bson_new();
mongoc_database_t *database = mongoc_client_get_database(client, "admin");
bson_t *command = BCON_NEW("ping", BCON_INT32(1));
printf("reply flags before: %u\n", reply->flags);
bool success = mongoc_database_command_simple(database, command, NULL, reply, &error);
if (!success)
{
puts("mongoc_database_command_simple() failed");
}
printf("reply flags after: %u\n", reply->flags);
char *json = bson_as_json(reply, NULL);
printf("answer: %s\n", json);
bson_free(json);
bson_destroy(reply);
bson_destroy(command);
mongoc_database_destroy(database);
}
int main(void)
{
mongoc_uri_t *uri = mongoc_uri_new("mongodb://127.0.0.1:27017");
mongoc_client_pool_t *pool = mongoc_client_pool_new(uri);
mongoc_client_t *client = mongoc_client_pool_pop(pool);
if (client)
{
execute_ping(client);
mongoc_client_pool_push(pool, client);
}
else
{
puts("mongoc_client_pool_pop() failed");
}
mongoc_client_pool_destroy(pool);
mongoc_uri_destroy(uri);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment