From 0e8a72aec282c9a30ce8be09d3cb5e7060a1f15e Mon Sep 17 00:00:00 2001 From: Bodo Petermann Date: Fri, 27 Dec 2024 12:41:40 +0000 Subject: [PATCH] in_systemd: fix memory leak Fix a memory leak in the systemd input plugin. If option "lowercase" was on, the temporary buffer to convert a key to the lower-case string was not freed. Signed-off-by: Bodo Petermann --- plugins/in_systemd/systemd.c | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/plugins/in_systemd/systemd.c b/plugins/in_systemd/systemd.c index 08ba75d210e..11dbae55d63 100644 --- a/plugins/in_systemd/systemd.c +++ b/plugins/in_systemd/systemd.c @@ -138,7 +138,7 @@ static int systemd_enumerate_data_store(struct flb_config *config, const char *sep; const char *key; const char *val; - char *buf = NULL; + char *buf; struct cfl_kvlist *kvlist = format_context; struct flb_systemd_config *ctx = plugin_context; struct cfl_variant *cfl_val = NULL; @@ -155,31 +155,19 @@ static int systemd_enumerate_data_store(struct flb_config *config, len = (sep - key); key_len = len; - - if (ctx->lowercase == FLB_TRUE) { - /* - * Ensure buf to have enough space for the key because the libsystemd - * might return larger data than the threshold. - */ - if (buf == NULL) { - buf = flb_sds_create_len(NULL, ctx->threshold); - } - if (flb_sds_alloc(buf) < len) { - buf = flb_sds_increase(buf, len - flb_sds_alloc(buf)); - } - for (i = 0; i < len; i++) { - buf[i] = tolower(key[i]); - } - list_key = flb_sds_create_len(buf, key_len); - } - else { - list_key = flb_sds_create_len(key, key_len); - } + list_key = flb_sds_create_len(key, key_len); if (!list_key) { return -1; } + if (ctx->lowercase == FLB_TRUE) { + buf = list_key; + for (i = 0; i < key_len; i++) { + buf[i] = tolower(buf[i]); + } + } + /* Check existence */ cfl_val = NULL; cfl_val = cfl_kvlist_fetch_s(kvlist, list_key, key_len); @@ -274,7 +262,6 @@ static int in_systemd_collect(struct flb_input_instance *ins, uint64_t usec; size_t length; const char *key; - char *buf = NULL; #ifdef FLB_HAVE_SQLDB char *cursor = NULL; #endif @@ -458,8 +445,6 @@ static int in_systemd_collect(struct flb_input_instance *ins, } } - flb_sds_destroy(buf); - #ifdef FLB_HAVE_SQLDB /* Save cursor */ if (ctx->db) {