From 0789d5a0b76c54769e50729542254570fb73343e Mon Sep 17 00:00:00 2001 From: shifter Date: Wed, 4 Sep 2024 14:47:19 +0200 Subject: [PATCH 1/2] filterx: json array get-subscript invalid NULL result fix Fixed multiple issues in the original solution. 1. The conditional check for the index argument being in range was incorrect. The correct condition should have been `json_object_array_length(self->jso) < index + 1` to function as intended. 2. The range check was redundant. Since `object-json-array` inherits from `filterx-list-interface`, the range check is already properly implemented there. Signed-off-by: shifter --- lib/filterx/object-json-array.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/filterx/object-json-array.c b/lib/filterx/object-json-array.c index f92ea3902f..3f67369da9 100644 --- a/lib/filterx/object-json-array.c +++ b/lib/filterx/object-json-array.c @@ -119,12 +119,6 @@ _get_subscript(FilterXList *s, guint64 index) FilterXJsonArray *self = (FilterXJsonArray *) s; struct json_object *jso = json_object_array_get_idx(self->jso, index); - if (!jso) - { - /* NULL is returned if the stored value is null. */ - if (json_object_array_length(self->jso) > index + 1) - return NULL; - } return filterx_json_convert_json_to_object_cached(&s->super, &self->root_container, jso); } From 8881661c9492bd318436bfaf64dd74d00bf0d3db Mon Sep 17 00:00:00 2001 From: shifter Date: Wed, 4 Sep 2024 14:47:51 +0200 Subject: [PATCH 2/2] filterx: list range check e2e tests Signed-off-by: shifter --- .../functional_tests/filterx/test_filterx.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/light/functional_tests/filterx/test_filterx.py b/tests/light/functional_tests/filterx/test_filterx.py index c022b77831..4aff1ed4d0 100644 --- a/tests/light/functional_tests/filterx/test_filterx.py +++ b/tests/light/functional_tests/filterx/test_filterx.py @@ -2008,3 +2008,34 @@ def test_get_sdata(config, syslog_ng): }, }, } + + +def test_list_range_check_with_nulls(config, syslog_ng): + (file_true, file_false) = create_config( + config, r""" + $MSG = json(); + js1 = json_array([null]); + js2 = json_array([null, "bar"]); + $MSG.caseA = js1[0]; + $MSG.caseB = js2[0]; + $MSG.caseC = js2[1]; + """, + ) + syslog_ng.start(config) + + assert file_true.get_stats()["processed"] == 1 + assert "processed" not in file_false.get_stats() + assert file_true.read_log() == '{"caseA":null,"caseB":null,"caseC":"bar"}\n' + + +def test_list_range_check_out_of_range(config, syslog_ng): + (file_true, file_false) = create_config( + config, r""" + $MSG = json(); + js1 = json_array([null, "bar"]); + $MSG.caseA = js1[2]; + """, + ) + syslog_ng.start(config) + assert file_false.get_stats()["processed"] == 1 + assert file_false.read_log() == "foobar\n"