write_mongodb: fix potential NULL dereference
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Sun, 2 Jul 2017 17:22:20 +0000 (19:22 +0200)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Sun, 2 Jul 2017 17:23:16 +0000 (19:23 +0200)
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

src/write_mongodb.c

index 63d3716..f585a39 100644 (file)
@@ -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;
   }