From aa466a2980741a16f6635428a11ee34a7f2357d2 Mon Sep 17 00:00:00 2001 From: shifter Date: Mon, 8 Jul 2024 12:59:01 +0200 Subject: [PATCH 1/3] filterx: parse-csv usage fix: add missing string-delimiters description Signed-off-by: shifter --- modules/csvparser/filterx-func-parse-csv.h | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/csvparser/filterx-func-parse-csv.h b/modules/csvparser/filterx-func-parse-csv.h index 2d2442d3fb..0f41151d21 100644 --- a/modules/csvparser/filterx-func-parse-csv.h +++ b/modules/csvparser/filterx-func-parse-csv.h @@ -35,6 +35,7 @@ #define FILTERX_FUNC_PARSE_CSV_USAGE "Usage: parse_csv(msg_str [" \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_COLUMNS"=json_array, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"=string, " \ + FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"=json_array, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_DIALECT"=string, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRIP_WHITESPACES"=boolean, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_GREEDY"=boolean])" From 2543c7931fa3586d8820f3268c5fd56a58245bab Mon Sep 17 00:00:00 2001 From: shifter Date: Mon, 8 Jul 2024 13:02:06 +0200 Subject: [PATCH 2/3] filterx: parse-csv: add empty delimiter handling Since the string-delimiters feature was added, the delimiter argument must be allowed to be set to empty to avoid parsing side effects. Signed-off-by: shifter --- modules/csvparser/filterx-func-parse-csv.c | 4 ++-- modules/csvparser/filterx-func-parse-csv.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/csvparser/filterx-func-parse-csv.c b/modules/csvparser/filterx-func-parse-csv.c index 2c9f4d80bd..26895ac2c7 100644 --- a/modules/csvparser/filterx-func-parse-csv.c +++ b/modules/csvparser/filterx-func-parse-csv.c @@ -229,9 +229,9 @@ _extract_opts(FilterXFunctionParseCSV *self, FilterXFunctionArgs *args, GError * value = filterx_function_args_get_named_literal_string(args, FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER, &len, &exists); if (exists) { - if (len < 1) + if (len < 1 && !self->string_delimiters) { - error_str = FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER " can not be empty"; + error_str = FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER; goto error; } if (!value) diff --git a/modules/csvparser/filterx-func-parse-csv.h b/modules/csvparser/filterx-func-parse-csv.h index 0f41151d21..ae66c03b43 100644 --- a/modules/csvparser/filterx-func-parse-csv.h +++ b/modules/csvparser/filterx-func-parse-csv.h @@ -39,6 +39,11 @@ FILTERX_FUNC_PARSE_CSV_ARG_NAME_DIALECT"=string, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRIP_WHITESPACES"=boolean, " \ FILTERX_FUNC_PARSE_CSV_ARG_NAME_GREEDY"=boolean])" +#define FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER "Either '" \ + FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"' or '" \ + FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"' must be set, and '" \ + FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER"' cannot be empty if '" \ + FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS"' is unset" FilterXExpr *filterx_function_parse_csv_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error); gpointer filterx_function_construct_parse_csv(Plugin *self); From a3b64e415d41422ce916d33c07f69e6f6d628851 Mon Sep 17 00:00:00 2001 From: shifter Date: Mon, 8 Jul 2024 13:02:38 +0200 Subject: [PATCH 3/3] filterx: parse-csv: add unit tests for empty delimiter option Signed-off-by: shifter --- .../tests/test_filterx_func_parse_csv.c | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/modules/csvparser/tests/test_filterx_func_parse_csv.c b/modules/csvparser/tests/test_filterx_func_parse_csv.c index a835a5c4b7..10212acd6e 100644 --- a/modules/csvparser/tests/test_filterx_func_parse_csv.c +++ b/modules/csvparser/tests/test_filterx_func_parse_csv.c @@ -454,6 +454,58 @@ Test(filterx_func_parse_csv, test_optional_argument_string_delimiters_and_delimi g_error_free(err); } +Test(filterx_func_parse_csv, test_optional_argument_delimiter_default_unset_when_string_delimiters_set) +{ + GList *args = NULL; + args = g_list_append(args, filterx_function_arg_new(NULL, + filterx_literal_new(filterx_string_new("testfoobar,baz", -1)))); + FilterXObject *string_delimiters = _generate_string_list("foo", NULL); + args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_STRING_DELIMITERS, + filterx_literal_new(string_delimiters))); + args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER, + filterx_literal_new(filterx_string_new("", -1)))); + + GError *err = NULL; + GError *args_err = NULL; + FilterXExpr *func = filterx_function_parse_csv_new("test", filterx_function_args_new(args, &args_err), &err); + cr_assert_null(args_err); + cr_assert_null(err); + + FilterXObject *obj = filterx_expr_eval(func); + + cr_assert_not_null(obj); + cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(json_array))); + + FilterXObject *elt = filterx_list_get_subscript(obj, 1); + + GString *repr = scratch_buffers_alloc(); + cr_assert(filterx_object_repr(elt, repr)); + + cr_assert_str_eq(repr->str, "bar,baz"); + filterx_object_unref(elt); + filterx_expr_unref(func); + filterx_object_unref(obj); + g_error_free(err); +} + +Test(filterx_func_parse_csv, test_optional_argument_delimiter_unable_to_set_with_empty_string_delimiters) +{ + GList *args = NULL; + args = g_list_append(args, filterx_function_arg_new(NULL, + filterx_literal_new(filterx_string_new("testfoobar,baz", -1)))); + args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_PARSE_CSV_ARG_NAME_DELIMITER, + filterx_literal_new(filterx_string_new("", -1)))); + + GError *err = NULL; + GError *args_err = NULL; + FilterXExpr *func = filterx_function_parse_csv_new("test", filterx_function_args_new(args, &args_err), &err); + cr_assert_null(args_err); + cr_assert_not_null(err); + cr_assert(strcmp(err->message, FILTERX_FUNC_PARSE_ERR_EMPTY_DELIMITER)); + + filterx_expr_unref(func); + g_error_free(err); +} static void setup(void)