Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config_format: cf_yaml: Plug memory leaks on exception for processing variants on plugin elements #9426

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/config_format/flb_cf_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ static int state_create_group(struct flb_cf *conf, struct parser_state *state, c
return YAML_SUCCESS;
}

static struct parser_state *state_pop(struct local_ctx *ctx)
static struct parser_state *state_pop_with_cleanup(struct local_ctx *ctx, int destroy_variants)
{
struct parser_state *last;

Expand All @@ -2083,6 +2083,17 @@ static struct parser_state *state_pop(struct local_ctx *ctx)
cfl_kvlist_destroy(last->keyvals);
}

if (destroy_variants == FLB_TRUE) {
/* Teardown associated variant stuffs */
if (last->variant_kvlist_key != NULL) {
cfl_sds_destroy(last->variant_kvlist_key);
}

if (last->variant != NULL) {
cfl_variant_destroy(last->variant);
}
}

state_destroy(last);

if (cfl_list_size(&ctx->states) <= 0) {
Expand All @@ -2092,6 +2103,11 @@ static struct parser_state *state_pop(struct local_ctx *ctx)
return cfl_list_entry_last(&ctx->states, struct parser_state, _head);
}

static struct parser_state *state_pop(struct local_ctx *ctx)
{
return state_pop_with_cleanup(ctx, FLB_FALSE);
}

static void state_destroy(struct parser_state *s)
{
flb_free(s);
Expand Down Expand Up @@ -2264,7 +2280,7 @@ static int read_config(struct flb_cf *conf, struct local_ctx *ctx,

/* free all remaining states */
if (code == -1) {
while ((state = state_pop(ctx)));
while ((state = state_pop_with_cleanup(ctx, FLB_TRUE)));
}
else {
state = state_pop(ctx);
Expand Down
16 changes: 16 additions & 0 deletions tests/internal/config_format_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define FLB_001 FLB_TESTS_CONF_PATH "/issue_7559.yaml"
#define FLB_002 FLB_TESTS_CONF_PATH "/processors.yaml"
#define FLB_000_WIN FLB_TESTS_CONF_PATH "\\fluent-bit-windows.yaml"
#define FLB_BROKEN_PLUGIN_VARIANT FLB_TESTS_CONF_PATH "/broken_plugin_variant.yaml"

#ifdef _WIN32
#define FLB_BASIC FLB_000_WIN
Expand Down Expand Up @@ -180,6 +181,20 @@ static void test_customs_section()
flb_cf_destroy(cf);
}

static void test_broken_plugin_variant_yaml()
{
struct flb_cf *cf;

cf = flb_cf_yaml_create(NULL, FLB_BROKEN_PLUGIN_VARIANT, NULL, 0);
TEST_CHECK(cf == NULL);

if (cf != NULL) {
TEST_CHECK_(cf != NULL, "somewhat config_format is created wrongly");
flb_cf_dump(cf);
flb_cf_destroy(cf);
}
}

static void test_slist_even()
{
struct flb_cf *cf;
Expand Down Expand Up @@ -443,6 +458,7 @@ static void test_processors()
TEST_LIST = {
{ "basic" , test_basic},
{ "customs section", test_customs_section},
{ "broken_plugin_variant_yaml", test_broken_plugin_variant_yaml},
{ "slist odd", test_slist_odd},
{ "slist even", test_slist_even},
{ "parsers file conf", test_parser_conf},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
env:
flush_interval: 1

service:
http_server: "on"
Health_Check: "on"
log_level: info

pipeline:
inputs:
- name: event_type
tag: event
type: logs
processors:
logs:
- name: log_replacer
replacement:
some_extra_data:
unsupported:
hi: * # this character should be quoted
some_string: "hello world"
unquoted_literals:
- some_int: 4
- some_float: 3.1
- some_bool: true
quoted_literals:
- some_quoted_int: "4"
- some_quoted_float: '3.1'
- some_quoted_bool: "true"

outputs:
- name: stdout
format: json
match: event
Loading