Skip to content

Commit

Permalink
in_systemd: fix memory leak
Browse files Browse the repository at this point in the history
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 <b.petermann@syseleven.de>
  • Loading branch information
bpetermannS11 committed Dec 27, 2024
1 parent d77c06d commit 0e8a72a
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions plugins/in_systemd/systemd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0e8a72a

Please sign in to comment.