Skip to content

Commit ce74688

Browse files
authored
CDRIVER-4255 add asserts for functions with client and pool parameters (#1302)
1 parent ee4e69c commit ce74688

Some content is hidden

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

57 files changed

+349
-45
lines changed

build/opts_templates/mongoc-opts.c.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ _{{ struct_name }}_parse (
1919
{
2020
bson_iter_t iter;
2121

22+
BSON_ASSERT_PARAM (client);
23+
2224
{% for path, opt_name, info in paths(description) %}
2325
{% if info['type'] == 'bool' %}
2426
{{ struct_name }}->{{ path }} = {{ description.default(opt_name, 'false') }};

src/libmongoc/examples/client-side-encryption-schema-map.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ main (void)
153153
/* Set up the key vault for this example. */
154154
keyvault_client = mongoc_client_new (
155155
"mongodb://localhost/?appname=client-side-encryption-keyvault");
156+
BSON_ASSERT (keyvault_client);
157+
156158
keyvault_coll = mongoc_client_get_collection (
157159
keyvault_client, KEYVAULT_DB, KEYVAULT_COLL);
158160
mongoc_collection_drop (keyvault_coll, NULL);
@@ -227,6 +229,7 @@ main (void)
227229

228230
client =
229231
mongoc_client_new ("mongodb://localhost/?appname=client-side-encryption");
232+
BSON_ASSERT (client);
230233

231234
/* Enable automatic encryption. It will determine that encryption is
232235
* necessary from the schema map instead of relying on the server to provide
@@ -256,6 +259,8 @@ main (void)
256259

257260
unencrypted_client = mongoc_client_new (
258261
"mongodb://localhost/?appname=client-side-encryption-unencrypted");
262+
BSON_ASSERT (unencrypted_client);
263+
259264
unencrypted_coll = mongoc_client_get_collection (
260265
unencrypted_client, ENCRYPTED_DB, ENCRYPTED_COLL);
261266
printf ("encrypted document: ");

src/libmongoc/examples/client-side-encryption-server-schema.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ main (void)
138138
/* Set up the key vault for this example. */
139139
keyvault_client = mongoc_client_new (
140140
"mongodb://localhost/?appname=client-side-encryption-keyvault");
141+
BSON_ASSERT (keyvault_client);
142+
141143
keyvault_coll = mongoc_client_get_collection (
142144
keyvault_client, KEYVAULT_DB, KEYVAULT_COLL);
143145
mongoc_collection_drop (keyvault_coll, NULL);
@@ -194,6 +196,8 @@ main (void)
194196

195197
client =
196198
mongoc_client_new ("mongodb://localhost/?appname=client-side-encryption");
199+
BSON_ASSERT (client);
200+
197201
ret = mongoc_client_enable_auto_encryption (
198202
client, auto_encryption_opts, &error);
199203
if (!ret) {
@@ -242,6 +246,8 @@ main (void)
242246

243247
unencrypted_client = mongoc_client_new (
244248
"mongodb://localhost/?appname=client-side-encryption-unencrypted");
249+
BSON_ASSERT (unencrypted_client);
250+
245251
unencrypted_coll = mongoc_client_get_collection (
246252
unencrypted_client, ENCRYPTED_DB, ENCRYPTED_COLL);
247253
printf ("encrypted document: ");

src/libmongoc/examples/mongoc-dump.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ mongoc_dump_database (mongoc_client_t *client,
101101
int ret = EXIT_SUCCESS;
102102
int i;
103103

104+
BSON_ASSERT_PARAM (client);
104105
BSON_ASSERT (database);
105106

106107
path = bson_strdup_printf ("dump/%s", database);
@@ -142,6 +143,8 @@ mongoc_dump (mongoc_client_t *client,
142143
char **str;
143144
int i;
144145

146+
BSON_ASSERT_PARAM (client);
147+
145148
if (!mongoc_dump_mkdir_p ("dump", 0750)) {
146149
perror ("Failed to create directory \"dump\"");
147150
return EXIT_FAILURE;

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void
6060
mongoc_client_pool_set_ssl_opts (mongoc_client_pool_t *pool,
6161
const mongoc_ssl_opt_t *opts)
6262
{
63+
BSON_ASSERT_PARAM (pool);
64+
6365
bson_mutex_lock (&pool->mutex);
6466

6567
_mongoc_ssl_opts_cleanup (&pool->ssl_opts,
@@ -83,6 +85,8 @@ void
8385
_mongoc_client_pool_set_internal_tls_opts (
8486
mongoc_client_pool_t *pool, _mongoc_internal_tls_opts_t *internal)
8587
{
88+
BSON_ASSERT_PARAM (pool);
89+
8690
bson_mutex_lock (&pool->mutex);
8791
if (!pool->ssl_opts_set) {
8892
bson_mutex_unlock (&pool->mutex);
@@ -252,6 +256,8 @@ mongoc_client_pool_destroy (mongoc_client_pool_t *pool)
252256
static void
253257
_start_scanner_if_needed (mongoc_client_pool_t *pool)
254258
{
259+
BSON_ASSERT_PARAM (pool);
260+
255261
if (!pool->topology->single_threaded) {
256262
_mongoc_topology_background_monitoring_start (pool->topology);
257263
}
@@ -260,6 +266,9 @@ _start_scanner_if_needed (mongoc_client_pool_t *pool)
260266
static void
261267
_initialize_new_client (mongoc_client_pool_t *pool, mongoc_client_t *client)
262268
{
269+
BSON_ASSERT_PARAM (pool);
270+
BSON_ASSERT_PARAM (client);
271+
263272
/* for tests */
264273
mongoc_client_set_stream_initiator (
265274
client,
@@ -292,7 +301,7 @@ mongoc_client_pool_pop (mongoc_client_pool_t *pool)
292301

293302
ENTRY;
294303

295-
BSON_ASSERT (pool);
304+
BSON_ASSERT_PARAM (pool);
296305

297306
wait_queue_timeout_ms = mongoc_uri_get_option_as_int32 (
298307
pool->uri, MONGOC_URI_WAITQUEUETIMEOUTMS, -1);
@@ -343,7 +352,7 @@ mongoc_client_pool_try_pop (mongoc_client_pool_t *pool)
343352

344353
ENTRY;
345354

346-
BSON_ASSERT (pool);
355+
BSON_ASSERT_PARAM (pool);
347356

348357
bson_mutex_lock (&pool->mutex);
349358

@@ -370,8 +379,8 @@ mongoc_client_pool_push (mongoc_client_pool_t *pool, mongoc_client_t *client)
370379
{
371380
ENTRY;
372381

373-
BSON_ASSERT (pool);
374-
BSON_ASSERT (client);
382+
BSON_ASSERT_PARAM (pool);
383+
BSON_ASSERT_PARAM (client);
375384

376385
bson_mutex_lock (&pool->mutex);
377386
_mongoc_queue_push_head (&pool->queue, client);
@@ -398,6 +407,8 @@ _mongoc_client_pool_set_stream_initiator (mongoc_client_pool_t *pool,
398407
mongoc_stream_initiator_t si,
399408
void *context)
400409
{
410+
BSON_ASSERT_PARAM (pool);
411+
401412
mongoc_topology_scanner_set_stream_initiator (
402413
pool->topology->scanner, si, context);
403414
}
@@ -409,6 +420,7 @@ mongoc_client_pool_get_size (mongoc_client_pool_t *pool)
409420
size_t size = 0;
410421

411422
ENTRY;
423+
BSON_ASSERT_PARAM (pool);
412424

413425
bson_mutex_lock (&pool->mutex);
414426
size = pool->size;
@@ -424,6 +436,7 @@ mongoc_client_pool_num_pushed (mongoc_client_pool_t *pool)
424436
size_t num_pushed = 0;
425437

426438
ENTRY;
439+
BSON_ASSERT_PARAM (pool);
427440

428441
bson_mutex_lock (&pool->mutex);
429442
num_pushed = pool->queue.length;
@@ -436,6 +449,8 @@ mongoc_client_pool_num_pushed (mongoc_client_pool_t *pool)
436449
mongoc_topology_t *
437450
_mongoc_client_pool_get_topology (mongoc_client_pool_t *pool)
438451
{
452+
BSON_ASSERT_PARAM (pool);
453+
439454
return pool->topology;
440455
}
441456

@@ -444,6 +459,7 @@ void
444459
mongoc_client_pool_max_size (mongoc_client_pool_t *pool, uint32_t max_pool_size)
445460
{
446461
ENTRY;
462+
BSON_ASSERT_PARAM (pool);
447463

448464
bson_mutex_lock (&pool->mutex);
449465
pool->max_pool_size = max_pool_size;
@@ -456,6 +472,7 @@ void
456472
mongoc_client_pool_min_size (mongoc_client_pool_t *pool, uint32_t min_pool_size)
457473
{
458474
ENTRY;
475+
BSON_ASSERT_PARAM (pool);
459476

460477
MONGOC_WARNING (
461478
"mongoc_client_pool_min_size is deprecated; its behavior does not match"
@@ -473,6 +490,8 @@ mongoc_client_pool_set_apm_callbacks (mongoc_client_pool_t *pool,
473490
mongoc_apm_callbacks_t *callbacks,
474491
void *context)
475492
{
493+
BSON_ASSERT_PARAM (pool);
494+
476495
mongoc_topology_t *const topology = BSON_ASSERT_PTR_INLINE (pool)->topology;
477496
mc_tpld_modification tdmod;
478497

@@ -510,6 +529,8 @@ mongoc_client_pool_set_error_api (mongoc_client_pool_t *pool, int32_t version)
510529
return false;
511530
}
512531

532+
BSON_ASSERT_PARAM (pool);
533+
513534
if (pool->error_api_set) {
514535
MONGOC_ERROR ("Can only set Error API Version once");
515536
return false;
@@ -526,6 +547,8 @@ mongoc_client_pool_set_appname (mongoc_client_pool_t *pool, const char *appname)
526547
{
527548
bool ret;
528549

550+
BSON_ASSERT_PARAM (pool);
551+
529552
bson_mutex_lock (&pool->mutex);
530553
ret = _mongoc_topology_set_appname (pool->topology, appname);
531554
bson_mutex_unlock (&pool->mutex);
@@ -538,6 +561,8 @@ mongoc_client_pool_enable_auto_encryption (mongoc_client_pool_t *pool,
538561
mongoc_auto_encryption_opts_t *opts,
539562
bson_error_t *error)
540563
{
564+
BSON_ASSERT_PARAM (pool);
565+
541566
return _mongoc_cse_client_pool_enable_auto_encryption (
542567
pool->topology, opts, error);
543568
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ _mongoc_client_session_from_iter (mongoc_client_t *client,
13821382
bson_error_t *error)
13831383
{
13841384
ENTRY;
1385+
BSON_ASSERT_PARAM (client);
13851386

13861387
/* must be int64 that fits in uint32 */
13871388
if (!BSON_ITER_HOLDS_INT64 (iter) || bson_iter_int64 (iter) > 0xffffffff) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,8 @@ _prep_for_auto_encryption (const mongoc_cmd_t *cmd, bson_t *out)
10701070
mongoc_client_t *
10711071
_get_mongocryptd_client (mongoc_client_t *client_encrypted)
10721072
{
1073+
BSON_ASSERT_PARAM (client_encrypted);
1074+
10731075
if (client_encrypted->topology->single_threaded) {
10741076
return client_encrypted->topology->mongocryptd_client;
10751077
}
@@ -1081,6 +1083,8 @@ void
10811083
_release_mongocryptd_client (mongoc_client_t *client_encrypted,
10821084
mongoc_client_t *mongocryptd_client)
10831085
{
1086+
BSON_ASSERT_PARAM (client_encrypted);
1087+
10841088
if (!mongocryptd_client) {
10851089
return;
10861090
}
@@ -1103,6 +1107,8 @@ _release_mongocryptd_client (mongoc_client_t *client_encrypted,
11031107
mongoc_collection_t *
11041108
_get_keyvault_coll (mongoc_client_t *client_encrypted)
11051109
{
1110+
BSON_ASSERT_PARAM (client_encrypted);
1111+
11061112
mongoc_write_concern_t *const wc = mongoc_write_concern_new ();
11071113
mongoc_read_concern_t *const rc = mongoc_read_concern_new ();
11081114

@@ -1149,6 +1155,8 @@ _release_keyvault_coll (mongoc_client_t *client_encrypted,
11491155
{
11501156
mongoc_client_t *keyvault_client;
11511157

1158+
BSON_ASSERT_PARAM (client_encrypted);
1159+
11521160
if (!keyvault_coll) {
11531161
return;
11541162
}
@@ -1206,6 +1214,7 @@ _mongoc_cse_auto_encrypt (mongoc_client_t *client_encrypted,
12061214

12071215
ENTRY;
12081216

1217+
BSON_ASSERT_PARAM (client_encrypted);
12091218
bson_init (encrypted);
12101219

12111220
if (client_encrypted->topology->bypass_auto_encryption) {
@@ -1315,6 +1324,7 @@ _mongoc_cse_auto_decrypt (mongoc_client_t *client_encrypted,
13151324

13161325
ENTRY;
13171326

1327+
BSON_ASSERT_PARAM (client_encrypted);
13181328
BSON_UNUSED (db_name);
13191329

13201330
keyvault_coll = _get_keyvault_coll (client_encrypted);
@@ -2920,6 +2930,8 @@ mongoc_client_encryption_decrypt (mongoc_client_encryption_t *client_encryption,
29202930
bool
29212931
_mongoc_cse_is_enabled (mongoc_client_t *client)
29222932
{
2933+
BSON_ASSERT_PARAM (client);
2934+
29232935
while (1) {
29242936
mongoc_topology_cse_state_t state = bson_atomic_int_fetch (
29252937
(int *) &client->topology->cse_state, bson_memory_order_relaxed);
@@ -3203,6 +3215,8 @@ mongoc_client_encryption_get_crypt_shared_version (
32033215
const char *
32043216
mongoc_client_get_crypt_shared_version (const mongoc_client_t *client)
32053217
{
3218+
BSON_ASSERT_PARAM (client);
3219+
32063220
#ifdef MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION
32073221
if (!client->topology->crypt) {
32083222
return NULL;

0 commit comments

Comments
 (0)