Skip to content

Commit bef2cc7

Browse files
authored
Merge pull request #3450 from vladpaiu/fix_mongo_crash_shutdown
fix: NULL check received MongoDB connections
2 parents 078e65b + 2b566e6 commit bef2cc7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

modules/cachedb_mongodb/cachedb_mongodb_dbase.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ cachedb_con *mongo_con_init(str *url)
182182
void mongo_free_connection(cachedb_pool_con *con)
183183
{
184184
mongo_con *mcon = (mongo_con *)con;
185+
186+
if (!mcon)
187+
return;
185188

186189
mongoc_collection_destroy(mcon->collection);
187190
mongoc_database_destroy(mcon->database);
@@ -208,6 +211,9 @@ int mongo_con_get(cachedb_con *con, str *attr, str *val)
208211
char *p;
209212
int ret = 0;
210213

214+
if (!con)
215+
return -1;
216+
211217
LM_DBG("find %.*s in %s\n", attr->len, attr->s,
212218
MONGO_NAMESPACE(con));
213219

@@ -295,6 +301,9 @@ int mongo_con_set(cachedb_con *con, str *attr, str *val, int expires)
295301
struct timeval start;
296302
int ret = 0;
297303

304+
if (!con)
305+
return -1;
306+
298307
query = bson_new();
299308
bson_append_utf8(query, MDB_PK, MDB_PKLEN, attr->s, attr->len);
300309

@@ -329,6 +338,9 @@ int mongo_con_remove(cachedb_con *con, str *attr)
329338
struct timeval start;
330339
int ret = 0;
331340

341+
if (!con)
342+
return -1;
343+
332344
doc = bson_new();
333345
bson_append_utf8(doc, MDB_PK, MDB_PKLEN, attr->s, attr->len);
334346

@@ -368,6 +380,9 @@ int mongo_raw_find(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns,
368380
int i, len, csz = 0, ret = -1;
369381
const char *p;
370382

383+
if (!con)
384+
return -1;
385+
371386
if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
372387
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
373388
return -1;
@@ -519,6 +534,9 @@ int mongo_raw_update(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
519534
const bson_value_t *v;
520535
int ret, count = 0;
521536

537+
if (!con)
538+
return -1;
539+
522540
if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
523541
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
524542
return -1;
@@ -624,6 +642,9 @@ int mongo_raw_insert(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
624642
const bson_value_t *v;
625643
int ret, count = 0;
626644

645+
if (!con)
646+
return -1;
647+
627648
if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
628649
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
629650
return -1;
@@ -708,6 +729,9 @@ int mongo_raw_remove(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
708729
const bson_value_t *v;
709730
int ret, count = 0;
710731

732+
if (!con)
733+
return -1;
734+
711735
if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
712736
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
713737
return -1;
@@ -807,6 +831,9 @@ int mongo_con_raw_query(cachedb_con *con, str *qstr, cdb_raw_entry ***reply,
807831
const char *p;
808832
int csz = 0, i, len;
809833

834+
if (!con)
835+
return -1;
836+
810837
LM_DBG("Get operation on namespace %s\n", MONGO_NAMESPACE(con));
811838
start_expire_timer(start,mongo_exec_threshold);
812839

@@ -966,6 +993,9 @@ int mongo_con_add(cachedb_con *con, str *attr, int val, int expires, int *new_va
966993
struct timeval start;
967994
int ret = 0;
968995

996+
if (!con)
997+
return -1;
998+
969999
cmd = bson_new();
9701000
bson_append_utf8(cmd, "findAndModify", 13,
9711001
mongoc_collection_get_name(MONGO_COLLECTION(con)), -1);
@@ -1032,6 +1062,9 @@ int mongo_con_get_counter(cachedb_con *con, str *attr, int *val)
10321062
struct timeval start;
10331063
int ret = -2;
10341064

1065+
if (!con)
1066+
return -1;
1067+
10351068
query = bson_new();
10361069
#if MONGOC_CHECK_VERSION(1, 5, 0)
10371070
bson_append_utf8(query, MDB_PK, MDB_PKLEN, attr->s, attr->len);
@@ -1217,6 +1250,9 @@ int mongo_db_query_trans(cachedb_con *con, const str *table, const db_key_t *_k,
12171250
char *strf, *stro;
12181251
str st;
12191252

1253+
if (!con)
1254+
return -1;
1255+
12201256
*_r = NULL;
12211257

12221258
filter = bson_new();
@@ -1516,6 +1552,9 @@ int mongo_db_insert_trans(cachedb_con *con, const str *table,
15161552
mongoc_collection_t *col = NULL;
15171553
struct timeval start;
15181554

1555+
if (!con)
1556+
return -1;
1557+
15191558
doc = bson_new();
15201559
if (kvo_to_bson(_k, _v, NULL, _n, doc) != 0) {
15211560
LM_ERR("failed to build bson\n");
@@ -1565,6 +1604,9 @@ int mongo_db_delete_trans(cachedb_con *con, const str *table,
15651604
mongoc_collection_t *col = NULL;
15661605
struct timeval start;
15671606

1607+
if (!con)
1608+
return -1;
1609+
15681610
doc = bson_new();
15691611
if (kvo_to_bson(_k, _v, _o, _n, doc) != 0) {
15701612
LM_ERR("failed to build bson\n");
@@ -1615,6 +1657,9 @@ int mongo_db_update_trans(cachedb_con *con, const str *table,
16151657
mongoc_collection_t *col = NULL;
16161658
struct timeval start;
16171659

1660+
if (!con)
1661+
return -1;
1662+
16181663
query = bson_new();
16191664
if (kvo_to_bson(_k, _v, _o, _n, query) != 0) {
16201665
LM_ERR("failed to build query bson\n");
@@ -1677,6 +1722,9 @@ int mongo_truncate(cachedb_con *con)
16771722
struct timeval start;
16781723
int ret = 0;
16791724

1725+
if (!con)
1726+
return -1;
1727+
16801728
start_expire_timer(start, mongo_exec_threshold);
16811729
if (!mongoc_collection_remove(MONGO_COLLECTION(con),
16821730
MONGOC_REMOVE_NONE, &empty_doc, NULL, &error)) {
@@ -1894,6 +1942,9 @@ int mongo_con_query(cachedb_con *con, const cdb_filter_t *filter,
18941942
const bson_t *doc;
18951943
struct timeval start;
18961944

1945+
if (!con)
1946+
return -1;
1947+
18971948
LM_DBG("find all in %s\n", MONGO_NAMESPACE(con));
18981949

18991950
cdb_res_init(res);
@@ -2074,6 +2125,9 @@ int mongo_con_update(cachedb_con *con, const cdb_filter_t *row_filter,
20742125
cdb_pair_t *pair;
20752126
str key;
20762127

2128+
if (!con)
2129+
return -1;
2130+
20772131
if (mongo_cdb_filter_to_bson(row_filter, &filter) != 0) {
20782132
LM_ERR("failed to build bson filter\n");
20792133
return -1;

0 commit comments

Comments
 (0)