Skip to content

Commit 48f4886

Browse files
committed
config: error configure_plugins_type on invalid properties
After this change, when a plugin configuration contains an invalid property, `configure_plugins_type` returns an error which ultimately crashes fluentbit, instead of only printing an error log and continuing on. The change also fixes a small issue where `name` was printed by `flb_error` after it was freed. To properly test this change and get the test to pass with `-DSANITIZE_ADDRESS=On` on all platforms, I had to update `configure_plugins_type` to free objects that it fails to instantiate. Signed-off-by: Bradley Laney <[email protected]>
1 parent f28e61e commit 48f4886

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

src/flb_config.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
737737
{
738738
int ret;
739739
char *tmp;
740-
char *name;
740+
char *name = NULL;
741741
char *s_type;
742742
struct mk_list *list;
743743
struct mk_list *head;
@@ -747,7 +747,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
747747
struct flb_cf_section *s;
748748
struct flb_cf_group *processors = NULL;
749749
int i;
750-
void *ins;
750+
void *ins = NULL;
751751

752752
if (type == FLB_CF_CUSTOM) {
753753
s_type = "custom";
@@ -766,7 +766,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
766766
list = &cf->outputs;
767767
}
768768
else {
769-
return -1;
769+
goto error;
770770
}
771771

772772
mk_list_foreach(head, list) {
@@ -775,7 +775,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
775775
if (!name) {
776776
flb_error("[config] section '%s' is missing the 'name' property",
777777
s_type);
778-
return -1;
778+
goto error;
779779
}
780780

781781
/* translate the variable */
@@ -801,10 +801,8 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
801801
if (!ins) {
802802
flb_error("[config] section '%s' tried to instance a plugin name "
803803
"that doesn't exist", name);
804-
flb_sds_destroy(name);
805-
return -1;
804+
goto error;
806805
}
807-
flb_sds_destroy(name);
808806

809807
/*
810808
* iterate section properties and populate instance by using specific
@@ -866,6 +864,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
866864
flb_error("[config] could not configure property '%s' on "
867865
"%s plugin with section name '%s'",
868866
kv->key, s_type, name);
867+
goto error;
869868
}
870869
}
871870

@@ -875,22 +874,44 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
875874
if (type == FLB_CF_INPUT) {
876875
ret = flb_processors_load_from_config_format_group(((struct flb_input_instance *) ins)->processor, processors);
877876
if (ret == -1) {
878-
return -1;
877+
goto error;
879878
}
880879
}
881880
else if (type == FLB_CF_OUTPUT) {
882881
ret = flb_processors_load_from_config_format_group(((struct flb_output_instance *) ins)->processor, processors);
883882
if (ret == -1) {
884-
return -1;
883+
goto error;
885884
}
886885
}
887886
else {
888887
flb_error("[config] section '%s' does not support processors", s_type);
889888
}
890889
}
890+
891+
flb_sds_destroy(name);
891892
}
892893

893894
return 0;
895+
896+
error:
897+
if (name != NULL) {
898+
flb_sds_destroy(name);
899+
}
900+
if (ins != NULL) {
901+
if (type == FLB_CF_CUSTOM) {
902+
flb_custom_instance_destroy(ins);
903+
}
904+
else if (type == FLB_CF_INPUT) {
905+
flb_input_instance_destroy(ins);
906+
}
907+
else if (type == FLB_CF_FILTER) {
908+
flb_filter_instance_destroy(ins);
909+
}
910+
else if (type == FLB_CF_OUTPUT) {
911+
flb_output_instance_destroy(ins);
912+
}
913+
}
914+
return -1;
894915
}
895916
/* Load a struct flb_config_format context into a flb_config instance */
896917
int flb_config_load_config_format(struct flb_config *config, struct flb_cf *cf)

tests/internal/config_format_yaml.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
22

33
#include <fluent-bit/flb_info.h>
4+
#include <fluent-bit/flb_input.h>
5+
#include <fluent-bit/flb_output.h>
46
#include <fluent-bit/flb_mem.h>
57
#include <fluent-bit/flb_kv.h>
68
#include <fluent-bit/flb_str.h>
@@ -833,6 +835,34 @@ static void test_upstream_servers()
833835
flb_cf_destroy(cf);
834836
}
835837

838+
static void test_invalid_property()
839+
{
840+
char* test_cases[] = {
841+
FLB_TESTS_CONF_PATH "/invalid_input_property.yaml",
842+
FLB_TESTS_CONF_PATH "/invalid_output_property.yaml",
843+
NULL,
844+
};
845+
846+
struct flb_cf *cf;
847+
struct flb_config *config;
848+
int ret;
849+
int i;
850+
851+
for (i = 0; test_cases[i] != NULL; i++) {
852+
cf = flb_cf_yaml_create(NULL, test_cases[i], NULL, 0);
853+
TEST_ASSERT(cf != NULL);
854+
855+
config = flb_config_init();
856+
TEST_ASSERT(config != NULL);
857+
858+
ret = flb_config_load_config_format(config, cf);
859+
TEST_ASSERT_(ret == -1, "expected invalid property to return an error in file %s", test_cases[i]);
860+
861+
flb_config_exit(config);
862+
flb_cf_destroy(cf);
863+
}
864+
}
865+
836866
TEST_LIST = {
837867
{ "basic" , test_basic},
838868
{ "customs section", test_customs_section},
@@ -846,5 +876,6 @@ TEST_LIST = {
846876
{ "stream_processor", test_stream_processor},
847877
{ "plugins", test_plugins},
848878
{ "upstream_servers", test_upstream_servers},
879+
{ "invalid_input_property", test_invalid_property},
849880
{ 0 }
850881
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pipeline:
2+
inputs:
3+
- name: dummy
4+
log_level: thisdoesnotexist
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pipeline:
2+
outputs:
3+
- name: stdout
4+
match: '*'
5+
log_level: thisdoesnotexist

0 commit comments

Comments
 (0)