From 0ef1ad0dd74b6fb10c0cff5d1c1f84777e6404c4 Mon Sep 17 00:00:00 2001 From: vdutta Date: Mon, 6 Dec 2021 11:06:52 +0530 Subject: [PATCH] Fix segfault in json plugin --- .gitignore | 1 + dmdynamicjson.c | 6 +++++- test/bbf_test/bbf_dm.c | 4 +++- test/files/etc/bbfdm/json/index.json | 22 ++++++++++++++++++++++ test/files/tmp/proxd.data | 3 ++- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index cc79c769..87056b68 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ tools/__pycache__ *.gcno *\.log .libs +m4/ diff --git a/dmdynamicjson.c b/dmdynamicjson.c index 33a7c6a6..020aa235 100644 --- a/dmdynamicjson.c +++ b/dmdynamicjson.c @@ -702,7 +702,11 @@ static char *ubus_get_value(json_object *mapping_obj, int json_version, char *re } json_object_object_get_ex(res, buf, &arr_obj); - nbre_entries = (arr_obj) ? json_object_array_length(arr_obj) : 0; + + if (arr_obj != NULL && json_object_is_type(arr_obj, json_type_array) == 1) { + nbre_entries = json_object_array_length(arr_obj); + } + dmasprintf(&value, "%d", nbre_entries); goto end; } diff --git a/test/bbf_test/bbf_dm.c b/test/bbf_test/bbf_dm.c index 7601dff7..81f428fd 100644 --- a/test/bbf_test/bbf_dm.c +++ b/test/bbf_test/bbf_dm.c @@ -99,8 +99,10 @@ int main(int argc, char *argv[]) } ubus_ctx = ubus_connect(NULL); - if (ubus_ctx == NULL) + if (ubus_ctx == NULL) { + fprintf(stderr, "Failed to connect with ubus\n"); return -1; + } dm_config_ubus(ubus_ctx); diff --git a/test/files/etc/bbfdm/json/index.json b/test/files/etc/bbfdm/json/index.json index 25a82a67..476e68e8 100644 --- a/test/files/etc/bbfdm/json/index.json +++ b/test/files/etc/bbfdm/json/index.json @@ -39,6 +39,28 @@ } ] }, + "MHubNumberOfEntries": { + "type": "int", + "version": "2.14", + "protocols": [ + "cwmp", + "usp" + ], + "read": true, + "write": false, + "datatype": "int", + "mapping": [ + { + "type": "ubus", + "ubus": { + "object": "proxd", + "method": "list", + "args": {}, + "key": "numofdevices" + } + } + ] + }, "Device.PD2.{i}.MiniHub.{i}.": { "type": "object", "version": "2.14", diff --git a/test/files/tmp/proxd.data b/test/files/tmp/proxd.data index ab830282..80c462f2 100644 --- a/test/files/tmp/proxd.data +++ b/test/files/tmp/proxd.data @@ -2,5 +2,6 @@ "devices": [ { "name": "one"}, {"name": "two"} - ] + ], + "numofdevices": "1" }