Skip to content

Commit 021a050

Browse files
committed
CDRIVER-446 Fix a leak in mongoc_client_command_simple()
fix a leak in mongoc_client_command_simple(). Fix some other random leaks while we're at it Closes #110
1 parent bb0e108 commit 021a050

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

src/mongoc/mongoc-collection.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,8 @@ mongoc_collection_get_index_info (mongoc_collection_t *collection,
10001000
bool cmd_success;
10011001
mongoc_read_prefs_t *read_prefs;
10021002
bson_t cmd = BSON_INITIALIZER;
1003-
bson_t *reply = bson_new ();
1003+
bson_t command_reply;
1004+
bson_t *reply = NULL;
10041005

10051006
BSON_ASSERT (collection);
10061007

@@ -1010,32 +1011,30 @@ mongoc_collection_get_index_info (mongoc_collection_t *collection,
10101011
read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
10111012

10121013
cmd_success = mongoc_collection_command_simple (collection, &cmd, NULL,
1013-
reply, error);
1014+
&command_reply, error);
10141015

10151016
if (cmd_success) {
10161017
/* intentionally empty */
1018+
reply = bson_copy (&command_reply);
10171019
} else if (error->code == MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST) {
10181020
bson_t empty_arr = BSON_INITIALIZER;
10191021
/* collection does not exist. in accordance with the spec we return
10201022
* an empty array. Also we need to clear out the error. */
10211023
error->code = 0;
10221024
error->domain = 0;
1023-
BSON_APPEND_ARRAY (reply, "indexes", &empty_arr);
1025+
BSON_APPEND_ARRAY (&command_reply, "indexes", &empty_arr);
1026+
reply = bson_copy (&command_reply);
10241027
} else if (error->code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND) {
1025-
bson_destroy (reply);
10261028
/* talking to an old server. */
10271029
/* clear out error. */
10281030
error->code = 0;
10291031
error->domain = 0;
10301032
reply =
10311033
_mongoc_collection_get_index_info_legacy (collection, error);
1032-
} else {
1033-
/* network error */
1034-
bson_destroy (reply);
1035-
reply = NULL;
10361034
}
10371035

10381036
bson_destroy (&cmd);
1037+
bson_destroy (&command_reply);
10391038
mongoc_read_prefs_destroy (read_prefs);
10401039

10411040
return reply;

src/mongoc/mongoc-database.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ mongoc_database_has_collection (mongoc_database_t *database,
632632
}
633633

634634
cleanup:
635-
bson_free (infos);
635+
bson_destroy (infos);
636636

637637
RETURN (ret);
638638
}
@@ -740,7 +740,8 @@ mongoc_database_get_collection_info (mongoc_database_t *database,
740740
bson_error_t *error)
741741
{
742742
mongoc_read_prefs_t *read_prefs;
743-
bson_t *reply = bson_new();
743+
bson_t *reply = NULL;
744+
bson_t command_reply;
744745
bson_t cmd = BSON_INITIALIZER;
745746
bool cmd_success;
746747

@@ -755,25 +756,22 @@ mongoc_database_get_collection_info (mongoc_database_t *database,
755756
read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
756757

757758
cmd_success = mongoc_database_command_simple (database, &cmd, read_prefs,
758-
reply, error);
759+
&command_reply, error);
759760
if (cmd_success) {
760761
/* intentionally empty */
762+
reply = bson_copy(&command_reply);
761763
} else if (error->code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND) {
762-
bson_destroy (reply);
763764
/* We are talking to a server that doesn' support listCollections. */
764765
/* clear out the error. */
765766
error->code = 0;
766767
error->domain = 0;
767768
/* try again with using system.namespaces */
768769
reply =
769770
_mongoc_database_get_collection_info_legacy (database, filter, error);
770-
} else {
771-
/* network error */
772-
bson_destroy (reply);
773-
reply = NULL;
774771
}
775772

776773
bson_destroy (&cmd);
774+
bson_destroy (&command_reply);
777775
mongoc_read_prefs_destroy (read_prefs);
778776

779777
return reply;

tests/test-bulk.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ test_bulk_edge_over_1000 (void)
353353
}
354354

355355
mongoc_bulk_operation_destroy(bulk_op);
356+
bson_destroy (&result);
356357

357358
mongoc_write_concern_destroy(wc);
358359

tests/test-mongoc-collection.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,8 @@ test_get_index_info (void)
11841184
bson_iter_t idx_spec_iter_copy;
11851185
bool r;
11861186
const char *cur_idx_name;
1187-
const char *idx1_name;
1188-
const char *idx2_name;
1187+
char *idx1_name = NULL;
1188+
char *idx2_name = NULL;
11891189
const char *id_idx_name = "_id_";
11901190
int num_idxs = 0;
11911191

@@ -1214,6 +1214,8 @@ test_get_index_info (void)
12141214
}
12151215
}
12161216

1217+
bson_destroy(indexinfo);
1218+
12171219
/* insert a dummy document so that the collection actually exists */
12181220
r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &dummy, NULL,
12191221
&error);
@@ -1307,6 +1309,9 @@ test_get_index_info (void)
13071309

13081310
assert (3 == num_idxs);
13091311

1312+
bson_free (idx1_name);
1313+
bson_free (idx2_name);
1314+
13101315
bson_destroy (indexinfo);
13111316
mongoc_collection_destroy (collection);
13121317
mongoc_client_destroy (client);

tests/test-mongoc-cursor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ test_invalid_query (void)
129129

130130
q = BCON_NEW ("foo", BCON_INT32 (1), "$orderby", "{", "}");
131131

132-
doc = q;
133-
134132
cursor = _mongoc_cursor_new (client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1,
135133
false, q, NULL, NULL);
136134
r = mongoc_cursor_next (cursor, &doc);
@@ -141,6 +139,7 @@ test_invalid_query (void)
141139
assert (error.code == MONGOC_ERROR_CURSOR_INVALID_CURSOR);
142140
assert (doc == NULL);
143141

142+
bson_destroy (q);
144143
mongoc_cursor_destroy (cursor);
145144
mongoc_client_destroy (client);
146145
mongoc_uri_destroy(uri);

0 commit comments

Comments
 (0)