From: Ruben Kerkhof Date: Sun, 2 Jul 2017 17:22:20 +0000 (+0200) Subject: write_mongodb: fix potential NULL dereference X-Git-Tag: collectd-5.8.0~131 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=2c59754399236dee51c68b0b624242c61f89c4c0;p=collectd.git write_mongodb: fix potential NULL dereference scan-build: Using '/usr/bin/clang-4.0' for static analysis make all-am make[1]: Entering directory '/home/ruben/src/collectd' CC src/write_mongodb_la-write_mongodb.lo src/write_mongodb.c:173:41: warning: Null pointer passed as an argument to a 'nonnull' parameter strlen(node->passwd) + strlen(node->host) + 5 + ^~~~~~~~~~~~~~~~~~ src/write_mongodb.c:199:42: warning: Null pointer passed as an argument to a 'nonnull' parameter uri_length = strlen(format_string) + strlen(node->host) + 5 + 1; ^~~~~~~~~~~~~~~~~~ 2 warnings generated. CCLD write_mongodb.la --- diff --git a/src/write_mongodb.c b/src/write_mongodb.c index 63d3716b..f585a396 100644 --- a/src/write_mongodb.c +++ b/src/write_mongodb.c @@ -163,9 +163,7 @@ static int wm_initialize(wm_node_t *node) /* {{{ */ return 0; } - INFO("write_mongodb plugin: Connecting to [%s]:%i", - (node->host != NULL) ? node->host : "localhost", - (node->port != 0) ? node->port : MONGOC_DEFAULT_PORT); + INFO("write_mongodb plugin: Connecting to [%s]:%d", node->host, node->port); if ((node->db != NULL) && (node->user != NULL) && (node->passwd != NULL)) { format_string = "mongodb://%s:%s@%s:%d/?authSource=%s"; @@ -185,11 +183,9 @@ static int wm_initialize(wm_node_t *node) /* {{{ */ node->client = mongoc_client_new(uri); if (!node->client) { - ERROR("write_mongodb plugin: Authenticating to [%s]%i for database " + ERROR("write_mongodb plugin: Authenticating to [%s]:%d for database " "\"%s\" as user \"%s\" failed.", - (node->host != NULL) ? node->host : "localhost", - (node->port != 0) ? node->port : MONGOC_DEFAULT_PORT, node->db, - node->user); + node->host, node->port, node->db, node->user); node->connected = 0; sfree(uri); return -1; @@ -209,9 +205,8 @@ static int wm_initialize(wm_node_t *node) /* {{{ */ node->client = mongoc_client_new(uri); if (!node->client) { - ERROR("write_mongodb plugin: Connecting to [%s]:%i failed.", - (node->host != NULL) ? node->host : "localhost", - (node->port != 0) ? node->port : MONGOC_DEFAULT_PORT); + ERROR("write_mongodb plugin: Connecting to [%s]:%d failed.", node->host, + node->port); node->connected = 0; sfree(uri); return -1; @@ -320,13 +315,19 @@ static int wm_config_node(oconfig_item_t *ci) /* {{{ */ if (node == NULL) return ENOMEM; mongoc_init(); - node->host = NULL; + node->host = strdup("localhost"); + if (node->host == NULL) { + sfree(node); + return ENOMEM; + } + node->port = MONGOC_DEFAULT_PORT; node->store_rates = 1; pthread_mutex_init(&node->lock, /* attr = */ NULL); status = cf_util_get_string_buffer(ci, node->name, sizeof(node->name)); if (status != 0) { + sfree(node->host); sfree(node); return status; }