From 86605e019eba9d15b973a5721e393e79fc8c5ff2 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 26 Sep 2024 18:04:10 +0900 Subject: [PATCH 1/3] config_format: cf_yaml: Plug memory leaks on exception for variants on plugin elements Signed-off-by: Hiroshi Hatake --- src/config_format/flb_cf_yaml.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/config_format/flb_cf_yaml.c b/src/config_format/flb_cf_yaml.c index c2ec53abc0c..c7c88ae5e8d 100644 --- a/src/config_format/flb_cf_yaml.c +++ b/src/config_format/flb_cf_yaml.c @@ -2083,6 +2083,15 @@ static struct parser_state *state_pop(struct local_ctx *ctx) cfl_kvlist_destroy(last->keyvals); } + /* 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) { From f5348a576aec0e299da8f3befc3178fec6944e3d Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 26 Sep 2024 18:42:42 +0900 Subject: [PATCH 2/3] config_format: cf_yaml: tests: Add a test case for broken plugin variant Signed-off-by: Hiroshi Hatake --- tests/internal/config_format_yaml.c | 16 +++++++++ .../yaml/broken_plugin_variant.yaml | 34 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/internal/data/config_format/yaml/broken_plugin_variant.yaml diff --git a/tests/internal/config_format_yaml.c b/tests/internal/config_format_yaml.c index 6f1fc488a6a..f34bdb273d4 100644 --- a/tests/internal/config_format_yaml.c +++ b/tests/internal/config_format_yaml.c @@ -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 @@ -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; @@ -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}, diff --git a/tests/internal/data/config_format/yaml/broken_plugin_variant.yaml b/tests/internal/data/config_format/yaml/broken_plugin_variant.yaml new file mode 100644 index 00000000000..65a5160013d --- /dev/null +++ b/tests/internal/data/config_format/yaml/broken_plugin_variant.yaml @@ -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 \ No newline at end of file From 01ffe3b4ffa1b9b27df9f8431e693d93fa9a73b0 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 26 Sep 2024 19:07:53 +0900 Subject: [PATCH 3/3] config_format: cf_yaml: Cleanup variants only if occurred an exception Signed-off-by: Hiroshi Hatake --- src/config_format/flb_cf_yaml.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/config_format/flb_cf_yaml.c b/src/config_format/flb_cf_yaml.c index c7c88ae5e8d..e09034393f3 100644 --- a/src/config_format/flb_cf_yaml.c +++ b/src/config_format/flb_cf_yaml.c @@ -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; @@ -2083,13 +2083,15 @@ static struct parser_state *state_pop(struct local_ctx *ctx) cfl_kvlist_destroy(last->keyvals); } - /* Teardown associated variant stuffs */ - if (last->variant_kvlist_key != NULL) { - cfl_sds_destroy(last->variant_kvlist_key); - } + 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); + if (last->variant != NULL) { + cfl_variant_destroy(last->variant); + } } state_destroy(last); @@ -2101,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); @@ -2273,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);