From 70d61f8be40a59d3212a48cd779ea990bd7f9439 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 25 Feb 2025 14:17:23 +0100 Subject: [PATCH 1/5] filterx/expr-switch: fix leaking case Signed-off-by: Attila Szakacs --- lib/filterx/expr-switch.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/filterx/expr-switch.c b/lib/filterx/expr-switch.c index 3e3f6848b..e1cf9940b 100644 --- a/lib/filterx/expr-switch.c +++ b/lib/filterx/expr-switch.c @@ -187,8 +187,13 @@ _find_matching_case(FilterXSwitch *self, FilterXObject *selector) FilterXSwitchCase *switch_case = (FilterXSwitchCase *) g_ptr_array_index(self->cases, i); FilterXObject *value = _eval_switch_case(switch_case); + if (filterx_compare_objects(selector, value, FCMPX_TYPE_AND_VALUE_BASED | FCMPX_EQ)) - return switch_case; + { + filterx_object_unref(value); + return switch_case; + } + filterx_object_unref(value); } return NULL; From 9bfc2eefd8cc2b47373dd359765aad04de41a329 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 25 Feb 2025 14:17:59 +0100 Subject: [PATCH 2/5] filterx/expr-switch: check case null-ness Signed-off-by: Attila Szakacs --- lib/filterx/expr-switch.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/filterx/expr-switch.c b/lib/filterx/expr-switch.c index e1cf9940b..396aa4125 100644 --- a/lib/filterx/expr-switch.c +++ b/lib/filterx/expr-switch.c @@ -187,6 +187,8 @@ _find_matching_case(FilterXSwitch *self, FilterXObject *selector) FilterXSwitchCase *switch_case = (FilterXSwitchCase *) g_ptr_array_index(self->cases, i); FilterXObject *value = _eval_switch_case(switch_case); + if (!value) + continue; if (filterx_compare_objects(selector, value, FCMPX_TYPE_AND_VALUE_BASED | FCMPX_EQ)) { From 3ce79e3b4d852eec9162f676a1826d5fe28411b3 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 25 Feb 2025 14:18:17 +0100 Subject: [PATCH 3/5] filterx/expr-switch: check selector null-ness Signed-off-by: Attila Szakacs --- lib/filterx/expr-switch.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/filterx/expr-switch.c b/lib/filterx/expr-switch.c index 396aa4125..9538854b9 100644 --- a/lib/filterx/expr-switch.c +++ b/lib/filterx/expr-switch.c @@ -205,7 +205,10 @@ static FilterXObject * _eval_switch(FilterXExpr *s) { FilterXSwitch *self = (FilterXSwitch *) s; + FilterXObject *selector = filterx_expr_eval_typed(self->selector); + if (!selector) + return NULL; FilterXSwitchCase *switch_case; From 7c9aa0dcf28fc05847c7326272e62f4d9d5af45a Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 25 Feb 2025 14:28:54 +0100 Subject: [PATCH 4/5] filterx: test invalid switch case and selector Signed-off-by: Attila Szakacs --- .../filterx/test_filterx_control.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/light/functional_tests/filterx/test_filterx_control.py b/tests/light/functional_tests/filterx/test_filterx_control.py index fb5df747f..39d94f345 100644 --- a/tests/light/functional_tests/filterx/test_filterx_control.py +++ b/tests/light/functional_tests/filterx/test_filterx_control.py @@ -509,3 +509,46 @@ def test_switch_duplicate_default_case(config, syslog_ng): ) with pytest.raises(Exception): syslog_ng.start(config) + + +def test_switch_invalid_selector(config, syslog_ng): + (file_true, file_false) = create_config( + config, + msg="orig_msg", + filterx_expr_1=r""" + switch (invalid.expr) { + case $MSG: + result = "no-no"; + break; + }; + $MSG=result; + """, + ) + syslog_ng.start(config) + + assert file_false.get_stats()["processed"] == 1 + assert "processed" not in file_true.get_stats() + assert file_false.read_log() == "orig_msg\n" + + +def test_switch_invalid_case(config, syslog_ng): + (file_true, file_false) = create_config( + config, + msg="string", + filterx_expr_1=r""" + switch (${values.str}) { + case invalid.expr: + result = "no-no"; + break; + default: + result = "default"; + break; + }; + $MSG=result; + """, + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == "default\n" From 0be2bd28afdd37cadd42c2aa9114d156785bcdbd Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Tue, 25 Feb 2025 14:19:48 +0100 Subject: [PATCH 5/5] news: add entry for #527 Signed-off-by: Attila Szakacs --- news/fx-bugfix-527.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/fx-bugfix-527.md diff --git a/news/fx-bugfix-527.md b/news/fx-bugfix-527.md new file mode 100644 index 000000000..54af701db --- /dev/null +++ b/news/fx-bugfix-527.md @@ -0,0 +1 @@ +`switch`: Fixed a crash that occurred when the selector or case failed to evaluate.