-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-34260: fix c code for memory leak (#931)
* DOCSP-34260: fix c code for memory leak * update connection strings * format errors
- Loading branch information
Showing
3 changed files
with
127 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 50 additions & 40 deletions
90
source/includes/connection-snippets/scram/c-connection-no-stable-api.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
#include <mongoc/mongoc.h> | ||
|
||
int main (int argc, char **argv) | ||
{ | ||
mongoc_client_t *client = NULL; | ||
bson_error_t error = {0}; | ||
mongoc_database_t *database = NULL; | ||
bson_t *command = NULL, reply; | ||
|
||
|
||
// Initialize the MongoDB C Driver. | ||
mongoc_init (); | ||
|
||
// Replace the <connection string> with your MongoDB deployment's connection string. | ||
client = mongoc_client_new("<connection string>"); | ||
|
||
// Get a handle on the "admin" database. | ||
database = mongoc_client_get_database (client, "admin"); | ||
|
||
// Ping the database. | ||
command = BCON_NEW("ping", BCON_INT32(1)); | ||
if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) | ||
{ | ||
printf("Pinged your deployment. You successfully connected to MongoDB!\n"); | ||
} | ||
else | ||
{ | ||
// Error condition. | ||
printf("Error: %s\n", error.message); | ||
return 0; | ||
} | ||
|
||
|
||
// Perform Cleanup. | ||
bson_destroy (&reply); | ||
bson_destroy (command); | ||
mongoc_database_destroy (database); | ||
mongoc_client_destroy (client); | ||
mongoc_cleanup (); | ||
|
||
return 0; | ||
} | ||
int main(void) { | ||
mongoc_client_t *client = NULL; | ||
bson_error_t error = {0}; | ||
mongoc_database_t *database = NULL; | ||
bson_t *command = NULL; | ||
bson_t reply = BSON_INITIALIZER; | ||
int rc = 0; | ||
bool ok = true; | ||
|
||
// Initialize the MongoDB C Driver. | ||
mongoc_init(); | ||
|
||
client = mongoc_client_new("<connection string>"); | ||
if (!client) { | ||
fprintf(stderr, "Failed to create a MongoDB client.\n"); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
// Get a handle on the "admin" database. | ||
database = mongoc_client_get_database(client, "admin"); | ||
if (!database) { | ||
fprintf(stderr, "Failed to get a MongoDB database handle.\n"); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
// Ping the database. | ||
command = BCON_NEW("ping", BCON_INT32(1)); | ||
ok = mongoc_database_command_simple( | ||
database, command, NULL, &reply, &error | ||
); | ||
if (!ok) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
bson_destroy(&reply); | ||
|
||
printf("Pinged your deployment. You successfully connected to MongoDB!\n"); | ||
|
||
// Perform cleanup. | ||
cleanup: | ||
bson_destroy(command); | ||
mongoc_database_destroy(database); | ||
mongoc_client_destroy(client); | ||
mongoc_cleanup(); | ||
|
||
return rc; | ||
} |
114 changes: 66 additions & 48 deletions
114
source/includes/connection-snippets/scram/c-connection.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,69 @@ | ||
#include <mongoc/mongoc.h> | ||
|
||
int main (int argc, char **argv) | ||
{ | ||
mongoc_client_t *client = NULL; | ||
bson_error_t error = {0}; | ||
mongoc_server_api_t *api = NULL; | ||
mongoc_database_t *database = NULL; | ||
bson_t *command = NULL, reply; | ||
|
||
// Initialize the MongoDB C Driver. | ||
mongoc_init (); | ||
|
||
// Replace the <connection string> with your MongoDB deployment's connection string. | ||
client = mongoc_client_new("<connection string>"); | ||
|
||
// Set the version of the Stable API on the client. | ||
api = mongoc_server_api_new (MONGOC_SERVER_API_V1); | ||
if (!mongoc_client_set_server_api (client, api, &error)) | ||
{ | ||
// Error condition. | ||
printf("Error: %s\n", error.message); | ||
return 0; | ||
} | ||
|
||
// Get a handle on the "admin" database. | ||
database = mongoc_client_get_database (client, "admin"); | ||
|
||
// Ping the database. | ||
command = BCON_NEW("ping", BCON_INT32(1)); | ||
if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) | ||
{ | ||
int main(void) { | ||
mongoc_client_t *client = NULL; | ||
bson_error_t error = {0}; | ||
mongoc_server_api_t *api = NULL; | ||
mongoc_database_t *database = NULL; | ||
bson_t *command = NULL; | ||
bson_t reply = BSON_INITIALIZER; | ||
int rc = 0; | ||
bool ok = true; | ||
|
||
// Initialize the MongoDB C Driver. | ||
mongoc_init(); | ||
|
||
client = mongoc_client_new("<connection string>"); | ||
if (!client) { | ||
fprintf(stderr, "Failed to create a MongoDB client.\n"); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
// Set the version of the Stable API on the client. | ||
api = mongoc_server_api_new(MONGOC_SERVER_API_V1); | ||
if (!api) { | ||
fprintf(stderr, "Failed to create a MongoDB server API.\n"); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
ok = mongoc_client_set_server_api(client, api, &error); | ||
if (!ok) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
// Get a handle on the "admin" database. | ||
database = mongoc_client_get_database(client, "admin"); | ||
if (!database) { | ||
fprintf(stderr, "Failed to get a MongoDB database handle.\n"); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
|
||
// Ping the database. | ||
command = BCON_NEW("ping", BCON_INT32(1)); | ||
ok = mongoc_database_command_simple( | ||
database, command, NULL, &reply, &error | ||
); | ||
if (!ok) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
rc = 1; | ||
goto cleanup; | ||
} | ||
bson_destroy(&reply); | ||
|
||
printf("Pinged your deployment. You successfully connected to MongoDB!\n"); | ||
} | ||
else | ||
{ | ||
// Error condition. | ||
printf("Error: %s\n", error.message); | ||
return 0; | ||
} | ||
|
||
|
||
// Perform Cleanup. | ||
bson_destroy (&reply); | ||
bson_destroy (command); | ||
mongoc_database_destroy (database); | ||
mongoc_client_destroy (client); | ||
mongoc_cleanup (); | ||
|
||
return 0; | ||
} | ||
|
||
// Perform cleanup. | ||
cleanup: | ||
bson_destroy(command); | ||
mongoc_database_destroy(database); | ||
mongoc_server_api_destroy(api); | ||
mongoc_client_destroy(client); | ||
mongoc_cleanup(); | ||
|
||
return rc; | ||
} |