write_stackdriver plugin: Check message for NULL before calling strdup().
[collectd.git] / src / write_stackdriver.c
index eb60ea9..9d06c25 100644 (file)
@@ -139,14 +139,17 @@ static api_error_t *parse_api_error(char const *body) {
 
   yajl_val code = yajl_tree_get(root, (char const *[]){"error", "code", NULL},
                                 yajl_t_number);
-  if (code != NULL) {
+  if (YAJL_IS_INTEGER(code)) {
     err->code = YAJL_GET_INTEGER(code);
   }
 
   yajl_val message = yajl_tree_get(
       root, (char const *[]){"error", "message", NULL}, yajl_t_string);
-  if (message != NULL) {
-    err->message = strdup(YAJL_GET_STRING(message));
+  if (YAJL_IS_STRING(message)) {
+    char const *m = YAJL_GET_STRING(message);
+    if (m != NULL) {
+      err->message = strdup(m);
+    }
   }
 
   return err;
@@ -187,6 +190,12 @@ static int do_post(wg_callback_t *cb, char const *url, void const *payload,
   curl_easy_setopt(cb->curl, CURLOPT_POST, 1L);
   curl_easy_setopt(cb->curl, CURLOPT_URL, url);
 
+  long timeout_ms = 2 * CDTIME_T_TO_MS(plugin_get_interval());
+  if (timeout_ms < 10000) {
+    timeout_ms = 10000;
+  }
+  curl_easy_setopt(cb->curl, CURLOPT_TIMEOUT_MS, timeout_ms);
+
   /* header */
   char *auth_header = wg_get_authorization_header(cb);
   if (auth_header == NULL) {