X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=ff2e51990f797f5a6256b37c7fe05b3c44618a22;hb=da920bb48c6769e7c58cf358cdfb43983af55056;hp=dd4f9b15ae39a5036cbb0b40f0b8c21624ee473a;hpb=d38c16d60f9d1b10cb60f010ce086135e6ecbe07;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index dd4f9b15..ff2e5199 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -32,14 +32,11 @@ #endif #include "collectd.h" + #include "common.h" #include "plugin.h" #include "utils_cache.h" -#if HAVE_PTHREAD_H -# include -#endif - #ifdef HAVE_MATH_H # include #endif @@ -115,10 +112,9 @@ char *ssnprintf_alloc (char const *format, ...) /* {{{ */ return (strdup (static_buffer)); /* Allocate a buffer large enough to hold the string. */ - alloc_buffer = malloc (alloc_buffer_size); + alloc_buffer = calloc (1, alloc_buffer_size); if (alloc_buffer == NULL) return (NULL); - memset (alloc_buffer, 0, alloc_buffer_size); /* Print again into this new buffer. */ va_start (ap, format); @@ -144,7 +140,7 @@ char *sstrdup (const char *s) /* Do not use `strdup' here, because it's not specified in POSIX. It's * ``only'' an XSI extension. */ sz = strlen (s) + 1; - r = (char *) malloc (sizeof (char) * sz); + r = malloc (sz); if (r == NULL) { ERROR ("sstrdup: Out of memory."); @@ -276,6 +272,9 @@ ssize_t swrite (int fd, const void *buf, size_t count) ptr = (const char *) buf; nleft = count; + if (fd < 0) + return (-1); + /* checking for closed peer connection */ pfd.fd = fd; pfd.events = POLLIN | POLLHUP; @@ -336,7 +335,7 @@ int strjoin (char *buffer, size_t buffer_size, size_t sep_len; size_t i; - if ((buffer_size < 1) || (fields_num <= 0)) + if ((buffer_size < 1) || (fields_num == 0)) return (-1); memset (buffer, 0, buffer_size); @@ -374,27 +373,6 @@ int strjoin (char *buffer, size_t buffer_size, return ((int) strlen (buffer)); } -int strsubstitute (char *str, char c_from, char c_to) -{ - int ret; - - if (str == NULL) - return (-1); - - ret = 0; - while (*str != '\0') - { - if (*str == c_from) - { - *str = c_to; - ret++; - } - str++; - } - - return (ret); -} /* int strsubstitute */ - int escape_string (char *buffer, size_t buffer_size) { char *temp; @@ -409,10 +387,9 @@ int escape_string (char *buffer, size_t buffer_size) if (buffer_size < 3) return (EINVAL); - temp = (char *) malloc (buffer_size); + temp = calloc (1, buffer_size); if (temp == NULL) return (ENOMEM); - memset (temp, 0, buffer_size); temp[0] = '"'; j = 1; @@ -1542,16 +1519,15 @@ int service_name_to_port_number (const char *service_name) { struct addrinfo *ai_list; struct addrinfo *ai_ptr; - struct addrinfo ai_hints; int status; int service_number; if (service_name == NULL) return (-1); - ai_list = NULL; - memset (&ai_hints, 0, sizeof (ai_hints)); - ai_hints.ai_family = AF_UNSPEC; + struct addrinfo ai_hints = { + .ai_family = AF_UNSPEC + }; status = getaddrinfo (/* node = */ NULL, service_name, &ai_hints, &ai_list);