Skip to content

Commit e1fc25d

Browse files
revert: flb_cf_yaml.c take upstream
Signed-off-by: Patrick Stephens <pat@telemetryforge.io>
1 parent f5b2fdd commit e1fc25d

1 file changed

Lines changed: 63 additions & 100 deletions

File tree

source/src/config_format/flb_cf_yaml.c

Lines changed: 63 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ static enum status state_copy_into_properties(struct parser_state *state, struct
918918
struct cfl_list *head;
919919
struct cfl_kvpair *kvp;
920920
struct cfl_variant *var;
921-
struct cfl_variant *variant;
922921
struct cfl_array *arr;
923922
size_t idx;
924923
size_t entry_count;
@@ -941,93 +940,57 @@ static enum status state_copy_into_properties(struct parser_state *state, struct
941940
}
942941
break;
943942
case CFL_VARIANT_ARRAY:
944-
/* For variant arrays from filter/processor sections, create a deep copy and insert directly */
945-
{
946-
struct cfl_array *src_array = kvp->val->data.as_array;
947-
struct cfl_array *new_array = cfl_array_create(src_array->entry_count);
943+
entry_count = kvp->val->data.as_array->entry_count;
944+
array_all_strings = 1;
945+
946+
for (idx = 0; idx < entry_count; idx++) {
947+
var = cfl_array_fetch_by_index(kvp->val->data.as_array, idx);
948+
if (var == NULL || var->type != CFL_VARIANT_STRING) {
949+
array_all_strings = 0;
950+
break;
951+
}
952+
}
948953

949-
if (new_array == NULL) {
950-
flb_error("unable to create array for variant property");
954+
if (array_all_strings == 1) {
955+
arr = flb_cf_section_property_add_list(conf, properties,
956+
kvp->key, cfl_sds_len(kvp->key));
957+
958+
if (arr == NULL) {
959+
flb_error("unable to add property list");
951960
return YAML_FAILURE;
952961
}
953-
cfl_array_resizable(new_array, CFL_TRUE);
954962

955-
/* Deep copy all array elements */
956-
for (idx = 0; idx < src_array->entry_count; idx++) {
957-
var = cfl_array_fetch_by_index(src_array, idx);
958-
if (var == NULL) {
959-
flb_error("unable to retrieve from array by index");
960-
cfl_array_destroy(new_array);
961-
return YAML_FAILURE;
962-
}
963+
for (idx = 0; idx < entry_count; idx++) {
964+
var = cfl_array_fetch_by_index(kvp->val->data.as_array, idx);
963965

964-
switch (var->type) {
965-
case CFL_VARIANT_STRING:
966-
if (cfl_array_append_string(new_array, var->data.as_string) < 0) {
967-
flb_error("unable to append string to array");
968-
cfl_array_destroy(new_array);
969-
return YAML_FAILURE;
970-
}
971-
break;
972-
case CFL_VARIANT_KVLIST:
973-
{
974-
struct cfl_kvlist *kvlist_copy = kvlist_deep_copy(var->data.as_kvlist);
975-
if (kvlist_copy == NULL) {
976-
flb_error("unable to deep copy kvlist in array");
977-
cfl_array_destroy(new_array);
978-
return YAML_FAILURE;
979-
}
980-
if (cfl_array_append_kvlist(new_array, kvlist_copy) < 0) {
981-
flb_error("unable to append kvlist to array");
982-
cfl_kvlist_destroy(kvlist_copy);
983-
cfl_array_destroy(new_array);
984-
return YAML_FAILURE;
985-
}
986-
}
987-
break;
988-
default:
989-
flb_error("unsupported array element type: %d", var->type);
990-
cfl_array_destroy(new_array);
966+
if (cfl_array_append_string(arr, var->data.as_string) < 0) {
967+
flb_error("unable to append string to array");
991968
return YAML_FAILURE;
992969
}
993970
}
994-
995-
/* Create variant from the new array and insert it */
996-
variant = cfl_variant_create_from_array(new_array);
997-
if (variant == NULL) {
998-
flb_error("unable to create variant from array");
999-
cfl_array_destroy(new_array);
1000-
return YAML_FAILURE;
1001-
}
1002-
1003-
if (cfl_kvlist_insert(properties, kvp->key, variant) < 0) {
1004-
flb_error("unable to insert array variant property");
1005-
cfl_variant_destroy(variant);
971+
}
972+
else {
973+
if (flb_cf_section_property_add_variant(conf,
974+
properties,
975+
kvp->key,
976+
cfl_sds_len(kvp->key),
977+
kvp->val) == NULL) {
978+
flb_error("unable to add variant property");
1006979
return YAML_FAILURE;
1007980
}
981+
kvp->val = NULL;
1008982
}
1009983
break;
1010984
case CFL_VARIANT_KVLIST:
1011-
/* Handle variant objects (nested properties) */
1012-
/* Create a deep copy of the kvlist to avoid use-after-free */
1013-
{
1014-
struct cfl_kvlist *kvlist_copy = kvlist_deep_copy(kvp->val->data.as_kvlist);
1015-
if (kvlist_copy == NULL) {
1016-
flb_error("unable to deep copy kvlist");
1017-
return YAML_FAILURE;
1018-
}
1019-
variant = cfl_variant_create_from_kvlist(kvlist_copy);
1020-
if (variant == NULL) {
1021-
flb_error("unable to create variant for kvlist");
1022-
cfl_kvlist_destroy(kvlist_copy);
1023-
return YAML_FAILURE;
1024-
}
1025-
if (cfl_kvlist_insert(properties, kvp->key, variant) < 0) {
1026-
flb_error("unable to add variant property");
1027-
cfl_variant_destroy(variant);
1028-
return YAML_FAILURE;
1029-
}
985+
if (flb_cf_section_property_add_variant(conf,
986+
properties,
987+
kvp->key,
988+
cfl_sds_len(kvp->key),
989+
kvp->val) == NULL) {
990+
flb_error("unable to add variant property");
991+
return YAML_FAILURE;
1030992
}
993+
kvp->val = NULL;
1031994
break;
1032995
default:
1033996
flb_error("unknown value type for properties: %d", kvp->val->type);
@@ -2089,29 +2052,6 @@ static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
20892052
case YAML_MAPPING_END_EVENT:
20902053
print_current_properties(state);
20912054

2092-
if (strcmp(state->key, "processors") == 0) {
2093-
struct flb_cf_group *group;
2094-
2095-
group = flb_cf_group_create(conf, state->cf_section,
2096-
state->key,
2097-
strlen(state->key));
2098-
2099-
if (group == NULL) {
2100-
flb_error("unable to create processors group");
2101-
return YAML_FAILURE;
2102-
}
2103-
2104-
state->cf_group = group;
2105-
state = state_push(ctx, STATE_INPUT_PROCESSORS);
2106-
2107-
if (state == NULL) {
2108-
flb_error("unable to allocate state");
2109-
return YAML_FAILURE;
2110-
}
2111-
2112-
break;
2113-
}
2114-
21152055
if (state->section == SECTION_PROCESSOR) {
21162056
status = state_move_into_config_group(state, state->cf_group);
21172057

@@ -2257,8 +2197,32 @@ static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
22572197
}
22582198
break;
22592199
case YAML_MAPPING_START_EVENT:
2200+
2201+
if (strcmp(state->key, "processors") == 0) {
2202+
struct flb_cf_group *group;
2203+
2204+
group = flb_cf_group_create(conf, state->cf_section,
2205+
state->key,
2206+
strlen(state->key));
2207+
2208+
if (group == NULL) {
2209+
flb_error("unable to create processors group");
2210+
return YAML_FAILURE;
2211+
}
2212+
2213+
state->cf_group = group;
2214+
state = state_push(ctx, STATE_INPUT_PROCESSORS);
2215+
2216+
if (state == NULL) {
2217+
flb_error("unable to allocate state");
2218+
return YAML_FAILURE;
2219+
}
2220+
2221+
break;
2222+
}
2223+
22602224
if (state->section == SECTION_PROCESSOR || state->section == SECTION_FILTER) {
2261-
/* when in a processor or filter section, allow plugins to have nested
2225+
/* when in a processor or filter section, we allow plugins to have nested
22622226
* properties which are returned as a cfl_variant */
22632227
state = state_push_variant(ctx, state, 1);
22642228

@@ -2270,8 +2234,7 @@ static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
22702234
}
22712235

22722236
if (strcmp(state->key, "routes") == 0 ||
2273-
strcmp(state->key, "processors") == 0 ||
2274-
strcmp(state->key, "filters") == 0) {
2237+
strcmp(state->key, "processors") == 0 ) {
22752238
state = state_push_variant(ctx, state, 1);
22762239

22772240
if (state == NULL) {

0 commit comments

Comments
 (0)