Fix several segfaults when UCI values are not available

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
This commit is contained in:
Daniel Danzberger 2019-12-24 18:56:55 +01:00
parent e113575168
commit 2c118cd2b3
2 changed files with 43 additions and 14 deletions

View file

@ -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);

View file

@ -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;