From: Florian Forster Date: Fri, 19 Jun 2020 09:35:19 +0000 (+0200) Subject: common: Use the strbuf library in format_values(). X-Git-Tag: 6.0.0-rc0~148^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d5ab3caef61986dd205eb8a2eceb638990c49d6;p=thirdparty%2Fcollectd.git common: Use the strbuf library in format_values(). --- diff --git a/Makefile.am b/Makefile.am index 21273f7e5..5beddafe3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -399,7 +399,7 @@ libavltree_la_SOURCES = \ libcommon_la_SOURCES = \ src/utils/common/common.c \ src/utils/common/common.h -libcommon_la_LIBADD = $(COMMON_LIBS) +libcommon_la_LIBADD = libstrbuf.la $(COMMON_LIBS) libheap_la_SOURCES = \ src/utils/heap/heap.c \ diff --git a/src/utils/common/common.c b/src/utils/common/common.c index 2a205537b..4a106897c 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -31,6 +31,7 @@ #include "plugin.h" #include "utils/common/common.h" +#include "utils/strbuf/strbuf.h" #include "utils_cache.h" /* for getaddrinfo */ @@ -205,7 +206,7 @@ char *sstrerror(int errnum, char *buf, size_t buflen) { pthread_mutex_unlock(&strerror_r_lock); } - /* #endif !HAVE_STRERROR_R */ + /* #endif !HAVE_STRERROR_R */ #elif STRERROR_R_CHAR_P { @@ -221,7 +222,7 @@ char *sstrerror(int errnum, char *buf, size_t buflen) { buflen); } } - /* #endif STRERROR_R_CHAR_P */ + /* #endif STRERROR_R_CHAR_P */ #else if (strerror_r(errnum, buf, buflen) != 0) { @@ -793,8 +794,8 @@ unsigned long long htonll(unsigned long long n) { #endif /* HAVE_HTONLL */ #if FP_LAYOUT_NEED_NOTHING - /* Well, we need nothing.. */ - /* #endif FP_LAYOUT_NEED_NOTHING */ +/* Well, we need nothing.. */ +/* #endif FP_LAYOUT_NEED_NOTHING */ #elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP #if FP_LAYOUT_NEED_ENDIANFLIP @@ -905,46 +906,31 @@ int format_name(char *ret, int ret_len, const char *hostname, int format_values(char *ret, size_t ret_len, /* {{{ */ const data_set_t *ds, const value_list_t *vl, bool store_rates) { - size_t offset = 0; - int status; - gauge_t *rates = NULL; - assert(0 == strcmp(ds->type, vl->type)); - memset(ret, 0, ret_len); - -#define BUFFER_ADD(...) \ - do { \ - status = snprintf(ret + offset, ret_len - offset, __VA_ARGS__); \ - if (status < 1) { \ - sfree(rates); \ - return -1; \ - } else if (((size_t)status) >= (ret_len - offset)) { \ - sfree(rates); \ - return -1; \ - } else \ - offset += ((size_t)status); \ - } while (0) + ret[0] = 0; + strbuf_t buf = STRBUF_CREATE_FIXED(ret, ret_len); - BUFFER_ADD("%.3f", CDTIME_T_TO_DOUBLE(vl->time)); + strbuf_printf(&buf, "%.3f", CDTIME_T_TO_DOUBLE(vl->time)); + gauge_t *rates = NULL; for (size_t i = 0; i < ds->ds_num; i++) { - if (ds->ds[i].type == DS_TYPE_GAUGE) - BUFFER_ADD(":" GAUGE_FORMAT, vl->values[i].gauge); - else if (store_rates) { + if (ds->ds[i].type == DS_TYPE_GAUGE) { + strbuf_printf(&buf, ":" GAUGE_FORMAT, vl->values[i].gauge); + } else if (store_rates) { if (rates == NULL) rates = uc_get_rate(ds, vl); if (rates == NULL) { WARNING("format_values: uc_get_rate failed."); return -1; } - BUFFER_ADD(":" GAUGE_FORMAT, rates[i]); + strbuf_printf(&buf, ":" GAUGE_FORMAT, rates[i]); } else if (ds->ds[i].type == DS_TYPE_COUNTER) - BUFFER_ADD(":%" PRIu64, (uint64_t)vl->values[i].counter); + strbuf_printf(&buf, ":%" PRIu64, (uint64_t)vl->values[i].counter); else if (ds->ds[i].type == DS_TYPE_DERIVE) - BUFFER_ADD(":%" PRIi64, vl->values[i].derive); + strbuf_printf(&buf, ":%" PRIi64, vl->values[i].derive); else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) - BUFFER_ADD(":%" PRIu64, vl->values[i].absolute); + strbuf_printf(&buf, ":%" PRIu64, vl->values[i].absolute); else { ERROR("format_values: Unknown data source type: %i", ds->ds[i].type); sfree(rates); @@ -952,8 +938,6 @@ int format_values(char *ret, size_t ret_len, /* {{{ */ } } /* for ds->ds_num */ -#undef BUFFER_ADD - sfree(rates); return 0; } /* }}} int format_values */