From 2c118cd2b3eaff5644566a4b50e0c342929ad2d8 Mon Sep 17 00:00:00 2001 From: Daniel Danzberger Date: Tue, 24 Dec 2019 18:56:55 +0100 Subject: [PATCH] Fix several segfaults when UCI values are not available Signed-off-by: Daniel Danzberger --- dmtree/tr181/deviceinfo.c | 28 ++++++++++++++++++++-------- libbbf_api/dmuci.c | 29 +++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/dmtree/tr181/deviceinfo.c b/dmtree/tr181/deviceinfo.c index 36082a61..6aa47503 100644 --- a/dmtree/tr181/deviceinfo.c +++ b/dmtree/tr181/deviceinfo.c @@ -578,13 +578,16 @@ int get_process_cpu_usage(char* refparam, struct dmctx *ctx, void *data, char *i int get_process_number_of_entries(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - json_object *res, *processes; + json_object *res = NULL, *processes = NULL; int nbre_process = 0; dmubus_call("router.system", "processes", UBUS_ARGS{{}}, 0, &res); - DM_ASSERT(res, *value = "0"); - json_object_object_get_ex(res, "processes", &processes); - nbre_process = json_object_array_length(processes); + if (res) { + json_object_object_get_ex(res, "processes", &processes); + if (processes) + nbre_process= json_object_array_length(processes); + } + dmasprintf(value, "%d", nbre_process); return 0; } @@ -641,16 +644,25 @@ int get_process_state(char* refparam, struct dmctx *ctx, void *data, char *insta int browsePocessEntriesInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) { - json_object *res, *processes, *fields, *process; + json_object *res = NULL, *processes = NULL; + json_object *fields = NULL, *process = NULL; char *pid_field, *command_field, *state_field, *mem_size_field, *cpu_time_field, *priority_field, *pid, *command, *mem_size, *state, *cpu_time, *priority, *idx, *idx_last= NULL; int i, id = 0; struct process_args proc_args = {0}; + size_t nbre_process = 0; dmubus_call("router.system", "processes", UBUS_ARGS{{}}, 0, &res); - if (!res) return 0; + + if (res) { + json_object_object_get_ex(res, "processes", &processes); + if (processes) + nbre_process = json_object_array_length(processes); + } + json_object_object_get_ex(res, "fields", &fields); - json_object_object_get_ex(res, "processes", &processes); - size_t nbre_process = json_object_array_length(processes); + if (fields == NULL) + return 0; + pid_field = (char *)dmjson_get_value_in_array_idx(fields, 0, 0, NULL); command_field = (char *)dmjson_get_value_in_array_idx(fields, 7, 0, NULL); state_field = (char *)dmjson_get_value_in_array_idx(fields, 3, 0, NULL); diff --git a/libbbf_api/dmuci.c b/libbbf_api/dmuci.c index 0951dc63..3d27d20c 100644 --- a/libbbf_api/dmuci.c +++ b/libbbf_api/dmuci.c @@ -540,6 +540,11 @@ int dmuci_rename_section(char *package, char *section, char *value) /**** UCI LOOKUP by section pointer ****/ int dmuci_lookup_ptr_by_section(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_section *s, char *option, char *value) { + struct uci_element *e; + + if (s == NULL || s->package == NULL) + return -1; + /*value*/ ptr->value = value; @@ -572,6 +577,9 @@ int dmuci_get_value_by_section_string(struct uci_section *s, char *option, char struct uci_element *e; struct uci_option *o; + if (s == NULL || option == NULL) + goto not_found; + uci_foreach_element(&s->options, e) { o = (uci_to_option(e)); if (!strcmp(o->e.name, option)) { @@ -583,6 +591,8 @@ int dmuci_get_value_by_section_string(struct uci_section *s, char *option, char return 0; } } + +not_found: *value = ""; return -1; } @@ -630,7 +640,9 @@ char *dmuci_set_value_by_section(struct uci_section *s, char *option, char *valu { struct uci_ptr up = {0}; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1) + return ""; + if (uci_set(uci_ctx, &up) != UCI_OK) return ""; @@ -646,7 +658,8 @@ int dmuci_delete_by_section(struct uci_section *s, char *option, char *value) struct uci_ptr up = {0}; uci_ctx->flags |= UCI_FLAG_EXPORT_NAME; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1) + return -1; if (uci_delete(uci_ctx, &up) != UCI_OK) return -1; @@ -658,7 +671,8 @@ int dmuci_delete_by_section_unnamed(struct uci_section *s, char *option, char *v { struct uci_ptr up = {0}; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1) + return -1; if (uci_delete(uci_ctx, &up) != UCI_OK) return -1; @@ -671,7 +685,8 @@ int dmuci_add_list_value_by_section(struct uci_section *s, char *option, char *v { struct uci_ptr up = {0}; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1) + return -1; if (uci_add_list(uci_ctx, &up) != UCI_OK) return -1; @@ -684,7 +699,8 @@ int dmuci_del_list_value_by_section(struct uci_section *s, char *option, char *v { struct uci_ptr up = {0}; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1) + return -1; if (uci_del_list(uci_ctx, &up) != UCI_OK) return -1; @@ -697,7 +713,8 @@ int dmuci_rename_section_by_section(struct uci_section *s, char *value) { struct uci_ptr up = {0}; - dmuci_lookup_ptr_by_section(uci_ctx, &up, s, NULL, value); + if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, NULL, value) == -1) + return -1; if (uci_rename(uci_ctx, &up) != UCI_OK) return -1;