Skip to content

Commit 81347a8

Browse files
committed
CDRIVER-2680 ignore NULL argument to destroy funcs
1 parent eb15738 commit 81347a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+119
-93
lines changed

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Next Release
2+
============
3+
4+
* All "destroy" functions such as bson_destroy or mongoc_collection_destroy
5+
now ignore a NULL argument.
6+
7+
18
mongo-c-driver 1.10.1
29
=====================
310

src/libbson/doc/bson_context_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Parameters
1919
Description
2020
-----------
2121

22-
The ``bson_context_destroy()`` function shall release all resources associated with ``context``.
22+
The ``bson_context_destroy()`` function shall release all resources associated with ``context``. Does nothing if ``context`` is NULL.
2323

2424
This should be called when you are no longer using a :symbol:`bson_context_t` that you have allocated with :symbol:`bson_context_new()`.
2525

src/libbson/doc/bson_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Parameters
1919
Description
2020
-----------
2121

22-
The :symbol:`bson_destroy()` function shall free an allocated :symbol:`bson_t` structure.
22+
The :symbol:`bson_destroy()` function shall free an allocated :symbol:`bson_t` structure. Does nothing if ``bson`` is NULL.
2323

2424
This function should always be called when you are done with a :symbol:`bson_t` unless otherwise specified.
2525

src/libbson/doc/bson_json_reader_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees a bson_json_reader_t.
23-
22+
Frees a bson_json_reader_t. Does nothing if ``reader`` is NULL.

src/libbson/doc/bson_reader_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Destroys and releases all resources associated with ``reader``.
23-
22+
Destroys and releases all resources associated with ``reader``. Does nothing if ``reader`` is NULL.

src/libbson/doc/bson_value_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Releases any resources associated with ``value``.
23-
22+
Releases any resources associated with ``value``. Does nothing if ``value`` is NULL.

src/libbson/doc/bson_writer_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Cleanup after ``writer`` and release any allocated memory. Note that the buffer supplied to :symbol:`bson_writer_new()` is *NOT* freed from this method. The caller is responsible for that.
23-
22+
Cleanup after ``writer`` and release any allocated memory. Does nothing if ``writer`` is NULL. Note that the buffer supplied to :symbol:`bson_writer_new()` is *NOT* freed from this method. The caller is responsible for that.

src/libbson/src/bson/bson-context.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,7 @@ bson_context_new (bson_context_flags_t flags)
463463
void
464464
bson_context_destroy (bson_context_t *context) /* IN */
465465
{
466-
if (context != &gContextDefault) {
467-
memset (context, 0, sizeof *context);
468-
bson_free (context);
469-
}
466+
bson_free (context);
470467
}
471468

472469

