Skip to content

Commit 7130d74

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 7130d74

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

src/flb_config.c

Lines changed: 30 additions & 8 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 */
@@ -802,9 +802,8 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
802802
flb_error("[config] section '%s' tried to instance a plugin name "
803803
"that doesn't exist", name);
804804
flb_sds_destroy(name);
805-
return -1;
805+
goto error;
806806
}
807-
flb_sds_destroy(name);
808807

809808
/*
810809
* iterate section properties and populate instance by using specific
@@ -866,6 +865,7 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
866865
flb_error("[config] could not configure property '%s' on "
867866
"%s plugin with section name '%s'",
868867
kv->key, s_type, name);
868+
goto error;
869869
}
870870
}
871871

@@ -875,22 +875,44 @@ static int configure_plugins_type(struct flb_config *config, struct flb_cf *cf,
875875
if (type == FLB_CF_INPUT) {
876876
ret = flb_processors_load_from_config_format_group(((struct flb_input_instance *) ins)->processor, processors);
877877
if (ret == -1) {
878-
return -1;
878+
goto error;
879879
}
880880
}
881881
else if (type == FLB_CF_OUTPUT) {
882882
ret = flb_processors_load_from_config_format_group(((struct flb_output_instance *) ins)->processor, processors);
883883
if (ret == -1) {
884-
return -1;
884+
goto error;
885885
}
886886
}
887887
else {
888888
flb_error("[config] section '%s' does not support processors", s_type);
889889
}
890890
}
891+
892+
flb_sds_destroy(name);
891893
}
892894

893895
return 0;
896+
897+
error:
898+
if (name != NULL) {
899+
flb_sds_destroy(name);
900+
}
901+
if (ins != NULL) {
902+
if (type == FLB_CF_CUSTOM) {
903+
flb_custom_instance_destroy(ins);
904+
}
905+
else if (type == FLB_CF_INPUT) {
906+
flb_input_instance_destroy(ins);
907+
}
908+
else if (type == FLB_CF_FILTER) {
909+
flb_filter_instance_destroy(ins);
910+
}
911+
else if (type == FLB_CF_OUTPUT) {
912+
flb_output_instance_destroy(ins);
913+
}
914+
}
915+
return -1;
894916
}
895917
/* Load a struct flb_config_format context into a flb_config instance */
896918
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)