From af2b0c020339ef224454e49131a94836e7c2365c Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Fri, 20 Sep 2024 06:18:52 +0000 Subject: [PATCH] Align with new code-analysis docker image --- .gitlab-ci.yml | 7 +- bbfdmd/ubus/bbfdmd.c | 2 +- libbbfdm-api/dmapi.c | 48 +++++----- libbbfdm-api/dmapi.h | 33 ++++--- libbbfdm-api/dmbbf.c | 46 +++++----- libbbfdm-api/dmbbf.h | 2 +- libbbfdm-api/dmcommon.c | 138 +++++++++++++++++----------- libbbfdm-api/dmcommon.h | 11 +-- libbbfdm-api/dmentry.c | 6 +- libbbfdm-api/dmjson.c | 13 +-- libbbfdm-api/dmmem.c | 13 ++- libbbfdm-api/dmmem.h | 2 +- libbbfdm-api/dmplugin.c | 5 +- libbbfdm-api/dmubus.c | 40 ++++---- libbbfdm-api/dmuci.c | 43 ++++----- libbbfdm-api/include/libbbfdm_api.h | 48 +++++----- libbbfdm-api/plugin/dotso_plugin.c | 26 ++++-- libbbfdm-api/plugin/json_plugin.c | 99 +++++++++++--------- libbbfdm-ubus/bbfdm-ubus.c | 8 +- libbbfdm-ubus/common.h | 2 +- libbbfdm-ubus/events.c | 12 +-- libbbfdm-ubus/get.c | 4 +- libbbfdm-ubus/get_helper.c | 52 +++-------- libbbfdm-ubus/get_helper.h | 6 +- libbbfdm-ubus/plugin.c | 6 +- libbbfdm-ubus/pretty_print.c | 11 +-- libbbfdm/device.c | 2 +- libbbfdm/deviceinfo.c | 107 ++++++++++++++------- libbbfdm/schedules.c | 6 +- libbbfdm/security.c | 12 ++- 30 files changed, 440 insertions(+), 370 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6704d3c0..a7caad91 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,13 +1,14 @@ variables: DEBUG: 'TRUE' SOURCE_FOLDER: "." - FLAWFINDER_OPTIONS: "-m 4 --error-level=5" - CPPCHECK_OPTIONS: "--enable=all --suppress=variableScope --suppress=unusedFunction --suppress=constParameter --suppress=knownConditionTrueFalse -DBBF_VENDOR_PREFIX=X_IOPSYS_EU_ -i test/" + COMPILATION_FIXUP: "cmake -DBBF_VENDOR_PREFIX=\\\"X_IOPSYS_EU_\\\" -DBBF_MAX_OBJECT_INSTANCES=255 -DBBFDMD_MAX_MSG_LEN=1048576 -DCMAKE_INSTALL_PREFIX=/" + CPPCHECK_OPTIONS: "--suppress=cert-MSC24-C -DBBF_VENDOR_PREFIX=X_IOPSYS_EU_" + CPD_OPTIONS: "--exclude ./build/ --minimum-tokens 200" include: - project: 'iopsys/gitlab-ci-pipeline' file: '/static-code-analysis.yml' - ref: '0.31' + ref: '1.2' - project: 'docs/portal2/pipeline-template' file: 'MkDocs.gitlab-ci.yml' rules: diff --git a/bbfdmd/ubus/bbfdmd.c b/bbfdmd/ubus/bbfdmd.c index 84434e9e..3cfe69f6 100644 --- a/bbfdmd/ubus/bbfdmd.c +++ b/bbfdmd/ubus/bbfdmd.c @@ -15,7 +15,7 @@ #include "bbfdm-ubus.h" #include "cli.h" -#include "libbbfdm/device.h" +#include "../../libbbfdm/device.h" static void usage(char *prog) { diff --git a/libbbfdm-api/dmapi.c b/libbbfdm-api/dmapi.c index b079bbe0..e106814e 100644 --- a/libbbfdm-api/dmapi.c +++ b/libbbfdm-api/dmapi.c @@ -26,72 +26,72 @@ * */ -int bbf_uci_add_section(char *package, char *type, struct uci_section **s) +int bbf_uci_add_section(const char *package, const char *type, struct uci_section **s) { return dmuci_add_section(package, type, s); } -int bbf_uci_delete_section(char *package, char *type, char *option, char *value) +int bbf_uci_delete_section(const char *package, const char *type, const char *option, const char *value) { return dmuci_delete(package, type, option, value); } -int bbf_uci_add_section_bbfdm(char *package, char *type, struct uci_section **s) +int bbf_uci_add_section_bbfdm(const char *package, const char *type, struct uci_section **s) { return dmuci_add_section_bbfdm(package, type, s); } -int bbf_uci_delete_section_bbfdm(char *package, char *type, char *option, char *value) +int bbf_uci_delete_section_bbfdm(const char *package, const char *type, const char *option, const char *value) { return dmuci_delete_bbfdm(package, type, option, value); } -int bbf_uci_rename_section(struct uci_section *s, char *value) +int bbf_uci_rename_section(struct uci_section *s, const char *value) { return dmuci_rename_section_by_section(s, value); } -int bbf_uci_get_value(char *package, char *section, char *option, char **value) +int bbf_uci_get_value(const char *package, const char *section, const char *option, char **value) { return dmuci_get_option_value_string(package, section, option, value); } -int bbf_uci_set_value(char *package, char *section, char *option, char *value) +int bbf_uci_set_value(const char *package, const char *section, const char *option, const char *value) { return dmuci_set_value(package, section, option, value); } -int bbf_uci_get_value_by_section(struct uci_section *s, char *option, char **value) +int bbf_uci_get_value_by_section(struct uci_section *s, const char *option, char **value) { return dmuci_get_value_by_section_string(s, option, value); } -char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, char *option, char *default_value) +char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, const char *option, const char *default_value) { return dmuci_get_value_by_section_fallback_def(s, option, default_value); } -int bbf_uci_set_value_by_section(struct uci_section *s, char *option, char *value) +int bbf_uci_set_value_by_section(struct uci_section *s, const char *option, const char *value) { return dmuci_set_value_by_section(s, option, value); } -int bbf_uci_delete_section_by_section(struct uci_section *s, char *option, char *value) +int bbf_uci_delete_section_by_section(struct uci_section *s, const char *option, const char *value) { return dmuci_delete_by_section(s, option, value); } -int bbf_uci_get_section_name(char *sec_name, char **value) +int bbf_uci_get_section_name(const char *sec_name, char **value) { return dmuci_get_section_name(sec_name, value); } -int bbf_uci_set_section_name(char *sec_name, char *str, size_t size) +int bbf_uci_set_section_name(const char *sec_name, char *str, size_t size) { return dmuci_set_section_name(sec_name, str, size); } -struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, void *arg2, int cmp, int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk) +struct uci_section *bbf_uci_walk_section(const char *package, const char *type, const void *arg1, const void *arg2, int cmp, int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk) { return dmuci_walk_section(package, type, arg1, arg2, cmp, filter, prev_section, walk); @@ -104,12 +104,12 @@ struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, * */ -int bbf_ubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res) +int bbf_ubus_call(const char *obj, const char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res) { return dmubus_call(obj, method, u_args, u_args_size, req_res); } -int bbf_ubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size) +int bbf_ubus_call_set(const char *obj, const char *method, struct ubus_arg u_args[], int u_args_size) { return dmubus_call_set(obj, method, u_args, u_args_size); } @@ -148,7 +148,7 @@ char *bbf_strdup(const char *ptr) * */ -void bbf_synchronise_config_sections_with_dmmap(char *package, char *section_type, char *dmmap_package, struct list_head *dup_list) +void bbf_synchronise_config_sections_with_dmmap(const char *package, const char *section_type, const char *dmmap_package, struct list_head *dup_list) { return synchronize_specific_config_sections_with_dmmap(package, section_type, dmmap_package, dup_list); } @@ -173,22 +173,22 @@ int bbf_get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int return get_number_of_entries(ctx, data, instance, browseinstobj); } -int bbf_convert_string_to_bool(char *str, bool *b) +int bbf_convert_string_to_bool(const char *str, bool *b) { return string_to_bool(str, b); } -void bbf_find_dmmap_section(char *dmmap_package, char *section_type, char *section_name, struct uci_section **dmmap_section) +void bbf_find_dmmap_section(const char *dmmap_package, const char *section_type, const char *section_name, struct uci_section **dmmap_section) { return get_dmmap_section_of_config_section(dmmap_package, section_type, section_name, dmmap_section); } -void bbf_find_dmmap_section_by_option(char *dmmap_package, char *section_type, char *option_name, char *option_value, struct uci_section **dmmap_section) +void bbf_find_dmmap_section_by_option(const char *dmmap_package, const char *section_type, const char *option_name, const char *option_value, struct uci_section **dmmap_section) { return get_dmmap_section_of_config_section_eq(dmmap_package, section_type, option_name, option_value, dmmap_section); } -int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, char *instance, char **value) +int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, const char *option_name, const char *instance, char **value) { if (!ctx || !s || !option_name || !instance || !value) return -1; @@ -204,7 +204,7 @@ int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, c return 0; } -int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, char *instance, char *value) +int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, const char *option_name, const char *instance, const char *value) { if (!ctx || !s || !option_name || !instance || !value) return -1; @@ -222,7 +222,7 @@ int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, c return 0; } -int bbfdm_get_references(struct dmctx *ctx, int match_action, const char *base_path, char *key_name, char *key_value, char *out, size_t out_len) +int bbfdm_get_references(struct dmctx *ctx, int match_action, const char *base_path, const char *key_name, char *key_value, char *out, size_t out_len) { char param_path[1024] = {0}; char *value = NULL; @@ -278,7 +278,7 @@ end: return 0; } -int _bbfdm_get_references(struct dmctx *ctx, const char *base_path, char *key_name, char *key_value, char **value) +int _bbfdm_get_references(struct dmctx *ctx, const char *base_path, const char *key_name, char *key_value, char **value) { char buf[1024] = {0}; diff --git a/libbbfdm-api/dmapi.h b/libbbfdm-api/dmapi.h index 4c61fd3d..4980f51b 100644 --- a/libbbfdm-api/dmapi.h +++ b/libbbfdm-api/dmapi.h @@ -47,8 +47,10 @@ extern char *DMT_TYPE[]; #define DM_STRNCPY(DST, SRC, SIZE) \ do { \ - strncpy(DST, SRC, SIZE - 1); \ - DST[SIZE-1] = '\0'; \ + if ((DST) != NULL && (SRC) != NULL) { \ + strncpy(DST, SRC, SIZE - 1); \ + DST[SIZE-1] = '\0'; \ + } \ } while(0) #define DM_ULTOSTR(DST, SRC, SIZE) \ @@ -165,36 +167,41 @@ struct dm_reference { struct dmctx { bool stop; bool match; + bool nextlevel; + bool iswildcard; + bool isgetschema; + bool iscommand; + bool isevent; + bool isinfo; + bool disable_mservice_browse; + int (*method_param)(DMPARAM_ARGS); int (*method_obj)(DMOBJECT_ARGS); int (*checkobj)(DMOBJECT_ARGS); int (*checkleaf)(DMOBJECT_ARGS); + struct list_head *memhead; struct blob_buf bb; + DMOBJ *dm_entryobj; struct uci_context *config_uci_ctx; struct uci_context *dmmap_uci_ctx; struct uci_context *varstate_uci_ctx; - bool nextlevel; - bool iswildcard; + int faultcode; - char fault_msg[256]; int setaction; + unsigned int dm_type; + unsigned char inparam_isparam; + unsigned char findparam; + char *in_param; char *in_value; char *in_type; char *addobj_instance; char *linker; char *linker_param; - unsigned int dm_type; - unsigned char inparam_isparam; - unsigned char findparam; char *inst_buf[16]; - bool isgetschema; - bool iscommand; - bool isevent; - bool isinfo; - bool disable_mservice_browse; + char fault_msg[256]; }; typedef struct dmnode { diff --git a/libbbfdm-api/dmbbf.c b/libbbfdm-api/dmbbf.c index 8cb62504..4342aef7 100644 --- a/libbbfdm-api/dmbbf.c +++ b/libbbfdm-api/dmbbf.c @@ -601,7 +601,7 @@ int dm_link_inst_obj(struct dmctx *dmctx, DMNODE *parent_node, void *data, char return err; } -static int rootcmp(char *inparam, char *rootobj) +static int rootcmp(const char *inparam, const char *rootobj) { char buf[32]; snprintf(buf, sizeof(buf), "%s.", rootobj); @@ -631,10 +631,10 @@ static int find_max_instance(struct dmctx *ctx, DMNODE *node) return ++(node->max_instance); } -char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt) +char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, const char *inst_opt, const char *alias_opt) { char buf[64] = {0}; - char *instance = ""; + char *instance = NULL; dmuci_get_value_by_section_string(s, inst_opt, &instance); @@ -652,13 +652,14 @@ char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_secti break; } - dmctx->inst_buf[parent_node->instance_level] = instance; - return instance; + dmctx->inst_buf[parent_node->instance_level] = instance ? instance : ""; + + return instance ? instance : ""; } char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, int inst_nbr) { - char *instance = ""; + char *instance = NULL; switch(parent_node->browse_type) { case BROWSE_NORMAL: @@ -669,13 +670,14 @@ char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, break; } - dmctx->inst_buf[parent_node->instance_level] = instance; - return instance; + dmctx->inst_buf[parent_node->instance_level] = instance ? instance : ""; + + return instance ? instance : ""; } int get_empty(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - *value = ""; + *value = dmstrdup(""); return 0; } @@ -1027,7 +1029,7 @@ static void prepare_optional_table(struct dmctx *dmctx, struct blob_buf *bb) typedef void (*ms_ubus_cb)(struct ubus_request *req, int type, struct blob_attr *msg); -static int ubus_call_blob_msg(char *obj, char *method, struct blob_buf *blob, int timeout, ms_ubus_cb ms_callback, void *callback_arg) +static int ubus_call_blob_msg(const char *obj, const char *method, struct blob_buf *blob, int timeout, ms_ubus_cb ms_callback, void *callback_arg) { struct ubus_context *ubus_ctx = NULL; uint32_t id; @@ -1406,7 +1408,7 @@ static int set_ubus_value(struct dmctx *dmctx, struct dmnode *node) json_object *res = NULL, *res_obj = NULL; char *ubus_name = node->obj->checkdep; char param_value[2048] = {0}; - char *ref_value = ""; + char *ref_value = dmstrdup(""); json_object *in_args = json_object_new_object(); json_object_object_add(in_args, "proto", json_object_new_string((dmctx->dm_type == BBFDM_BOTH) ? "both" : (dmctx->dm_type == BBFDM_CWMP) ? "cwmp" : "usp")); @@ -1713,14 +1715,14 @@ static int get_value_param(DMPARAM_ARGS) return get_ubus_value(dmctx, node); } else { char full_param[MAX_DM_PATH] = {0}; - char *value = ""; + char *value = dmstrdup(""); snprintf(full_param, sizeof(full_param), "%s%s", node->current_object, leaf->parameter); (leaf->getvalue)(full_param, dmctx, data, instance, &value); if ((leaf->dm_flags & DM_FLAG_SECURE) && (dmctx->dm_type == BBFDM_CWMP)) { - value = ""; + value = dmstrdup(""); } else if (value && *value) { if (leaf->dm_flags & DM_FLAG_REFERENCE) { value = get_value_by_reference(dmctx, value); @@ -1752,7 +1754,7 @@ static int mparam_get_value_in_param(DMPARAM_ARGS) dmctx->stop = (dmctx->iswildcard) ? false : true; } else { char full_param[MAX_DM_PATH] = {0}; - char *value = ""; + char *value = dmstrdup(""); snprintf(full_param, sizeof(full_param), "%s%s", node->current_object, leaf->parameter); @@ -1767,7 +1769,7 @@ static int mparam_get_value_in_param(DMPARAM_ARGS) (leaf->getvalue)(full_param, dmctx, data, instance, &value); if ((leaf->dm_flags & DM_FLAG_SECURE) && (dmctx->dm_type == BBFDM_CWMP)) { - value = ""; + value = dmstrdup(""); } else if (value && *value) { if (leaf->dm_flags & DM_FLAG_REFERENCE) { value = get_value_by_reference(dmctx, value); @@ -1835,7 +1837,7 @@ int dm_entry_get_value(struct dmctx *dmctx) /* ********** * get name * **********/ -static void fill_blob_alias_param(struct blob_buf *bb, char *path, char *data, char *type, char *alias) +static void fill_blob_alias_param(struct blob_buf *bb, const char *path, const char *data, const char *type, const char *alias) { if (!bb || !path || !data || !type || !alias) return; @@ -1888,7 +1890,7 @@ static int mparam_get_name(DMPARAM_ARGS) perm = leaf->permission->get_permission(refparam, dmctx, data, instance); if (DM_LSTRCMP(leaf->parameter, "Alias") == 0) { - char *alias = ""; + char *alias = dmstrdup(""); (leaf->getvalue)(refparam, dmctx, data, instance, &alias); fill_blob_alias_param(&dmctx->bb, refparam, perm, DMT_TYPE[leaf->type], alias); @@ -1936,7 +1938,7 @@ static int mparam_get_name_in_param(DMPARAM_ARGS) perm = leaf->permission->get_permission(refparam, dmctx, data, instance); if (DM_LSTRCMP(leaf->parameter, "Alias") == 0) { - char *alias = ""; + char *alias = dmstrdup(""); (leaf->getvalue)(refparam, dmctx, data, instance, &alias); fill_blob_alias_param(&dmctx->bb, refparam, perm, DMT_TYPE[leaf->type], alias); @@ -1990,7 +1992,7 @@ static int mparam_get_name_in_obj(DMPARAM_ARGS) perm = leaf->permission->get_permission(refparam, dmctx, data, instance); if (DM_LSTRCMP(leaf->parameter, "Alias") == 0) { - char *alias = ""; + char *alias = dmstrdup(""); (leaf->getvalue)(refparam, dmctx, data, instance, &alias); fill_blob_alias_param(&dmctx->bb, refparam, perm, DMT_TYPE[leaf->type], alias); @@ -2387,7 +2389,7 @@ static int mparam_set_value(DMPARAM_ARGS) } else { char refparam[MAX_DM_PATH] = {0}; char param_value[2048] = {0}; - char *value = ""; + char *value = dmstrdup(""); snprintf(refparam, MAX_DM_PATH, "%s%s", node->current_object, leaf->parameter); if (DM_STRCMP(refparam, dmctx->in_param) != 0) @@ -2543,7 +2545,7 @@ static int get_key_check_param(DMPARAM_ARGS) return err ? err : 0; } else { char full_param[MAX_DM_PATH] = {0}; - char *value = ""; + char *value = dmstrdup(""); snprintf(full_param, sizeof(full_param), "%s%s", node->current_object, leaf->parameter); @@ -2552,7 +2554,7 @@ static int get_key_check_param(DMPARAM_ARGS) (leaf->getvalue)(full_param, dmctx, data, instance, &value); - if (value && value[0] != '\0' && DM_STRCMP(value, dmctx->linker) == 0) { + if (DM_STRLEN(value) && DM_STRCMP(value, dmctx->linker) == 0) { if (node->current_object[DM_STRLEN(node->current_object) - 1] == '.') node->current_object[DM_STRLEN(node->current_object) - 1] = 0; dmctx->linker_param = dmstrdup(node->current_object); diff --git a/libbbfdm-api/dmbbf.h b/libbbfdm-api/dmbbf.h index bd123ec2..b5a53b7c 100644 --- a/libbbfdm-api/dmbbf.h +++ b/libbbfdm-api/dmbbf.h @@ -31,7 +31,7 @@ bool dm_is_micro_service(void); void dm_set_micro_service(void); int get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int (*browseinstobj)(struct dmctx *ctx, struct dmnode *node, void *data, char *instance)); -char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, char *inst_opt, char *alias_opt); +char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_section *s, const char *inst_opt, const char *alias_opt); char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, int inst_nbr); int get_empty(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); diff --git a/libbbfdm-api/dmcommon.c b/libbbfdm-api/dmcommon.c index d0469f9f..d79271f3 100644 --- a/libbbfdm-api/dmcommon.c +++ b/libbbfdm-api/dmcommon.c @@ -72,7 +72,7 @@ int compare_strings(const void *a, const void *b) char *get_uptime(void) { FILE *fp = fopen(UPTIME, "r"); - char *uptime = "0"; + char *uptime = NULL; if (fp != NULL) { char *pch = NULL, *spch = NULL, buf[64] = {0}; @@ -84,7 +84,7 @@ char *get_uptime(void) fclose(fp); } - return uptime; + return uptime ? uptime : "0"; } int check_file(const char *path) @@ -104,7 +104,9 @@ int check_file(const char *path) char *cidr2netmask(int bits) { - static char buf[INET_ADDRSTRLEN]; + char *buf = (char *)dmcalloc(INET_ADDRSTRLEN, sizeof(char)); + if (!buf) + return ""; uint32_t mask = (bits >= 32) ? 0xFFFFFFFFUL : (0xFFFFFFFFUL << (32 - bits)); mask = htonl(mask); @@ -190,7 +192,7 @@ int dmcmd(const char *cmd, int n, ...) int i, status; pid_t pid, wpid; - argv[0] = (char *)cmd; + argv[0] = dmstrdup(cmd); va_start(arg, n); for (i = 0; i < n; i++) { argv[i + 1] = va_arg(arg, char *); @@ -224,7 +226,7 @@ int dmcmd_no_wait(const char *cmd, int n, ...) int i; pid_t pid; - argv[0] = (char *)cmd; + argv[0] = dmstrdup(cmd); va_start(arg, n); for (i = 0; i < n; i++) { argv[i + 1] = va_arg(arg, char *); @@ -256,7 +258,7 @@ int run_cmd(const char *cmd, char *output, size_t out_len) memset(output, 0, out_len); - pp = popen(cmd, "r"); + pp = popen(cmd, "r"); // flawfinder: ignore if (pp != NULL) { if (!(fgets(output, out_len, pp) == NULL && ferror(pp) != 0)) { ret = 0; @@ -291,7 +293,10 @@ void add_dmmap_config_dup_list(struct list_head *dup_list, struct uci_section *c { struct dm_data *dm_data = NULL; - dm_data = dmcalloc(1, sizeof(struct dm_data)); + dm_data = (struct dm_data *)dmcalloc(1, sizeof(struct dm_data)); + if (!dm_data) + return; + list_add_tail(&dm_data->list, dup_list); dm_data->config_section = config_section; dm_data->dmmap_section = dmmap_section; @@ -604,11 +609,15 @@ unsigned int count_occurrences(const char *str, char c) bool isdigit_str(const char *str) { if (!DM_STRLEN(str)) - return 0; + return false; - while(isdigit(*str++)); + while (*str) { + if (!isdigit((unsigned char)*str)) + return false; + str++; + } - return (*(str-1)) ? 0 : 1; + return true; } bool ishex_str(const char *str) @@ -642,9 +651,13 @@ bool special_char_exits(const char *str) if (!DM_STRLEN(str)) return false; - while (!special_char(*str++)); + while (*str) { + if (special_char(*str)) + return true; + str++; + } - return (*(str-1)) ? true : false; + return false; } void replace_special_char(char *str, char c) @@ -686,32 +699,45 @@ char *dm_strword(char *src, char *str) return NULL; } -char **strsplit(const char *str, const char *delim, size_t *numtokens) +char **strsplit(const char *str, const char *delim, size_t *num_tokens) { - char *s = strdup(str); - size_t tokens_alloc = 1; - size_t tokens_used = 0; - char **tokens = dmcalloc(tokens_alloc, sizeof(char*)); - char *token, *strtok_ctx; + char *token = NULL, *save_ptr = NULL; + char buf[2048] = {0}; + size_t tokens_num = 0; - for (token = strtok_r(s, delim, &strtok_ctx); - token != NULL; - token = strtok_r(NULL, delim, &strtok_ctx)) { + if (!str || !delim || !num_tokens) { + if (num_tokens) + *num_tokens = 0; + return NULL; + } - if (tokens_used == tokens_alloc) { - tokens_alloc *= 2; - tokens = dmrealloc(tokens, tokens_alloc * sizeof(char*)); - } - tokens[tokens_used++] = dmstrdup(token); + DM_STRNCPY(buf, str, sizeof(buf)); + + for (token = strtok_r(buf, delim, &save_ptr); + token != NULL; + token = strtok_r(NULL, delim, &save_ptr)) + tokens_num++; + + if (tokens_num == 0) { + *num_tokens = 0; + return NULL; } - if (tokens_used == 0) { - dmfree(tokens); - tokens = NULL; - } else { - tokens = dmrealloc(tokens, tokens_used * sizeof(char*)); + + char **tokens = dmcalloc(tokens_num, sizeof(char *)); + if (!tokens) { + *num_tokens = 0; + return NULL; } - *numtokens = tokens_used; - FREE(s); + + DM_STRNCPY(buf, str, sizeof(buf)); + + token = strtok_r(buf, delim, &save_ptr); + for (size_t idx = 0; idx < tokens_num && token != NULL; idx++) { + tokens[idx] = dmstrdup(token); + token = strtok_r(NULL, delim, &save_ptr); + } + + *num_tokens = tokens_num; return tokens; } @@ -727,7 +753,7 @@ void convert_str_to_uppercase(char *str) char *get_macaddr(const char *interface_name) { char *device = get_device(interface_name); - char *mac; + char *mac = NULL; if (device[0]) { char file[128]; @@ -737,10 +763,9 @@ char *get_macaddr(const char *interface_name) dm_read_sysfs_file(file, val, sizeof(val)); convert_str_to_uppercase(val); mac = dmstrdup(val); - } else { - mac = ""; } - return mac; + + return mac ? mac : ""; } char *get_device(const char *interface_name) @@ -793,14 +818,14 @@ bool value_exits_in_str_list(const char *str_list, const char *delimitor, const char *add_str_to_str_list(const char *str_list, const char *delimitor, const char *str) { - char *res = ""; + char *res = NULL; if (!str_list || !delimitor || !str) return ""; dmasprintf(&res, "%s%s%s", str_list, strlen(str_list) ? delimitor : "", str); - return res; + return res ? res : ""; } char *remove_str_from_str_list(const char *str_list, const char *delimitor, const char *str) @@ -921,8 +946,9 @@ int get_net_device_sysfs(const char *device, const char *name, char **value) } *value = dmstrdup(val); } else { - *value = "0"; + *value = dmstrdup("0"); } + return 0; } @@ -932,29 +958,29 @@ int get_net_device_status(const char *device, char **value) get_net_device_sysfs(device, "operstate", &operstate); if (operstate == NULL || *operstate == '\0') { - *value = "Down"; + *value = dmstrdup("Down"); return 0; } if (strcmp(operstate, "up") == 0) - *value = "Up"; + *value = dmstrdup("Up"); else if (strcmp(operstate, "unknown") == 0) - *value = "Unknown"; + *value = dmstrdup("Unknown"); else if (strcmp(operstate, "notpresent") == 0) - *value = "NotPresent"; + *value = dmstrdup("NotPresent"); else if (strcmp(operstate, "lowerlayerdown") == 0) - *value = "LowerLayerDown"; + *value = dmstrdup("LowerLayerDown"); else if (strcmp(operstate, "dormant") == 0) - *value = "Dormant"; + *value = dmstrdup("Dormant"); else - *value = "Down"; + *value = dmstrdup("Down"); return 0; } int get_net_iface_sysfs(const char *uci_iface, const char *name, char **value) { - const char *device = get_device((char *)uci_iface); + const char *device = get_device(uci_iface); return get_net_device_sysfs(device, name, value); } @@ -964,7 +990,7 @@ int dm_time_utc_format(time_t ts, char **dst) char time_buf[32] = { 0, 0 }; struct tm *t_tm; - *dst = "0001-01-01T00:00:00Z"; + *dst = dmstrdup("0001-01-01T00:00:00Z"); t_tm = gmtime(&ts); if (t_tm == NULL) @@ -982,7 +1008,7 @@ int dm_time_format(time_t ts, char **dst) char time_buf[32] = { 0, 0 }; struct tm *t_tm; - *dst = "0001-01-01T00:00:00+00:00"; + *dst = dmstrdup("0001-01-01T00:00:00+00:00"); t_tm = localtime(&ts); if (t_tm == NULL) @@ -1057,7 +1083,7 @@ void bbfdm_set_fault_message(struct dmctx *ctx, const char *format, ...) return; va_start(args, format); - vsnprintf(ctx->fault_msg, sizeof(ctx->fault_msg), format, args); + vsnprintf(ctx->fault_msg, sizeof(ctx->fault_msg), format, args); // flawfinder: ignore va_end(args); } @@ -1698,11 +1724,11 @@ unsigned long file_system_size(const char *path, const enum fs_size_type_enum ty static int get_base64_char(char b64) { - char *base64C = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + const char *base64C = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; for (int i = 0; i < 64; i++) if (base64C[i] == b64) - return i; + return i; return -1; } @@ -1715,7 +1741,7 @@ char *base64_decode(const char *src) return ""; size_t decsize = DM_STRLEN(src)*6/8; - char *out = (char *)dmmalloc((decsize +1) * sizeof(char)); + char *out = (char *)dmcalloc(decsize + 1, sizeof(char)); for (i = 0; i < DM_STRLEN(src)-1; i++) { out[j] = (get_base64_char(src[i]) << (j%3==0?2:(j%3==1?4:6))) + (get_base64_char(src[i+1]) >> (j%3==0?4:(j%3==1? 2:0))); @@ -1812,7 +1838,7 @@ char *replace_str(const char *input_str, const char *old_substr, const char *new } // Count occurrences of old_substr in input_str - for (size_t i = 0; inext != mem_list) { - dmm = list_entry(mem_list->next, struct dmmem, list); + struct dmmem *dmm = NULL, *tmp = NULL; + + list_for_each_entry_safe(dmm, tmp, mem_list, list) { list_del(&dmm->list); FREE(dmm); } } -void dmfree(void *m) +void dmfree(const void *m) { if (m == NULL) return; struct dmmem *rm; @@ -185,15 +185,14 @@ void bbfdm_init_mem(struct dmctx *ctx) void bbfdm_clean_mem(struct dmctx *ctx) { - struct dmmem *dmm = NULL; + struct dmmem *dmm = NULL, *tmp = NULL; if (ctx->memhead == NULL) { BBF_ERR("Memory list is NULL!"); return; } - while (ctx->memhead->next != ctx->memhead) { - dmm = list_entry(ctx->memhead->next, struct dmmem, list); + list_for_each_entry_safe(dmm, tmp, ctx->memhead, list) { list_del(&dmm->list); FREE(dmm); } diff --git a/libbbfdm-api/dmmem.h b/libbbfdm-api/dmmem.h index 49ef6586..855a9080 100644 --- a/libbbfdm-api/dmmem.h +++ b/libbbfdm-api/dmmem.h @@ -20,7 +20,7 @@ #include "dmapi.h" -void dmfree(void *m); +void dmfree(const void *m); void __dminitmem(struct list_head *mem_list); void __dmcleanmem(struct list_head *mem_list); diff --git a/libbbfdm-api/dmplugin.c b/libbbfdm-api/dmplugin.c index 04aeb24c..cfe22db1 100644 --- a/libbbfdm-api/dmplugin.c +++ b/libbbfdm-api/dmplugin.c @@ -87,10 +87,9 @@ static void add_service_to_list(struct list_head *srvlist, const char *srv_name, void free_services_from_list(struct list_head *clist) { - struct service *srv = NULL; + struct service *srv = NULL, *tmp = NULL; - while (clist->next != clist) { - srv = list_entry(clist->next, struct service, list); + list_for_each_entry_safe(srv, tmp, clist, list) { list_del(&srv->list); free(srv->name); free(srv->parent_dm); diff --git a/libbbfdm-api/dmubus.c b/libbbfdm-api/dmubus.c index 9aac9759..b4e1cfcd 100644 --- a/libbbfdm-api/dmubus.c +++ b/libbbfdm-api/dmubus.c @@ -81,7 +81,7 @@ static void prepare_blob_message(struct blob_buf *b, const struct ubus_arg u_arg static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg) { - const char *str; + char *str = NULL; if (!msg) return; @@ -369,23 +369,31 @@ static const struct dm_ubus_cache_entry * dm_ubus_cache_lookup(unsigned hash) static void dm_ubus_cache_entry_new(unsigned hash, json_object *data) { - struct dm_ubus_cache_entry *entry = malloc(sizeof(*entry)); + struct dm_ubus_cache_entry *entry = NULL; - if (entry) { - entry->data = data; - entry->hash = hash; - list_add_tail(&entry->list, &dmubus_cache); - } + entry = calloc(1, sizeof(struct dm_ubus_cache_entry)); + if (!entry) + return; + + list_add_tail(&entry->list, &dmubus_cache); + entry->data = data; + entry->hash = hash; } -static void dm_ubus_cache_entry_free(struct dm_ubus_cache_entry *entry) +static void dm_ubus_cache_entry_free(void) { - list_del(&entry->list); + struct dm_ubus_cache_entry *entry = NULL, *tmp = NULL; - if (entry->data) - json_object_put(entry->data); + list_for_each_entry_safe(entry, tmp, &dmubus_cache, list) { + list_del(&entry->list); - FREE(entry); + if (entry->data) { + json_object_put(entry->data); + entry->data = NULL; + } + + FREE(entry); + } } int dmubus_call(const char *obj, const char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res) @@ -489,12 +497,6 @@ bool dmubus_object_method_exists(const char *object) void dmubus_free() { - struct dm_ubus_cache_entry *entry, *tmp; - - // cppcheck-suppress unknownMacro - list_for_each_entry_safe(entry, tmp, &dmubus_cache, list) - dm_ubus_cache_entry_free(entry); - + dm_ubus_cache_entry_free(); dm_libubus_free(); - } diff --git a/libbbfdm-api/dmuci.c b/libbbfdm-api/dmuci.c index 927136d5..56a1aae0 100644 --- a/libbbfdm-api/dmuci.c +++ b/libbbfdm-api/dmuci.c @@ -17,7 +17,6 @@ #include "dmmem.h" static struct uci_context *uci_ctx = NULL; -static char *db_config = NULL; NEW_UCI_PATH(bbfdm) NEW_UCI_PATH(varstate) @@ -52,8 +51,6 @@ void bbfdm_uci_init(struct dmctx *bbf_ctx) uci_ctx = bbf_ctx->config_uci_ctx; uci_ctx_bbfdm = bbf_ctx->dmmap_uci_ctx; uci_ctx_varstate = bbf_ctx->varstate_uci_ctx; - - db_config = ETC_DB_CONFIG; } void bbfdm_uci_exit(struct dmctx *bbf_ctx) @@ -154,13 +151,13 @@ int dmuci_get_section_type(const char *package, const char *section, char **valu struct uci_ptr ptr = {0}; if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, NULL, NULL)) { - *value = ""; + *value = dmstrdup(""); return -1; } if (ptr.s) { *value = dmstrdup(ptr.s->type); // MEM WILL BE FREED IN DMMEMCLEAN } else { - *value = ""; + *value = dmstrdup(""); return -1; } return 0; @@ -171,7 +168,7 @@ int dmuci_get_option_value_string(const char *package, const char *section, cons struct uci_ptr ptr = {0}; if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, option, NULL)) { - *value = ""; + *value = dmstrdup(""); return -1; } if (ptr.o && ptr.o->type == UCI_TYPE_LIST) { @@ -179,7 +176,7 @@ int dmuci_get_option_value_string(const char *package, const char *section, cons } else if (ptr.o && ptr.o->v.string) { *value = dmstrdup(ptr.o->v.string); // MEM WILL BE FREED IN DMMEMCLEAN } else { - *value = ""; + *value = dmstrdup(""); return -1; } return 0; @@ -187,10 +184,10 @@ int dmuci_get_option_value_string(const char *package, const char *section, cons char *dmuci_get_option_value_fallback_def(const char *package, const char *section, const char *option, const char *default_value) { - char *value = ""; + char *value = NULL; dmuci_get_option_value_string(package, section, option, &value); - if (*value == '\0') + if (DM_STRLEN(value) == 0) value = dmstrdup(default_value); return value; @@ -237,7 +234,7 @@ int dmuci_get_option_value_list(const char *package, const char *section, const return 0; } -static struct uci_option *dmuci_get_option_ptr(char *cfg_path, const char *package, const char *section, const char *option) +static struct uci_option *dmuci_get_option_ptr(const char *cfg_path, const char *package, const char *section, const char *option) { struct uci_option *o = NULL; struct uci_element *e = NULL; @@ -245,7 +242,7 @@ static struct uci_option *dmuci_get_option_ptr(char *cfg_path, const char *packa char *oconfdir; oconfdir = uci_ctx->confdir; - uci_ctx->confdir = cfg_path; + uci_ctx->confdir = dmstrdup(cfg_path); if (dmuci_lookup_ptr(uci_ctx, &ptr, package, section, option, NULL)) goto end; @@ -597,23 +594,23 @@ int dmuci_get_value_by_section_string(struct uci_section *s, const char *option, if (o->type == UCI_TYPE_LIST) { *value = dmuci_list_to_string(&o->v.list, " "); } else { - *value = o->v.string ? dmstrdup(o->v.string) : ""; // MEM WILL BE FREED IN DMMEMCLEAN + *value = o->v.string ? dmstrdup(o->v.string) : dmstrdup(""); // MEM WILL BE FREED IN DMMEMCLEAN } return 0; } } not_found: - *value = ""; + *value = dmstrdup(""); return -1; } char *dmuci_get_value_by_section_fallback_def(struct uci_section *s, const char *option, const char *default_value) { - char *value = ""; + char *value = NULL; dmuci_get_value_by_section_string(s, option, &value); - if (*value == '\0') + if (DM_STRLEN(value) == 0) value = dmstrdup(default_value); return value; @@ -787,12 +784,12 @@ struct uci_section *dmuci_walk_section (const char *package, const char *stype, goto end; case CMP_OPTION_EQUAL: dmuci_get_value_by_section_string(s, (const char *)arg1, &value); - if (DM_STRCMP(value, (char *)arg2) == 0) + if (DM_STRCMP(value, (const char *)arg2) == 0) goto end; break; case CMP_OPTION_CONTAINING: dmuci_get_value_by_section_string(s, (const char *)arg1, &value); - if (DM_STRSTR(value, (char *)arg2)) + if (DM_STRSTR(value, (const char *)arg2)) goto end; break; case CMP_OPTION_CONT_WORD: @@ -800,7 +797,7 @@ struct uci_section *dmuci_walk_section (const char *package, const char *stype, dup = dmstrdup(value); pch = strtok_r(dup, " ", &spch); while (pch != NULL) { - if (DM_STRCMP((char *)arg2, pch) == 0) { + if (DM_STRCMP((const char *)arg2, pch) == 0) { dmfree(dup); goto end; } @@ -812,7 +809,7 @@ struct uci_section *dmuci_walk_section (const char *package, const char *stype, dmuci_get_value_by_section_list(s, (const char *)arg1, &list_value); if (list_value != NULL) { uci_foreach_element(list_value, m) { - if (DM_STRCMP(m->name, (char *)arg2) == 0) + if (DM_STRCMP(m->name, (const char *)arg2) == 0) goto end; } } @@ -855,13 +852,13 @@ struct uci_section *dmuci_walk_all_sections(const char *package, struct uci_sect /**** UCI GET db config *****/ int db_get_value_string(const char *package, const char *section, const char *option, char **value) { - struct uci_option *o; + struct uci_option *o = NULL; - o = dmuci_get_option_ptr((db_config) ? db_config : ETC_DB_CONFIG, package, section, option); + o = dmuci_get_option_ptr(ETC_DB_CONFIG, package, section, option); if (o) { - *value = o->v.string ? dmstrdup(o->v.string) : ""; // MEM WILL BE FREED IN DMMEMCLEAN + *value = o->v.string ? dmstrdup(o->v.string) : dmstrdup(""); // MEM WILL BE FREED IN DMMEMCLEAN } else { - *value = ""; + *value = dmstrdup(""); return -1; } return 0; diff --git a/libbbfdm-api/include/libbbfdm_api.h b/libbbfdm-api/include/libbbfdm_api.h index 3a787a3e..a2105627 100644 --- a/libbbfdm-api/include/libbbfdm_api.h +++ b/libbbfdm-api/include/libbbfdm_api.h @@ -131,7 +131,7 @@ ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_add_section(char *package, char *type, struct uci_section **s); +int bbf_uci_add_section(const char *package, const char *type, struct uci_section **s); /*********************************************************************//** ** @@ -147,7 +147,7 @@ int bbf_uci_add_section(char *package, char *type, struct uci_section **s); ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_delete_section(char *package, char *type, char *option, char *value); +int bbf_uci_delete_section(const char *package, const char *type, const char *option, const char *value); /*********************************************************************//** ** @@ -162,7 +162,7 @@ int bbf_uci_delete_section(char *package, char *type, char *option, char *value) ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_add_section_bbfdm(char *package, char *type, struct uci_section **s); +int bbf_uci_add_section_bbfdm(const char *package, const char *type, struct uci_section **s); /*********************************************************************//** ** @@ -178,7 +178,7 @@ int bbf_uci_add_section_bbfdm(char *package, char *type, struct uci_section **s) ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_delete_section_bbfdm(char *package, char *type, char *option, char *value); +int bbf_uci_delete_section_bbfdm(const char *package, const char *type, const char *option, const char *value); /*********************************************************************//** ** @@ -192,7 +192,7 @@ int bbf_uci_delete_section_bbfdm(char *package, char *type, char *option, char * ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_rename_section(struct uci_section *s, char *value); +int bbf_uci_rename_section(struct uci_section *s, const char *value); /*********************************************************************//** ** @@ -208,7 +208,7 @@ int bbf_uci_rename_section(struct uci_section *s, char *value); ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_get_value(char *package, char *section, char *option, char **value); +int bbf_uci_get_value(const char *package, const char *section, const char *option, char **value); /*********************************************************************//** ** @@ -226,7 +226,7 @@ int bbf_uci_get_value(char *package, char *section, char *option, char **value); ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_set_value(char *package, char *section, char *option, char *value); +int bbf_uci_set_value(const char *package, const char *section, const char *option, const char *value); /*********************************************************************//** ** @@ -241,7 +241,7 @@ int bbf_uci_set_value(char *package, char *section, char *option, char *value); ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_get_value_by_section(struct uci_section *s, char *option, char **value); +int bbf_uci_get_value_by_section(struct uci_section *s, const char *option, char **value); /*********************************************************************//** ** @@ -257,7 +257,7 @@ int bbf_uci_get_value_by_section(struct uci_section *s, char *option, char **val ** \return uci option value if the value is not empty, empty otherwise ** **************************************************************************/ -char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, char *option, char *default_value); +char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, const char *option, const char *default_value); /*********************************************************************//** ** @@ -272,7 +272,7 @@ char *bbf_uci_get_value_by_section_fallback_def(struct uci_section *s, char *opt ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_set_value_by_section(struct uci_section *s, char *option, char *value); +int bbf_uci_set_value_by_section(struct uci_section *s, const char *option, const char *value); /*********************************************************************//** ** @@ -287,7 +287,7 @@ int bbf_uci_set_value_by_section(struct uci_section *s, char *option, char *valu ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_delete_section_by_section(struct uci_section *s, char *option, char *value); +int bbf_uci_delete_section_by_section(struct uci_section *s, const char *option, const char *value); /*********************************************************************//** ** @@ -301,7 +301,7 @@ int bbf_uci_delete_section_by_section(struct uci_section *s, char *option, char ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_get_section_name(char *sec_name, char **value); +int bbf_uci_get_section_name(const char *sec_name, char **value); /*********************************************************************//** ** @@ -315,10 +315,10 @@ int bbf_uci_get_section_name(char *sec_name, char **value); ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_uci_set_section_name(char *sec_name, char *str, size_t size); +int bbf_uci_set_section_name(const char *sec_name, char *str, size_t size); -struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, void *arg2, int cmp, int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk); +struct uci_section *bbf_uci_walk_section(const char *package, const char *type, const void *arg1, const void *arg2, int cmp, int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk); /******************* @@ -342,7 +342,7 @@ struct uci_section *bbf_uci_walk_section(char *package, char *type, void *arg1, ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_ubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res); +int bbf_ubus_call(const char *obj, const char *method, struct ubus_arg u_args[], int u_args_size, json_object **req_res); /*********************************************************************//** ** @@ -358,7 +358,7 @@ int bbf_ubus_call(char *obj, char *method, struct ubus_arg u_args[], int u_args_ ** \return 0 if the operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_ubus_call_set(char *obj, char *method, struct ubus_arg u_args[], int u_args_size); +int bbf_ubus_call_set(const char *obj, const char *method, struct ubus_arg u_args[], int u_args_size); /******************* @@ -445,7 +445,7 @@ char *bbf_strdup(const char *ptr); ** \param dup_list - pointer to the list of all sections ** **************************************************************************/ -void bbf_synchronise_config_sections_with_dmmap(char *package, char *section_type, char *dmmap_package, struct list_head *dup_list); +void bbf_synchronise_config_sections_with_dmmap(const char *package, const char *section_type, const char *dmmap_package, struct list_head *dup_list); /*********************************************************************//** ** @@ -519,7 +519,7 @@ int bbf_get_number_of_entries(struct dmctx *ctx, void *data, char *instance, int ** \return bool value if the operation is successful, false otherwise ** **************************************************************************/ -int bbf_convert_string_to_bool(char *str, bool *b); +int bbf_convert_string_to_bool(const char *str, bool *b); /*********************************************************************//** ** @@ -533,7 +533,7 @@ int bbf_convert_string_to_bool(char *str, bool *b); ** \param dmmap_section - pointer to the dmmap section, it should be 'NULL' if the section is not found ** **************************************************************************/ -void bbf_find_dmmap_section(char *dmmap_package, char *section_type, char *section_name, struct uci_section **dmmap_section); +void bbf_find_dmmap_section(const char *dmmap_package, const char *section_type, const char *section_name, struct uci_section **dmmap_section); /*********************************************************************//** ** @@ -548,7 +548,7 @@ void bbf_find_dmmap_section(char *dmmap_package, char *section_type, char *secti ** \param dmmap_section - pointer to the dmmap section, it should be 'NULL' if the section is not found ** **************************************************************************/ -void bbf_find_dmmap_section_by_option(char *dmmap_package, char *section_type, char *option_name, char *option_value, struct uci_section **dmmap_section); +void bbf_find_dmmap_section_by_option(const char *dmmap_package, const char *section_type, const char *option_name, const char *option_value, struct uci_section **dmmap_section); /*********************************************************************//** ** @@ -565,7 +565,7 @@ void bbf_find_dmmap_section_by_option(char *dmmap_package, char *section_type, c ** \return 0 if operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, char *instance, char **value); +int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, const char *option_name, const char *instance, char **value); /*********************************************************************//** ** @@ -582,7 +582,7 @@ int bbf_get_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, c ** \return 0 if operation is successful, -1 otherwise ** **************************************************************************/ -int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, char *instance, char *value); +int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, const char *option_name, const char *instance, const char *value); /*********************************************************************//** ** @@ -601,7 +601,7 @@ int bbf_set_alias(struct dmctx *ctx, struct uci_section *s, char *option_name, c ** \return 0 if operation is successful, -1 otherwise ** **************************************************************************/ -int bbfdm_get_references(struct dmctx *ctx, int match_action, const char *base_path, char *key_name, char *key_value, char *out, size_t out_len); +int bbfdm_get_references(struct dmctx *ctx, int match_action, const char *base_path, const char *key_name, char *key_value, char *out, size_t out_len); /*********************************************************************//** ** @@ -618,7 +618,7 @@ int bbfdm_get_references(struct dmctx *ctx, int match_action, const char *base_p ** \return 0 if operation is successful, -1 otherwise ** **************************************************************************/ -int _bbfdm_get_references(struct dmctx *ctx, const char *base_path, char *key_name, char *key_value, char **value); +int _bbfdm_get_references(struct dmctx *ctx, const char *base_path, const char *key_name, char *key_value, char **value); /*********************************************************************//** ** diff --git a/libbbfdm-api/plugin/dotso_plugin.c b/libbbfdm-api/plugin/dotso_plugin.c index 0db7fd08..686c95a9 100644 --- a/libbbfdm-api/plugin/dotso_plugin.c +++ b/libbbfdm-api/plugin/dotso_plugin.c @@ -22,7 +22,11 @@ struct loaded_library static void add_list_loaded_libraries(struct list_head *library_list, void *library) { - struct loaded_library *lib = calloc(1, sizeof(struct loaded_library)); + struct loaded_library *lib = NULL; + + lib = (struct loaded_library *)calloc(1, sizeof(struct loaded_library)); + if (!lib) + return; list_add_tail(&lib->list, library_list); lib->library = library; @@ -30,14 +34,16 @@ static void add_list_loaded_libraries(struct list_head *library_list, void *libr static void free_all_list_open_library(struct list_head *library_list) { - struct loaded_library *lib = NULL; + struct loaded_library *lib = NULL, *tmp = NULL; - while (library_list->next != library_list) { - lib = list_entry(library_list->next, struct loaded_library, list); + list_for_each_entry_safe(lib, tmp, library_list, list) { list_del(&lib->list); + if (lib->library) { dlclose(lib->library); + lib->library = NULL; } + FREE(lib); } } @@ -89,17 +95,17 @@ int load_dotso_plugins(DMOBJ *entryobj, const char *plugin_path) if (dynamic_obj[i].root_obj) { if (dm_entryobj->nextdynamicobj == NULL) { - dm_entryobj->nextdynamicobj = calloc(__INDX_DYNAMIC_MAX, sizeof(struct dm_dynamic_obj)); + dm_entryobj->nextdynamicobj = (struct dm_dynamic_obj *)calloc(__INDX_DYNAMIC_MAX, sizeof(struct dm_dynamic_obj)); dm_entryobj->nextdynamicobj[INDX_JSON_MOUNT].idx_type = INDX_JSON_MOUNT; dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].idx_type = INDX_LIBRARY_MOUNT; } if (dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj == NULL) { - dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj = calloc(2, sizeof(DMOBJ *)); + dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj = (DMOBJ **)calloc(2, sizeof(DMOBJ *)); dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj[0] = dynamic_obj[i].root_obj; } else { int idx = get_obj_idx(dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj); - dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj = realloc(dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj, (idx + 2) * sizeof(DMOBJ *)); + dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj = (DMOBJ **)realloc(dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj, (idx + 2) * sizeof(DMOBJ *)); dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj[idx] = dynamic_obj[i].root_obj; dm_entryobj->nextdynamicobj[INDX_LIBRARY_MOUNT].nextobj[idx+1] = NULL; } @@ -109,17 +115,17 @@ int load_dotso_plugins(DMOBJ *entryobj, const char *plugin_path) if (dynamic_obj[i].root_leaf) { if (dm_entryobj->dynamicleaf == NULL) { - dm_entryobj->dynamicleaf = calloc(__INDX_DYNAMIC_MAX, sizeof(struct dm_dynamic_leaf)); + dm_entryobj->dynamicleaf = (struct dm_dynamic_leaf *)calloc(__INDX_DYNAMIC_MAX, sizeof(struct dm_dynamic_leaf)); dm_entryobj->dynamicleaf[INDX_JSON_MOUNT].idx_type = INDX_JSON_MOUNT; dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].idx_type = INDX_LIBRARY_MOUNT; } if (dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf == NULL) { - dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf = calloc(2, sizeof(DMLEAF *)); + dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf = (DMLEAF **)calloc(2, sizeof(DMLEAF *)); dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf[0] = dynamic_obj[i].root_leaf; } else { int idx = get_leaf_idx(dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf); - dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf = realloc(dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf, (idx + 2) * sizeof(DMLEAF *)); + dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf = (DMLEAF **)realloc(dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf, (idx + 2) * sizeof(DMLEAF *)); dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf[idx] = dynamic_obj[i].root_leaf; dm_entryobj->dynamicleaf[INDX_LIBRARY_MOUNT].nextleaf[idx+1] = NULL; } diff --git a/libbbfdm-api/plugin/json_plugin.c b/libbbfdm-api/plugin/json_plugin.c index 8db58d83..f7c5f3bb 100644 --- a/libbbfdm-api/plugin/json_plugin.c +++ b/libbbfdm-api/plugin/json_plugin.c @@ -65,11 +65,11 @@ static void free_event_command_args(const char **arg_p) if (arg_p) { int i = 0; while (arg_p[i]) { - dmfree((char *)arg_p[i]); + dmfree(arg_p[i]); i++; } - free((char **)arg_p); + free(arg_p); } } @@ -229,9 +229,9 @@ static json_object *get_requested_json_obj(json_object *json_obj, char *instance char *p = strchr(buf, '['); if (strcmp(p+1, "@index") == 0 || strcmp(p+1, "@i-1") == 0) { - idx_pos = instance ? DM_STRTOL(instance)-1 : 1; + idx_pos = instance ? DM_STRTOUL(instance)-1 : 1; } else { - idx_pos = DM_STRTOL(p+1); + idx_pos = DM_STRTOUL(p+1); } *p = 0; @@ -318,7 +318,7 @@ static void replace_indexes(struct dmctx *ctx, char *old_key, char *new_key, siz } } -static void resolve_all_symbols(struct dmctx *ctx, void *data, char *instance, char *value, int nbr_instances, int json_version, +static void resolve_all_symbols(struct dmctx *ctx, void *data, char *instance, const char *value, int nbr_instances, int json_version, const char *old_key, char *new_key, size_t key_len) { char *pch = NULL, *pchr = NULL; @@ -363,7 +363,7 @@ static void resolve_all_symbols(struct dmctx *ctx, void *data, char *instance, c replace_indexes(ctx, new_key, new_key, key_len); } -static int fill_ubus_arguments(struct dmctx *ctx, void *data, char *instance, char *value, int nbr_instances, int json_version, +static int fill_ubus_arguments(struct dmctx *ctx, void *data, char *instance, const char *value, int nbr_instances, int json_version, struct json_object *args_obj, struct ubus_arg u_args[]) { int u_args_size = 0; @@ -397,8 +397,8 @@ static int fill_ubus_arguments(struct dmctx *ctx, void *data, char *instance, ch static void free_ubus_arguments(struct ubus_arg u_args[], int u_args_size) { for (int i = 0; i < u_args_size; i++) { - dmfree((char *)u_args[i].key); - dmfree((char *)u_args[i].val); + dmfree(u_args[i].key); + dmfree(u_args[i].val); } } @@ -669,7 +669,7 @@ static char *uci_get_value(json_object *mapping_obj, int json_version, char *ref struct json_object *list_name = NULL; struct json_object *linker_jobj = NULL; char *linker = NULL; - char *value = ""; + char *value = NULL; json_object_object_get_ex(mapping_obj, "uci", &obj); json_object_object_get_ex(obj, "file", &file); @@ -764,7 +764,7 @@ static char *uci_get_value(json_object *mapping_obj, int json_version, char *ref end: FREE(linker); - return value; + return value ? value : ""; } static char *ubus_get_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance) @@ -779,7 +779,7 @@ static char *ubus_get_value(json_object *mapping_obj, int json_version, char *re char buf_method[256] = {0}; struct ubus_arg u_args[16] = {0}; int u_args_size = 0; - char *value = ""; + char *value = NULL; int nbr_instances = get_number_of_instances(refparam); @@ -839,7 +839,7 @@ static char *ubus_get_value(json_object *mapping_obj, int json_version, char *re } } - return value; + return value ? value : ""; } static int handle_linker_json_object(struct json_object *linker_jobj, struct dmctx *ctx, char *in, char **out) @@ -864,7 +864,7 @@ static char *uci_v1_get_value(json_object *mapping_obj, char *refparam, struct d { struct json_object *data_s = NULL; struct json_object *key = NULL, *list = NULL, *linker_jobj = NULL; - char *value = ""; + char *value = NULL; json_object_object_get_ex(mapping_obj, "data", &data_s); json_object_object_get_ex(mapping_obj, "key", &key); @@ -905,7 +905,7 @@ static char *uci_v1_get_value(json_object *mapping_obj, char *refparam, struct d } end: - return value; + return value ? value : ""; } static char *ubus_v1_get_value(json_object *mapping_obj, char *refparam, struct dmctx *ctx, void *data, char *instance) @@ -913,7 +913,7 @@ static char *ubus_v1_get_value(json_object *mapping_obj, char *refparam, struct struct json_object *data_json = NULL; struct json_object *key = NULL; struct json_object *linker_jobj = NULL; - char *value = ""; + char *value = NULL; json_object_object_get_ex(mapping_obj, "data", &data_json); json_object_object_get_ex(mapping_obj, "key", &key); @@ -935,7 +935,7 @@ static char *ubus_v1_get_value(json_object *mapping_obj, char *refparam, struct } end: - return value; + return value ? value: ""; } static char *get_value_from_mapping(json_object *param_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance) @@ -1235,7 +1235,7 @@ static int fill_string_arguments(struct json_object *json_obj, int *min_length, return 0; } -static int dm_validate_value(struct dmctx *ctx, json_object *json_obj, char *value) +static int dm_validate_value(struct dmctx *ctx, json_object *json_obj, const char *value) { struct json_object *type_obj = NULL; @@ -1290,9 +1290,9 @@ static int dm_validate_value(struct dmctx *ctx, json_object *json_obj, char *val struct json_object *datatype_obj = NULL; struct json_object *maxsize = NULL; struct json_object *item_obj = NULL; - int min_item = -1; - int max_item = -1; - int max_size = -1; + int min_item = 0; + int max_item = 0; + int max_size = 0; json_object_object_get_ex(list_obj, "datatype", &datatype_obj); if (!datatype_obj) @@ -1361,7 +1361,7 @@ static int dm_validate_value(struct dmctx *ctx, json_object *json_obj, char *val return 0; } -static int uci_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, const void *data, char *instance, char *value) +static int uci_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, const char *value) { struct json_object *uci_obj = NULL; struct json_object *file = NULL; @@ -1428,10 +1428,14 @@ static int uci_set_value(json_object *mapping_obj, int json_version, char *refpa res = dmuci_set_value(json_object_get_string(file), uci_type, opt_temp, value); } else { if (value != NULL) { + char buf[1024] = {0}; + if (dmuci_delete_by_section(((struct dm_data *)data)->config_section, opt_temp, NULL)) dmuci_delete(json_object_get_string(file), uci_type, opt_temp, NULL); - char *p = strtok(value, ","); + DM_STRNCPY(buf, value, sizeof(buf)); + + char *p = strtok(buf, ","); while (p) { strip_lead_trail_whitespace(p); @@ -1450,9 +1454,13 @@ static int uci_set_value(json_object *mapping_obj, int json_version, char *refpa res = dmuci_set_value(json_object_get_string(file), json_object_get_string(section_name), opt_temp, value); } else { if (value != NULL) { + char buf[1024] = {0}; + dmuci_delete(json_object_get_string(file), json_object_get_string(section_name), opt_temp, NULL); - char *p = strtok(value, ","); + DM_STRNCPY(buf, value, sizeof(buf)); + + char *p = strtok(buf, ","); while (p) { strip_lead_trail_whitespace(p); @@ -1470,7 +1478,7 @@ static int uci_set_value(json_object *mapping_obj, int json_version, char *refpa return res; } -static int ubus_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, char *value) +static int ubus_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, const char *value) { struct json_object *ubus_obj = NULL; struct json_object *object = NULL; @@ -1505,7 +1513,7 @@ static int ubus_set_value(json_object *mapping_obj, int json_version, char *refp return res; } -static int uci_v1_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, bool is_dmmap) +static int uci_v1_set_value(json_object *mapping_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, const char *value, bool is_dmmap) { struct json_object *data_s = NULL; struct json_object *key = NULL, *list = NULL; @@ -1524,9 +1532,13 @@ static int uci_v1_set_value(json_object *mapping_obj, int json_version, char *re if (list_value) { if (value != NULL) { + char buf[1024] = {0}; + dmuci_delete_by_section(req_sec, list_value, NULL); - char *p = strtok(value, ","); + DM_STRNCPY(buf, value, sizeof(buf)); + + char *p = strtok(buf, ","); while (p) { strip_lead_trail_whitespace(p); dmuci_add_list_value_by_section(req_sec, list_value, p); @@ -1557,7 +1569,7 @@ static int uci_v1_set_value(json_object *mapping_obj, int json_version, char *re return -1; } -static int set_value_from_mapping(json_object *param_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, char *value) +static int set_value_from_mapping(json_object *param_obj, int json_version, char *refparam, struct dmctx *ctx, void *data, char *instance, const char *value) { struct json_object *type_obj = NULL, *mapping_arr = NULL, *mapping = NULL; int res = 0; @@ -1633,7 +1645,7 @@ static int setvalue_param(char *refparam, struct dmctx *ctx, void *data, char *i bbfdm_get_reference_linker(ctx, value, &reference); } else { reference.path = value; - reference.value = ""; + reference.value = dmstrdup(""); } switch (action) { @@ -1653,9 +1665,9 @@ static int setvalue_param(char *refparam, struct dmctx *ctx, void *data, char *i static bool is_obj(char *object, json_object *jobj) { json_object_object_foreach(jobj, key, json_obj) { - if((strcmp(key, "type") == 0) && (strcmp(json_object_get_string(json_obj), "object") == 0)) + if((DM_LSTRCMP(key, "type") == 0) && (strcmp(json_object_get_string(json_obj), "object") == 0)) return true; - else if((strcmp(key, "type") == 0) && (strcmp(json_object_get_string(json_obj), "object") != 0)) + else if((DM_LSTRCMP(key, "type") == 0) && (strcmp(json_object_get_string(json_obj), "object") != 0)) return false; } return false; @@ -1673,11 +1685,11 @@ static bool valid_event_param(char *param) { bool ret; - if (strcmp(param, "type") == 0) { + if (DM_LSTRCMP(param, "type") == 0) { ret = false; - } else if (strcmp(param, "version") == 0) { + } else if (DM_LSTRCMP(param, "version") == 0) { ret = false; - } else if (strcmp(param, "protocols") == 0) { + } else if (DM_LSTRCMP(param, "protocols") == 0) { ret = false; } else { ret = true; @@ -1686,13 +1698,13 @@ static bool valid_event_param(char *param) return ret; } -static char** fill_command_param(int count, struct json_object *obj) +static const char **fill_command_param(int count, struct json_object *obj) { - char **res_p = NULL; + const char **res_p = NULL; if (!obj || !count) return res_p; - res_p = malloc(sizeof(char *) * (count + 1)); + res_p = (const char **)calloc(count + 1, sizeof(char *)); if (res_p) { res_p[count] = NULL; int id = 0; @@ -1713,8 +1725,7 @@ static void parse_param(char *object, char *param, json_object *jobj, DMLEAF *pl char full_param[1024] = {0}; char param_ext[256] = {0}; size_t n_flags; - // cppcheck-suppress nullPointerRedundantCheck - char **in_p = NULL, **out_p = NULL, **ev_arg = NULL, **tmp = NULL; + const char **in_p = NULL, **out_p = NULL, **ev_arg = NULL, **tmp = NULL; if (!jobj || !pleaf) return; @@ -1769,7 +1780,7 @@ static void parse_param(char *object, char *param, json_object *jobj, DMLEAF *pl if (valid_event_param(key)) { param_count++; if (!ev_arg) { - ev_arg = malloc(sizeof(char*) * param_count); + ev_arg = (const char **)calloc(param_count, sizeof(char *)); if (!ev_arg) break; } else { @@ -1787,7 +1798,7 @@ static void parse_param(char *object, char *param, json_object *jobj, DMLEAF *pl if (ev_arg) { param_count++; - tmp = realloc(ev_arg, sizeof(char*) * param_count); + tmp = realloc(ev_arg, sizeof(const char *) * param_count); if (tmp == NULL) { FREE(ev_arg); } @@ -1850,7 +1861,7 @@ static void parse_param(char *object, char *param, json_object *jobj, DMLEAF *pl } snprintf(full_param, sizeof(full_param), "%s%s", object, param_ext); - save_json_data(list, full_param, jobj, json_version, (const char**)in_p, (const char**)out_p, (const char**)ev_arg); + save_json_data(list, full_param, jobj, json_version, in_p, out_p, ev_arg); } static void count_obj_param_under_jsonobj(json_object *jsonobj, int *obj_number, int *param_number) @@ -1858,10 +1869,10 @@ static void count_obj_param_under_jsonobj(json_object *jsonobj, int *obj_number, json_object_object_foreach(jsonobj, key, jobj) { if (json_object_get_type(jobj) == json_type_object) { json_object_object_foreach(jobj, key1, jobj1) { - if ((strcmp(key1, "type") == 0) && (strcmp(json_object_get_string(jobj1), "object") == 0)) { + if ((DM_LSTRCMP(key1, "type") == 0) && (DM_LSTRCMP(json_object_get_string(jobj1), "object") == 0)) { (*obj_number)++; break; - } else if (((strcmp(key1, "type") == 0) && (strcmp(json_object_get_string(jobj1), "object") != 0)) && (strcmp(key, "mapping") != 0)) { + } else if (((DM_LSTRCMP(key1, "type") == 0) && (DM_LSTRCMP(json_object_get_string(jobj1), "object") != 0)) && (DM_LSTRCMP(key, "mapping") != 0)) { (*param_number)++; break; } @@ -2041,7 +2052,7 @@ int load_json_plugins(DMOBJ *entryobj, const char *plugin_path) char obj_path[MAX_DM_LENGTH] = {0}; DMOBJ *dm_entryobj = NULL; - if (strcmp(key, "json_plugin_version") == 0) { + if (DM_LSTRCMP(key, "json_plugin_version") == 0) { json_plugin_version = get_json_plugin_version(jobj); continue; } diff --git a/libbbfdm-ubus/bbfdm-ubus.c b/libbbfdm-ubus/bbfdm-ubus.c index cc4ff25b..aaa44345 100644 --- a/libbbfdm-ubus/bbfdm-ubus.c +++ b/libbbfdm-ubus/bbfdm-ubus.c @@ -116,7 +116,7 @@ static void async_complete_cb(struct uloop_process *p, __attribute__((unused)) i static struct bbfdm_async_req *async_req_new(void) { - struct bbfdm_async_req *r = malloc(sizeof(*r)); + struct bbfdm_async_req *r = (struct bbfdm_async_req *)calloc(1, sizeof(*r)); if (r) { memset(&r->process, 0, sizeof(r->process)); @@ -883,7 +883,7 @@ static struct ubus_object bbf_object = { static void run_schema_updater(struct bbfdm_context *u) { - bool ret; + bool ret = false; char method_name[256] = {0}; ret = is_object_schema_update_available(u); @@ -1197,7 +1197,7 @@ static int daemon_load_config_internal_plugin(bbfdm_config_t *config) static int daemon_load_config(bbfdm_config_t *config) { - int err = -1; + int err = 0; if (INTERNAL_ROOT_TREE) { err = daemon_load_config_internal_plugin(config); @@ -1323,7 +1323,7 @@ static void bbfdm_ctx_init(struct bbfdm_context *bbfdm_ctx) static int daemon_load_data_model(struct bbfdm_context *daemon_ctx) { - int err = -1; + int err = 0; if (INTERNAL_ROOT_TREE) { BBF_INFO("Loading Data Model Internal plugin (%s)", daemon_ctx->config.service_name); diff --git a/libbbfdm-ubus/common.h b/libbbfdm-ubus/common.h index e439e427..602a8f39 100644 --- a/libbbfdm-ubus/common.h +++ b/libbbfdm-ubus/common.h @@ -14,7 +14,7 @@ #include #include -#include "dmcommon.h" +#include #include "bbfdm-ubus.h" #define STRINGIFY(x) #x diff --git a/libbbfdm-ubus/events.c b/libbbfdm-ubus/events.c index bb62efb0..2cc9ab9e 100644 --- a/libbbfdm-ubus/events.c +++ b/libbbfdm-ubus/events.c @@ -95,18 +95,19 @@ static void add_ubus_event_handler(struct ubus_event_handler *ev, const char *ev return; struct ev_handler_node *node = NULL; - node = (struct ev_handler_node *) malloc(sizeof(struct ev_handler_node)); + node = (struct ev_handler_node *)calloc(1, sizeof(struct ev_handler_node)); if (!node) { BBF_ERR("Out of memory!"); return; } - node->ev_name = ev_name ? strdup(ev_name) : NULL; - node->dm_path = dm_path ? strdup(dm_path) : NULL; - node->ev_handler = ev; INIT_LIST_HEAD(&node->list); list_add_tail(&node->list, ev_list); + + node->ev_handler = ev; + node->ev_name = ev_name ? strdup(ev_name) : NULL; + node->dm_path = dm_path ? strdup(dm_path) : NULL; } int register_events_to_ubus(struct ubus_context *ctx, struct list_head *ev_list) @@ -147,14 +148,13 @@ int register_events_to_ubus(struct ubus_context *ctx, struct list_head *ev_list) if (!param_name || !event_name || !strlen(event_name)) continue; - struct ubus_event_handler *ev = (struct ubus_event_handler *)malloc(sizeof(struct ubus_event_handler)); + struct ubus_event_handler *ev = (struct ubus_event_handler *)calloc(1, sizeof(struct ubus_event_handler)); if (!ev) { BBF_ERR("Out of memory!"); err = -1; goto end; } - memset(ev, 0, sizeof(struct ubus_event_handler)); ev->cb = bbfdm_event_handler; if (0 != ubus_register_event_handler(ctx, ev, event_name)) { diff --git a/libbbfdm-ubus/get.c b/libbbfdm-ubus/get.c index fb0ae594..0ca5fffc 100644 --- a/libbbfdm-ubus/get.c +++ b/libbbfdm-ubus/get.c @@ -731,7 +731,7 @@ static int resolve_path(struct dmctx *bbf_ctx, char *qPath, size_t pos, struct l struct pathNode *ptr; int fault; bool check = true; - size_t start; + size_t start = 0; bool non_leaf = false; LIST_HEAD(plist_local); @@ -845,7 +845,7 @@ void bbfdm_get_value(bbfdm_data_t *data, void *output) LIST_HEAD(resolved_list); if (DM_STRLEN(pn->path) == 0) - snprintf(pn->path, MAX_DM_PATH, ROOT_NODE); + snprintf(pn->path, MAX_DM_PATH, "%s", ROOT_NODE); fault = get_resolved_paths(&data->bbf_ctx, pn->path, &resolved_list); diff --git a/libbbfdm-ubus/get_helper.c b/libbbfdm-ubus/get_helper.c index 4719cb46..b34d7bf6 100644 --- a/libbbfdm-ubus/get_helper.c +++ b/libbbfdm-ubus/get_helper.c @@ -21,19 +21,6 @@ DMOBJ *DEAMON_DM_ROOT_OBJ = NULL; DM_MAP_OBJ *INTERNAL_ROOT_TREE = NULL; -static jmp_buf gs_jump_location; -static bool gs_jump_called_by_bbf = false; - -void handle_pending_signal(int sig) -{ - if (gs_jump_called_by_bbf) { - siglongjmp(gs_jump_location, 1); - } - - BBF_ERR("Exception [%d] not cause by bbf dm, exit with error", sig); - exit(1); -} - int bbfdm_cmd_exec(struct dmctx *bbf_ctx, int cmd) { int fault = 0; @@ -41,20 +28,7 @@ int bbfdm_cmd_exec(struct dmctx *bbf_ctx, int cmd) if (bbf_ctx->in_param == NULL) return USP_FAULT_INTERNAL_ERROR; - if (sigsetjmp(gs_jump_location, 1) == 0) { - gs_jump_called_by_bbf = true; - fault = bbf_entry_method(bbf_ctx, cmd); - } else { - BBF_ERR("PID [%d]::Exception on [%d => %s]", getpid(), cmd, bbf_ctx->in_param); - fault = USP_FAULT_INTERNAL_ERROR; - if (dm_is_micro_service()) { - BBF_ERR("Micro-service PID [%d]::Exception on [%d => %s]", getpid(), cmd, bbf_ctx->in_param); - exit(-1); - } - } - - gs_jump_called_by_bbf = false; - + fault = bbf_entry_method(bbf_ctx, cmd); if (fault) BBF_WARNING("Fault [%d => %d => %s]", fault, cmd, bbf_ctx->in_param); @@ -98,23 +72,23 @@ bool present_in_path_list(struct list_head *plist, char *entry) return false; } -void add_pv_list(char *para, char *val, char *type, struct list_head *pv_list) +void add_pv_list(const char *para, const char *val, const char *type, struct list_head *pv_list) { struct pvNode *node = NULL; - node = (struct pvNode *) malloc(sizeof(*node)); + node = (struct pvNode *)calloc(1, sizeof(*node)); if (!node) { BBF_ERR("Out of memory!"); return; } + INIT_LIST_HEAD(&node->list); + list_add_tail(&node->list, pv_list); + node->param = (para) ? strdup(para) : strdup(""); node->val = (val) ? strdup(val) : strdup(""); node->type = (type) ? strdup(type) : strdup(""); - - INIT_LIST_HEAD(&node->list); - list_add_tail(&node->list, pv_list); } void free_pv_list(struct list_head *pv_list) @@ -131,7 +105,7 @@ void free_pv_list(struct list_head *pv_list) } } -void add_path_list(char *param, struct list_head *plist) +void add_path_list(const char *param, struct list_head *plist) { struct pathNode *node = NULL; size_t len; @@ -143,11 +117,11 @@ void add_path_list(char *param, struct list_head *plist) return; } - len = DM_STRLEN(param); - strncpyt(node->path, param, len + 1); - INIT_LIST_HEAD(&node->list); list_add_tail(&node->list, plist); + + len = DM_STRLEN(param); + strncpyt(node->path, param, len + 1); } void free_path_list(struct list_head *plist) @@ -199,8 +173,8 @@ static int CountConsecutiveDigits(char *p) static int compare_path(const void *arg1, const void *arg2) { - struct pvNode *pv1 = (struct pvNode *)arg1; - struct pvNode *pv2 = (struct pvNode *)arg2; + const struct pvNode *pv1 = (const struct pvNode *)arg1; + const struct pvNode *pv2 = (const struct pvNode *)arg2; char *s1 = pv1->param; char *s2 = pv2->param; @@ -256,7 +230,7 @@ struct pvNode *sort_pv_path(struct list_head *pv_list, size_t pv_count) if (list_empty(pv_list) || pv_count == 0) return NULL; - struct pvNode *arr = malloc(sizeof(struct pvNode) * pv_count); + struct pvNode *arr = (struct pvNode *)calloc(pv_count, sizeof(struct pvNode)); if (arr == NULL) return NULL; diff --git a/libbbfdm-ubus/get_helper.h b/libbbfdm-ubus/get_helper.h index 6597c28f..e6837862 100644 --- a/libbbfdm-ubus/get_helper.h +++ b/libbbfdm-ubus/get_helper.h @@ -17,8 +17,6 @@ struct pathNode { char path[MAX_DM_PATH]; }; -void handle_pending_signal(int sig); - int bbfdm_cmd_exec(struct dmctx *bbf_ctx, int cmd); void bbf_init(struct dmctx *dm_ctx); @@ -28,10 +26,10 @@ void bbf_sub_cleanup(struct dmctx *dm_ctx); bool present_in_path_list(struct list_head *plist, char *entry); -void add_pv_list(char *para, char *val, char *type, struct list_head *pv_list); +void add_pv_list(const char *para, const char *val, const char *type, struct list_head *pv_list); void free_pv_list(struct list_head *pv_list); -void add_path_list(char *param, struct list_head *plist); +void add_path_list(const char *param, struct list_head *plist); void free_path_list(struct list_head *plist); void fill_err_code_table(bbfdm_data_t *data, int fault); diff --git a/libbbfdm-ubus/plugin.c b/libbbfdm-ubus/plugin.c index c1097dba..404faeff 100644 --- a/libbbfdm-ubus/plugin.c +++ b/libbbfdm-ubus/plugin.c @@ -15,7 +15,8 @@ #include "common.h" #include "get_helper.h" -#include "plugin/json_plugin.h" +#include "../libbbfdm-api/plugin/json_plugin.h" + extern struct list_head loaded_json_files; extern struct list_head json_list; @@ -182,6 +183,9 @@ int bbfdm_load_json_plugin(struct list_head *json_plugin, struct list_head *json json_object_object_foreach(json_obj, key, jobj) { char node_obj[1024] = {0}; + if (key == NULL) + continue; + if (strcmp(key, "json_plugin_version") == 0) { json_plugin_version = get_json_plugin_version(jobj); continue; diff --git a/libbbfdm-ubus/pretty_print.c b/libbbfdm-ubus/pretty_print.c index d0f0fbda..5f16fbed 100644 --- a/libbbfdm-ubus/pretty_print.c +++ b/libbbfdm-ubus/pretty_print.c @@ -48,7 +48,7 @@ static bool is_res_required(char *str, size_t *start, size_t *len) BBF_DEBUG("Entry |%s|", str); if (match(str, GLOB_CHAR, 0, NULL)) { - size_t s_len, b_len, p_len; + size_t s_len = 0, b_len = 0, p_len = 0; char *star, *b_start, *b_end, *plus; char temp_char[MAX_DM_KEY_LEN] = {'\0'}; @@ -241,18 +241,18 @@ static void add_result_node(struct list_head *rlist, char *key, char *cookie) { struct resultstack *rnode = NULL; - rnode = (struct resultstack *) malloc(sizeof(*rnode)); + rnode = (struct resultstack *)calloc(1, sizeof(*rnode)); if (!rnode) { BBF_ERR("Out of memory!"); return; } + INIT_LIST_HEAD(&rnode->list); + list_add(&rnode->list, rlist); + rnode->key = (key) ? strdup(key) : strdup(""); rnode->cookie = cookie; BBF_DEBUG("## ResSTACK ADD (%s) ##", rnode->key); - - INIT_LIST_HEAD(&rnode->list); - list_add(&rnode->list, rlist); } static bool is_leaf_element(char *path) @@ -298,7 +298,6 @@ static bool add_paths_to_stack(struct blob_buf *bb, char *path, size_t begin, void *c; char *k; - ptr = path + begin; if (is_leaf_element(ptr)) { add_data_blob(bb, ptr, pv->val, pv->type); diff --git a/libbbfdm/device.c b/libbbfdm/device.c index e4ab1665..0cd916de 100644 --- a/libbbfdm/device.c +++ b/libbbfdm/device.c @@ -22,7 +22,7 @@ **************************************************************/ static int get_Device_RootDataModelVersion(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - *value = "2.17"; + *value = dmstrdup("2.17"); return 0; } diff --git a/libbbfdm/deviceinfo.c b/libbbfdm/deviceinfo.c index d982710d..f05d7214 100644 --- a/libbbfdm/deviceinfo.c +++ b/libbbfdm/deviceinfo.c @@ -321,7 +321,7 @@ static void init_processes(void) if (process_count == 0 || !(pentry_exits = check_entry_exists(entry->d_name))) { - pentry = dm_dynamic_malloc(&global_memhead, sizeof(struct process_entry)); + pentry = dm_dynamic_calloc(&global_memhead, 1, sizeof(struct process_entry)); if (!pentry) return; @@ -564,12 +564,14 @@ int bbf_config_restore(const char *url, const char *username, const char *passwo const char *file_size, const char *checksum_algorithm, const char *checksum, const char *command, const char *obj_path) { - char config_restore[256] = "/tmp/bbf_config_restore"; + char config_restore[256] = {0}; int res = 0; char fault_msg[128] = {0}; time_t complete_time = 0; time_t start_time = time(NULL); + DM_STRNCPY(config_restore, "/tmp/bbf_config_restore", sizeof(config_restore)); + // Check the file system size if there is sufficient space for downloading the config file if (!validate_file_system_size(file_size)) { snprintf(fault_msg, sizeof(fault_msg), "Available memory space is less than required for the operation"); @@ -662,7 +664,7 @@ static int bbf_fw_image_download(const char *url, const char *auto_activate, con const char *file_size, const char *checksum_algorithm, const char *checksum, const char *bank_id, const char *command, const char *obj_path, const char *commandKey, char *keep) { - char fw_image_path[256] = "/tmp/firmware-XXXXXX"; + char fw_image_path[256] = {0}; json_object *json_obj = NULL; bool activate = false, valid = false; int res = 0; @@ -670,6 +672,8 @@ static int bbf_fw_image_download(const char *url, const char *auto_activate, con time_t complete_time = 0; time_t start_time = time(NULL); + DM_STRNCPY(fw_image_path, "/tmp/firmware-XXXXXX", sizeof(fw_image_path)); + // Check the file system size if there is sufficient space for downloading the firmware image if (!validate_file_system_size(file_size)) { res = -1; @@ -704,7 +708,7 @@ static int bbf_fw_image_download(const char *url, const char *auto_activate, con goto end; } - string_to_bool((char *)auto_activate, &activate); + string_to_bool(auto_activate, &activate); char *act = (activate) ? "1" : "0"; dmubus_call_blocking("system", "validate_firmware_image", UBUS_ARGS{{"path", fw_image_path, String}}, 1, &json_obj); @@ -983,7 +987,7 @@ static int get_device_active_fwimage(char *refparam, struct dmctx *ctx, void *da } if (DM_STRLEN(id) == 0) { - *value = ""; + *value = dmstrdup(""); return 0; } @@ -1008,7 +1012,7 @@ static int get_device_boot_fwimage(char *refparam, struct dmctx *ctx, void *data } if (DM_STRLEN(id) == 0) { - *value = ""; + *value = dmstrdup(""); return 0; } @@ -1224,7 +1228,7 @@ static int get_vcf_date(char *refparam, struct dmctx *ctx, void *data, char *ins struct dirent *d_file = NULL; char *config_name = NULL; - *value = "0001-01-01T00:00:00Z"; + *value = dmstrdup("0001-01-01T00:00:00Z"); dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "name", &config_name); if ((dir = opendir (DEFAULT_CONFIG_DIR)) != NULL) { while ((d_file = readdir (dir)) != NULL) { @@ -1296,7 +1300,7 @@ static int get_vlf_max_size(char *refparam, struct dmctx *ctx, void *data, char static int get_vlf_persistent(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - *value = "0"; + *value = dmstrdup("0"); return 0; } @@ -1343,10 +1347,16 @@ static int get_DeviceInfoProcessor_Architecture(char *refparam, struct dmctx *ct return 0; if (DM_LSTRSTR(utsname.machine, "arm") || DM_LSTRSTR(utsname.machine, "aarch64")) { - *value = "arm"; + *value = dmstrdup("arm"); } else if(DM_LSTRSTR(utsname.machine, "mips")) { - const bool is_big_endian = IS_BIG_ENDIAN; - *value = (is_big_endian) ? "mipseb" : "mipsel"; + union { + uint16_t value; + uint8_t bytes[2]; + } endian_test = { .bytes = { 0x00, 0xff } }; + + const bool is_big_endian = (endian_test.value < 0x100); + + *value = (is_big_endian) ? dmstrdup("mipseb") : dmstrdup("mipsel"); } else *value = dmstrdup(utsname.machine); return 0; @@ -1457,7 +1467,7 @@ static int get_DeviceInfoFirmwareImage_Available(char *refparam, struct dmctx *c } if ((*value)[0] == '\0') - *value = "true"; + *value = dmstrdup("true"); return 0; } @@ -1507,7 +1517,7 @@ static int get_memory_status_total(char* refparam, struct dmctx *ctx, void *data { json_object *res = NULL; dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); - DM_ASSERT(res, *value = "0"); + DM_ASSERT(res, *value = dmstrdup("0")); char *total = dmjson_get_value(res, 2, "memory", "total"); dmasprintf(value, "%lu", DM_STRTOUL(total) / 1024); return 0; @@ -1517,7 +1527,7 @@ static int get_memory_status_free(char* refparam, struct dmctx *ctx, void *data, { json_object *res = NULL; dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); - DM_ASSERT(res, *value = "0"); + DM_ASSERT(res, *value = dmstrdup("0")); char *free = dmjson_get_value(res, 2, "memory", "free"); dmasprintf(value, "%lu", DM_STRTOUL(free) / 1024); return 0; @@ -1530,7 +1540,7 @@ static int get_memory_status_total_persistent(char* refparam, struct dmctx *ctx, unsigned int total = (dinfo.f_bsize * dinfo.f_blocks) / 1024; dmasprintf(value, "%u", total); } else { - *value = "0"; + *value = dmstrdup("0"); } return 0; @@ -1543,7 +1553,7 @@ static int get_memory_status_free_persistent(char* refparam, struct dmctx *ctx, unsigned int free = (dinfo.f_bsize * dinfo.f_bavail) / 1024; dmasprintf(value, "%u", free); } else { - *value = "0"; + *value = dmstrdup("0"); } return 0; @@ -1617,13 +1627,16 @@ static int get_operate_args_DeviceInfoVendorLogFile_Upload(char *refparam, struc } static int operate_DeviceInfoVendorLogFile_Upload(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { + const char *upload_command = "Upload()"; char upload_path[256] = {'\0'}; - char upload_command[32] = {'\0'}; char *vlf_file_path = NULL; char *ret = DM_STRRCHR(refparam, '.'); - strncpy(upload_path, refparam, ret - refparam +1); - DM_STRNCPY(upload_command, ret+1, sizeof(upload_command)); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(upload_path)) + snprintf(upload_path, ret - refparam + 2, "%s", refparam); char *user = dmjson_get_value((json_object *)value, 1, "Username"); char *pass = dmjson_get_value((json_object *)value, 1, "Password"); @@ -1635,12 +1648,29 @@ static int operate_DeviceInfoVendorLogFile_Upload(char *refparam, struct dmctx * dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "log_file", &vlf_file_path); if (DM_STRLEN(vlf_file_path) == 0) { - vlf_file_path = DEF_VENDOR_LOG_FILE; - char cmd[64] = {0}; - snprintf(cmd, sizeof(cmd), "logread > %s", DEF_VENDOR_LOG_FILE); - if (system(cmd) == -1) { + vlf_file_path = dmstrdup(DEF_VENDOR_LOG_FILE); + char buffer[256] = {0}; + + // Open the log file for writing + FILE *logfile = fopen(DEF_VENDOR_LOG_FILE, "w"); + if (logfile == NULL) + return USP_FAULT_COMMAND_FAILURE; + + // Use popen to run "logread" command and open a pipe to read its output + FILE *fp = popen("logread", "r"); // flawfinder: ignore + if (fp == NULL) { + fclose(logfile); return USP_FAULT_COMMAND_FAILURE; } + + // Read the output of "logread" and write it to the log file + while (fgets(buffer, sizeof(buffer), fp) != NULL) { + fputs(buffer, logfile); + } + + // Close the pipe and the log file + pclose(fp); + fclose(logfile); } int res = bbf_upload_log(url, user, pass, vlf_file_path, upload_command, upload_path); @@ -1668,13 +1698,16 @@ static int get_operate_args_DeviceInfoVendorConfigFile_Backup(char *refparam, st static int operate_DeviceInfoVendorConfigFile_Backup(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { + const char *backup_command = "Backup()"; char backup_path[256] = {'\0'}; - char backup_command[32] = {'\0'}; char *vcf_name = NULL; char *ret = DM_STRRCHR(refparam, '.'); - strncpy(backup_path, refparam, ret - refparam +1); - DM_STRNCPY(backup_command, ret+1, sizeof(backup_command)); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(backup_path)) + snprintf(backup_path, ret - refparam + 2, "%s", refparam); char *url = dmjson_get_value((json_object *)value, 1, "URL"); if (url[0] == '\0') @@ -1711,12 +1744,15 @@ static int get_operate_args_DeviceInfoVendorConfigFile_Restore(char *refparam, s static int operate_DeviceInfoVendorConfigFile_Restore(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { + const char *restore_command = "Restore()"; char restore_path[256] = {'\0'}; - char restore_command[32] = {'\0'}; char *ret = DM_STRRCHR(refparam, '.'); - DM_STRNCPY(restore_path, refparam, ret - refparam + 2); - DM_STRNCPY(restore_command, ret+1, sizeof(restore_command)); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(restore_path)) + snprintf(restore_path, ret - refparam + 2, "%s", refparam); char *url = dmjson_get_value((json_object *)value, 1, "URL"); if (url[0] == '\0') @@ -1756,12 +1792,15 @@ static int get_operate_args_DeviceInfoFirmwareImage_Download(char *refparam, str static int operate_DeviceInfoFirmwareImage_Download(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { - char obj_path[256] = {'\0'}; - char command[32] = {'\0'}; + const char *command = "Download()"; + char obj_path[256] = {0}; char *ret = DM_STRRCHR(refparam, '.'); - DM_STRNCPY(obj_path, refparam, ret - refparam + 2); - DM_STRNCPY(command, ret+1, sizeof(command)); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(obj_path)) + snprintf(obj_path, ret - refparam + 2, "%s", refparam); char *url = dmjson_get_value((json_object *)value, 1, "URL"); char *auto_activate = dmjson_get_value((json_object *)value, 1, "AutoActivate"); @@ -1771,7 +1810,7 @@ static int operate_DeviceInfoFirmwareImage_Download(char *refparam, struct dmctx // Assuming auto activate as false, if not provided by controller, in case of strict validation, // this should result into a fault if (DM_STRLEN(auto_activate) == 0) - auto_activate="0"; + auto_activate = dmstrdup("0"); char *username = dmjson_get_value((json_object *)value, 1, "Username"); char *password = dmjson_get_value((json_object *)value, 1, "Password"); diff --git a/libbbfdm/schedules.c b/libbbfdm/schedules.c index 0a772fcb..c26b9219 100644 --- a/libbbfdm/schedules.c +++ b/libbbfdm/schedules.c @@ -350,6 +350,7 @@ static int get_schedule_start(char *refparam, struct dmctx *ctx, void *data, cha static int set_schedule_start(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { + const char *reg_exp = "^([01][0-9]|2[0-3]):[0-5][0-9]$"; int ret = 0; switch (action) { @@ -357,7 +358,6 @@ static int set_schedule_start(char *refparam, struct dmctx *ctx, void *data, cha if (bbfdm_validate_string(ctx, value, -1, 5, NULL, NULL)) ret = FAULT_9007; - char *reg_exp = "^([01][0-9]|2[0-3]):[0-5][0-9]$"; if (match(value, reg_exp, 0, NULL) != true) ret = FAULT_9007; break; @@ -405,12 +405,12 @@ static int get_schedule_status(char *refparam, struct dmctx *ctx, void *data, ch string_to_bool(val, &inst_enable); if (glob_enable == false && inst_enable == true) { - *value = "StackDisabled"; + *value = dmstrdup("StackDisabled"); return 0; } if (!inst_enable) { - *value = "Inactive"; + *value = dmstrdup("Inactive"); return 0; } diff --git a/libbbfdm/security.c b/libbbfdm/security.c index 53ecd067..f25d1e30 100644 --- a/libbbfdm/security.c +++ b/libbbfdm/security.c @@ -59,10 +59,12 @@ static char *get_certificate_sig_alg(int sig_nid) return ""; } -static char *generate_serial_number(char *text, int length) +static char *generate_serial_number(const char *text, int length) { - char *hex = (char *)dmcalloc(100, sizeof(char)); unsigned pos = 0; + char *hex = (char *)dmcalloc(100, sizeof(char)); + if (!hex) + return dmstrdup(""); for (int i = 0; i < length; i++) { pos += snprintf(&hex[pos], 100 - pos, "%02x:", text[i] & 0xff); @@ -74,7 +76,7 @@ static char *generate_serial_number(char *text, int length) return hex; } -static int fill_certificate_paths(char *dir_path, int *cidx) +static int fill_certificate_paths(const char *dir_path, int *cidx) { struct dirent *d_file = NULL; DIR *dir = NULL; @@ -199,11 +201,13 @@ static int get_Security_CertificateNumberOfEntries(char *refparam, struct dmctx static int get_SecurityCertificate_LastModif(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { struct certificate_profile *cert_profile = (struct certificate_profile *)((struct dm_data *)data)->additional_data; - char buf[sizeof("AAAA-MM-JJTHH:MM:SSZ")] = "0001-01-01T00:00:00Z"; + char buf[sizeof("0001-01-01T00:00:00Z")] = {0}; struct stat b; if (!stat(cert_profile->path, &b)) strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&b.st_mtime)); + else + DM_STRNCPY(buf, "0001-01-01T00:00:00Z", sizeof("0001-01-01T00:00:00Z")); *value = dmstrdup(buf); return 0;