src/libbson/src/bson/bson-json.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,8 +2145,15 @@ void
21452145
bson_json_reader_destroy (bson_json_reader_t *reader) /* IN */
21462146
{
21472147
int i;
2148-
bson_json_reader_producer_t *p = &reader->producer;
2149-
bson_json_reader_bson_t *b = &reader->bson;
2148+
bson_json_reader_producer_t *p;
2149+
bson_json_reader_bson_t *b;
2150+
2151+
if (!reader) {
2152+
return;
2153+
}
2154+
2155+
p = &reader->producer;
2156+
b = &reader->bson;
21502157

21512158
if (reader->producer.dcb) {
21522159
reader->producer.dcb (reader->producer.data);

src/libbson/src/bson/bson-reader.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ _bson_reader_data_tell (bson_reader_data_t *reader) /* IN */
644644
void
645645
bson_reader_destroy (bson_reader_t *reader) /* IN */
646646
{
647-
BSON_ASSERT (reader);
647+
if (!reader) {
648+
return;
649+
}
648650

649651
switch (reader->type) {
650652
case 0:

src/libbson/src/bson/bson-value.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ bson_value_copy (const bson_value_t *src, /* IN */
142142
void
143143
bson_value_destroy (bson_value_t *value) /* IN */
144144
{
145+
if (!value) {
146+
return;
147+
}
148+
145149
switch (value->value_type) {
146150
case BSON_TYPE_UTF8:
147151
bson_free (value->value.v_utf8.str);

src/libbson/src/bson/bson.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,9 @@ bson_copy_to_excluding_noinit (const bson_t *src,
23112311
void
23122312
bson_destroy (bson_t *bson)
23132313
{
2314-
BSON_ASSERT (bson);
2314+
if (!bson) {
2315+
return;
2316+
}
23152317

23162318
if (!(bson->flags &
23172319
(BSON_FLAG_RDONLY | BSON_FLAG_INLINE | BSON_FLAG_NO_FREE))) {

src/libmongoc/doc/mongoc_apm_callbacks_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Synopsis
1111
void
1212
mongoc_apm_callbacks_destroy (mongoc_apm_callbacks_t *callbacks);
1313
14-
Free a :symbol:`mongoc_apm_callbacks_t`.
14+
Free a :symbol:`mongoc_apm_callbacks_t`. Does nothing if ``callbacks`` is NULL.
1515

1616
See Also
1717
--------

src/libmongoc/doc/mongoc_bulk_operation_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Synopsis
1111
void
1212
mongoc_bulk_operation_destroy (mongoc_bulk_operation_t *bulk);
1313
14-
Destroys a :symbol:`mongoc_bulk_operation_t` and frees the structure.
14+
Destroys a :symbol:`mongoc_bulk_operation_t` and frees the structure. Does nothing if ``bulk`` is NULL.
1515

1616
Parameters
1717
----------

src/libmongoc/doc/mongoc_client_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Synopsis
1111
void
1212
mongoc_client_destroy (mongoc_client_t *client);
1313
14-
Release all resources associated with ``client`` and free the structure.
14+
Release all resources associated with ``client`` and free the structure. Does nothing if ``client`` is NULL.
1515

1616
Parameters
1717
----------

src/libmongoc/doc/mongoc_client_session_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Synopsis
1111
void
1212
mongoc_client_session_destroy (mongoc_client_session_t *session);
1313
14-
End a session, returning its session id to the pool, and free all client resources associated with the session. If a multi-document transaction is in progress, abort it.
14+
End a session, returning its session id to the pool, and free all client resources associated with the session. If a multi-document transaction is in progress, abort it. Does nothing if ``session`` is NULL.
1515

1616
See the example code for :symbol:`mongoc_client_session_t`.
1717

src/libmongoc/doc/mongoc_collection_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Parameters
1919
Description
2020
-----------
2121

22-
This function shall destroy a :symbol:`mongoc_collection_t` and its associated resources.
22+
This function shall destroy a :symbol:`mongoc_collection_t` and its associated resources. Does nothing if ``collection`` is NULL.
2323

2424
.. warning::
2525

src/libmongoc/doc/mongoc_cursor_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees a :symbol:`mongoc_cursor_t` and releases all associated resources. If a server-side cursor has been allocated, it will be released as well.
23-
22+
Frees a :symbol:`mongoc_cursor_t` and releases all associated resources. If a server-side cursor has been allocated, it will be released as well. Does nothing if ``cursor`` is NULL.

src/libmongoc/doc/mongoc_database_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Synopsis
1111
void
1212
mongoc_database_destroy (mongoc_database_t *database);
1313
14-
Releases all resources associated with ``database``, including freeing the structure.
14+
Releases all resources associated with ``database``, including freeing the structure. Does nothing if ``database`` is NULL.
1515

1616
Parameters
1717
----------

src/libmongoc/doc/mongoc_find_and_modify_opts_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ Parameters
2020
Description
2121
-----------
2222

23-
Frees all resources associated with the find and modify builder structure.
23+
Frees all resources associated with the find and modify builder structure. Does nothing if ``find_and_modify_opts`` is NULL.
2424

src/libmongoc/doc/mongoc_gridfs_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
This function shall destroy the gridfs structure referenced by ``gridfs`` and any resources associated with the gridfs.
23-
22+
This function shall destroy the gridfs structure referenced by ``gridfs`` and any resources associated with the gridfs. Does nothing if ``gridfs`` is NULL.

src/libmongoc/doc/mongoc_gridfs_file_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Destroys the :symbol:`mongoc_gridfs_file_t` instance and any resources associated with it.
23-
22+
Destroys the :symbol:`mongoc_gridfs_file_t` instance and any resources associated with it. Does nothing if ``file`` is NULL.

src/libmongoc/doc/mongoc_gridfs_file_list_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees a ``mongoc_gridfs_file_list_t`` and releases any associated resources.
23-
22+
Frees a ``mongoc_gridfs_file_list_t`` and releases any associated resources. Does nothing if ``list`` is NULL.

src/libmongoc/doc/mongoc_read_concern_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees all resources associated with the read concern structure.
23-
22+
Frees all resources associated with the read concern structure. Does nothing if ``read_concern`` is NULL.

src/libmongoc/doc/mongoc_read_prefs_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees a read preference and all associated resources.
23-
22+
Frees a read preference and all associated resources. Does nothing if ``read_prefs`` is NULL.

src/libmongoc/doc/mongoc_server_description_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Clean up all memory associated with the server description.
23-
22+
Clean up all memory associated with the server description. Does nothing if ``description`` is NULL.

src/libmongoc/doc/mongoc_session_opts_destroy.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ Synopsis
1111
void
1212
mongoc_session_opts_destroy (mongoc_session_opt_t *opts);
1313
14-
Free a :symbol:`mongoc_session_opt_t`.
14+
Free a :symbol:`mongoc_session_opt_t`. Does nothing if ``opts`` is NULL.
15+
16+
Parameters
17+
----------
18+
19+
* ``opts``: A :symbol:`mongoc_session_opt_t`.
20+
21+
Example
22+
-------
1523

1624
See the example code for :symbol:`mongoc_session_opts_set_causal_consistency`.
1725

src/libmongoc/doc/mongoc_socket_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
This function releases all resources associated with a :symbol:`mongoc_socket_t`. This should be called when you are no longer using the socket.
23-
22+
This function releases all resources associated with a :symbol:`mongoc_socket_t`. This should be called when you are no longer using the socket. Does nothing if ``sock`` is NULL.

src/libmongoc/doc/mongoc_stream_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ Parameters
1616

1717
* ``stream``: A :symbol:`mongoc_stream_t`.
1818

19-
This function shall release all resources associated with a :symbol:`mongoc_stream_t`, including freeing the structure. It is invalid to use ``stream`` after calling this function.
20-
19+
This function shall release all resources associated with a :symbol:`mongoc_stream_t`, including freeing the structure. It is invalid to use ``stream`` after calling this function. Does nothing if ``stream`` is NULL.

src/libmongoc/doc/mongoc_transaction_opts_destroy.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ Synopsis
1111
void
1212
mongoc_transaction_opts_destroy (mongoc_transaction_opt_t *opts);
1313
14-
Free a :symbol:`mongoc_transaction_opt_t`.
14+
Free a :symbol:`mongoc_transaction_opt_t`. Does nothing if ``opts`` is NULL.
15+
16+
Parameters
17+
----------
18+
19+
* ``opts``: A :symbol:`mongoc_transaction_opt_t`.
1520

1621
.. only:: html
1722

src/libmongoc/doc/mongoc_uri_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees all resources associated with a uri.
23-
22+
Frees all resources associated with a uri. Does nothing if ``uri`` is NULL.

src/libmongoc/doc/mongoc_write_concern_destroy.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Frees all resources associated with the write concern structure.
23-
22+
Frees all resources associated with the write concern structure. Does nothing if ``write_concern`` is NULL.

src/libmongoc/src/mongoc/mongoc-change-stream.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ mongoc_change_stream_error_document (const mongoc_change_stream_t *stream,
439439
void
440440
mongoc_change_stream_destroy (mongoc_change_stream_t *stream)
441441
{
442-
BSON_ASSERT (stream);
442+
if (!stream) {
443+
return;
444+
}
445+
443446
bson_destroy (&stream->pipeline_to_append);
444447
bson_destroy (&stream->full_document);
445448
bson_destroy (&stream->opts);

src/libmongoc/src/mongoc/mongoc-client-pool.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ mongoc_client_pool_destroy (mongoc_client_pool_t *pool)
160160

161161
ENTRY;
162162

163-
BSON_ASSERT (pool);
163+
if (!pool) {
164+
EXIT;
165+
}
164166

165167
if (pool->topology->session_pool) {
166168
client = mongoc_client_pool_pop (pool);

src/libmongoc/src/mongoc/mongoc-client-session.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ mongoc_transaction_opts_destroy (mongoc_transaction_opt_t *opts)
168168
{
169169
ENTRY;
170170

171-
BSON_ASSERT (opts);
171+
if (!opts) {
172+
EXIT;
173+
}
172174

173175
txn_opts_cleanup (opts);
174176
bson_free (opts);
@@ -331,7 +333,9 @@ mongoc_session_opts_destroy (mongoc_session_opt_t *opts)
331333
{
332334
ENTRY;
333335

334-
BSON_ASSERT (opts);
336+
if (!opts) {
337+
EXIT;
338+
}
335339

336340
txn_opts_cleanup (&opts->default_txn_opts);
337341
bson_free (opts);
@@ -1008,7 +1012,9 @@ mongoc_client_session_destroy (mongoc_client_session_t *session)
10081012
{
10091013
ENTRY;
10101014

1011-
BSON_ASSERT (session);
1015+
if (!session) {
1016+
EXIT;
1017+
}
10121018

10131019
if (_mongoc_client_session_in_txn (session)) {
10141020
mongoc_client_session_abort_transaction (session, NULL);

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ _mongoc_client_retryable_write_command_with_stream (
15721572
if (retry_server_stream && retry_server_stream->sd->max_wire_version >=
15731573
WIRE_VERSION_RETRY_WRITES) {
15741574
parts->assembled.server_stream = retry_server_stream;
1575-
_mongoc_bson_destroy_if_set (reply);
1575+
bson_destroy (reply);
15761576
GOTO (retry);
15771577
}
15781578
}

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ _mongoc_collection_new (mongoc_client_t *client,
186186
* None.
187187
*
188188
* Side effects:
189-
* Everything.
189+
* None.
190190
*
191191
*--------------------------------------------------------------------------
192192
*/
@@ -196,7 +196,9 @@ mongoc_collection_destroy (mongoc_collection_t *collection) /* IN */
196196
{
197197
ENTRY;
198198

199-
BSON_ASSERT (collection);
199+
if (!collection) {
200+
EXIT;
201+
}
200202

201203
bson_clear (&collection->gle);
202204

src/libmongoc/src/mongoc/mongoc-cursor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,9 @@ mongoc_cursor_destroy (mongoc_cursor_t *cursor)
539539
char db[MONGOC_NAMESPACE_MAX];
540540
ENTRY;
541541

542-
BSON_ASSERT (cursor);
542+
if (!cursor) {
543+
EXIT;
544+
}
543545

544546
if (cursor->impl.destroy) {
545547
cursor->impl.destroy (&cursor->impl);

src/libmongoc/src/mongoc/mongoc-database.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ mongoc_database_destroy (mongoc_database_t *database)
103103
{
104104
ENTRY;
105105

106-
BSON_ASSERT (database);
106+
if (!database) {
107+
EXIT;
108+
}
107109

108110
if (database->read_prefs) {
109111
mongoc_read_prefs_destroy (database->read_prefs);

0 commit comments

Comments
 (0)