Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NULL check received MongoDB connections #3450

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions modules/cachedb_mongodb/cachedb_mongodb_dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ cachedb_con *mongo_con_init(str *url)
void mongo_free_connection(cachedb_pool_con *con)
{
mongo_con *mcon = (mongo_con *)con;

if (!mcon)
return;

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

if (!con)
return -1;

LM_DBG("find %.*s in %s\n", attr->len, attr->s,
MONGO_NAMESPACE(con));

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

if (!con)
return -1;

query = bson_new();
bson_append_utf8(query, MDB_PK, MDB_PKLEN, attr->s, attr->len);

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

if (!con)
return -1;

doc = bson_new();
bson_append_utf8(doc, MDB_PK, MDB_PKLEN, attr->s, attr->len);

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

if (!con)
return -1;

if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
return -1;
Expand Down Expand Up @@ -519,6 +534,9 @@ int mongo_raw_update(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
const bson_value_t *v;
int ret, count = 0;

if (!con)
return -1;

if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
return -1;
Expand Down Expand Up @@ -624,6 +642,9 @@ int mongo_raw_insert(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
const bson_value_t *v;
int ret, count = 0;

if (!con)
return -1;

if (bson_iter_type(ns) != BSON_TYPE_UTF8) {
LM_ERR("collection name must be a string (%d)!\n", bson_iter_type(ns));
return -1;
Expand Down Expand Up @@ -708,6 +729,9 @@ int mongo_raw_remove(cachedb_con *con, bson_t *raw_query, bson_iter_t *ns)
const bson_value_t *v;
int ret, count = 0;

if (!con)
return -1;

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

if (!con)
return -1;

LM_DBG("Get operation on namespace %s\n", MONGO_NAMESPACE(con));
start_expire_timer(start,mongo_exec_threshold);

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

if (!con)
return -1;

cmd = bson_new();
bson_append_utf8(cmd, "findAndModify", 13,
mongoc_collection_get_name(MONGO_COLLECTION(con)), -1);
Expand Down Expand Up @@ -1032,6 +1062,9 @@ int mongo_con_get_counter(cachedb_con *con, str *attr, int *val)
struct timeval start;
int ret = -2;

if (!con)
return -1;

query = bson_new();
#if MONGOC_CHECK_VERSION(1, 5, 0)
bson_append_utf8(query, MDB_PK, MDB_PKLEN, attr->s, attr->len);
Expand Down Expand Up @@ -1217,6 +1250,9 @@ int mongo_db_query_trans(cachedb_con *con, const str *table, const db_key_t *_k,
char *strf, *stro;
str st;

if (!con)
return -1;

*_r = NULL;

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

if (!con)
return -1;

doc = bson_new();
if (kvo_to_bson(_k, _v, NULL, _n, doc) != 0) {
LM_ERR("failed to build bson\n");
Expand Down Expand Up @@ -1565,6 +1604,9 @@ int mongo_db_delete_trans(cachedb_con *con, const str *table,
mongoc_collection_t *col = NULL;
struct timeval start;

if (!con)
return -1;

doc = bson_new();
if (kvo_to_bson(_k, _v, _o, _n, doc) != 0) {
LM_ERR("failed to build bson\n");
Expand Down Expand Up @@ -1615,6 +1657,9 @@ int mongo_db_update_trans(cachedb_con *con, const str *table,
mongoc_collection_t *col = NULL;
struct timeval start;

if (!con)
return -1;

query = bson_new();
if (kvo_to_bson(_k, _v, _o, _n, query) != 0) {
LM_ERR("failed to build query bson\n");
Expand Down Expand Up @@ -1677,6 +1722,9 @@ int mongo_truncate(cachedb_con *con)
struct timeval start;
int ret = 0;

if (!con)
return -1;

start_expire_timer(start, mongo_exec_threshold);
if (!mongoc_collection_remove(MONGO_COLLECTION(con),
MONGOC_REMOVE_NONE, &empty_doc, NULL, &error)) {
Expand Down Expand Up @@ -1894,6 +1942,9 @@ int mongo_con_query(cachedb_con *con, const cdb_filter_t *filter,
const bson_t *doc;
struct timeval start;

if (!con)
return -1;

LM_DBG("find all in %s\n", MONGO_NAMESPACE(con));

cdb_res_init(res);
Expand Down Expand Up @@ -2074,6 +2125,9 @@ int mongo_con_update(cachedb_con *con, const cdb_filter_t *row_filter,
cdb_pair_t *pair;
str key;

if (!con)
return -1;

if (mongo_cdb_filter_to_bson(row_filter, &filter) != 0) {
LM_ERR("failed to build bson filter\n");
return -1;
Expand Down
Loading