diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b1eb9973d964f22e8d2f0b36aed59fd2ede1f757..03720069806d5ded1361e3dd1fc8ca0c232d9406 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,8 +12,9 @@ variables: DEBUG: 'TRUE' SOURCE_FOLDER: "src" SUPERVISOR_LOG: "$CI_PROJECT_DIR/supervisord.log" - RUN_CPPCHECK: "cppcheck --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=unreadVariable --suppress=variableScope --suppress=syntaxError ." - CPPCHECK_OPTIONS: "--enable=all --inline-suppr --error-exitcode=1 --suppress=unmatchedSuppression --suppress=unusedFunction --suppress=unreadVariable --suppress=variableScope --suppress=syntaxError --suppress=missingIncludeSystem --suppress=missingInclude --suppress=constParameter --suppress=redundantAssignment" + RUN_CPPCHECK: "cppcheck --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=unreadVariable --suppress=syntaxError ." + CPPCHECK_OPTIONS: "--enable=all --inline-suppr --error-exitcode=1 -suppress=unusedFunction --suppress=missingInclude" + RUN_CPD: "/home/user/pmd-bin-6.19.0/bin/run.sh cpd --minimum-tokens 200 --language c --exclude ./test/cmocka --exclude ./src/tests --exclude ./src/extensions --files" CPD_OPTIONS: " --minimum-tokens 5000 --language c --exclude ./test/cmocka --exclude ./src/tests --exclude ./src/extensions --files" diff --git a/src/cmdu.c b/src/cmdu.c index 07c501b07916b9b6871fa6b3515c823e87ebfa4d..e673c8cd7796520955067aedf645a8ab99ef4b6c 100644 --- a/src/cmdu.c +++ b/src/cmdu.c @@ -799,7 +799,7 @@ int cmdu_parse_tlvs(struct cmdu_buff *c, struct tlv *tv[][TLV_MAXNUM], */ struct tlv *cmdu_extract_tlv(struct cmdu_buff *c, uint8_t tlv_type) { - struct tlv *t, *tmp; + struct tlv *t; int found = 0; int inlen; @@ -818,6 +818,7 @@ struct tlv *cmdu_extract_tlv(struct cmdu_buff *c, uint8_t tlv_type) if (found) { uint16_t tlen = tlv_total_length(t); + struct tlv *tmp; tmp = tlv_alloc(tlv_length(t)); if (tmp) diff --git a/src/cmdu_ackq.c b/src/cmdu_ackq.c index 727d59fdd48de3bdc5df2e5f8764a58daacaa415..a69e5fffd6c9e1b805b07c3ebceb87505ea84fe1 100644 --- a/src/cmdu_ackq.c +++ b/src/cmdu_ackq.c @@ -142,7 +142,7 @@ static void cmdu_ackq_ageout_entry(struct cmdu_ackq *st, struct hlist_head *head __func__, msg->ageing_tmo.tv_sec, msg->ageing_tmo.tv_usec, now.tv_sec, now.tv_usec); - if (!timercmp(&msg->ageing_tmo, &now, >)) { + if (!timercmp(&msg->ageing_tmo, &now, >)) { /* cppcheck-suppress syntaxError */ dbg("%s(): No response from " MACFMT " with CMDU 0x%x mid = %hu\n", __func__, MAC2STR(msg->origin), msg->type, msg->mid); diff --git a/src/cmdufrag.c b/src/cmdufrag.c index b20b91fba0ef705097e47d4a68d4790d99346f60..a668e44858b4d7b504bd897d05a1a808820830ce 100644 --- a/src/cmdufrag.c +++ b/src/cmdufrag.c @@ -407,7 +407,7 @@ int cmdufrag_queue_enqueue(void *rxfq, struct cmdu_buff *cmdu, uint32_t timeout) hlist_add_head(&frag->hlist, &q->table[idx]); if (timer_pending(&q->ageing_timer)) { - if (timercmp(&q->next_tmo, &frag->ageing_tmo, >)) { + if (timercmp(&q->next_tmo, &frag->ageing_tmo, >)) { /* cppcheck-suppress syntaxError */ q->next_tmo.tv_sec = frag->ageing_tmo.tv_sec; q->next_tmo.tv_usec = frag->ageing_tmo.tv_usec; diff --git a/src/config.c b/src/config.c index 12386a0dc91b04624bbe6577ad19ed148a7632ad..b563d658f919f683fea02d923a8725985542c022 100644 --- a/src/config.c +++ b/src/config.c @@ -394,9 +394,10 @@ static int i1905_config_get_base(struct i1905_config *cfg, struct uci_section *s if (tb[I1905_UUID]) { char *val = tb[I1905_UUID]->v.string; - char uuidstr[37] = {0}; if (strlen(val) == 36) { + char uuidstr[37] = {0}; + strncpy(uuidstr, val, sizeof(uuidstr)); uuid_strtob(uuidstr, cfg->uuid); } @@ -930,10 +931,11 @@ int i1905_reconfig(struct i1905_config *cfg, struct i1905_useropts *u) if (cfg->update_config) { struct uci_section *s; - char alstr[18] = {0}; s = uci_lookup_section(ctx, pkg, "ieee1905"); if (s) { + char alstr[18] = {0}; + hwaddr_ntoa(cfg->macaddr, alstr); uci_add_option(ctx, pkg, s, "macaddress", alstr, false); uci_commit(ctx, &pkg, false); diff --git a/src/i1905.c b/src/i1905.c index bbce3c1aedaa145ebdbc993547832fee54d40395..88bca21204cd0776f173aab2fdd3d1aec9dd6826 100644 --- a/src/i1905.c +++ b/src/i1905.c @@ -270,7 +270,6 @@ static void i1905_bypass_bridge_flow(uint8_t *macaddr) void i1905_print_interfaces(struct i1905_private *priv) { - struct i1905_selfdevice_interface *ifs; struct i1905_selfdevice *self; struct i1905_interface *iface; @@ -281,6 +280,8 @@ void i1905_print_interfaces(struct i1905_private *priv) dbg("Local interfaces ---\n"); self = &priv->dm.self; if (!list_empty(&self->local_iflist)) { + struct i1905_selfdevice_interface *ifs = NULL; + list_for_each_entry(ifs, &self->local_iflist, list) { dbg("%-16s: macaddr = " MACFMT ", exclude = %d\n", ifs->ifname, MAC2STR(ifs->macaddr), ifs->exclude); @@ -913,13 +914,12 @@ static void i1905_recv_1905(struct uloop_fd *fd, unsigned int events) container_of(fd, struct i1905_interface_private, uloop_1905); struct i1905_interface *iface = i1905_interface_priv(ifpriv); struct i1905_private *priv = (struct i1905_private *)ifpriv->i1905private; - int res; for (;;) { struct cmdu_buff *rxf = NULL; int eth_hdrsize = 14; - + int res; rxf = cmdu_alloc_default(); if (!rxf) { @@ -1091,12 +1091,12 @@ static void i1905_recv_lldp(struct uloop_fd *fd, unsigned int events) container_of(fd, struct i1905_interface_private, uloop_lldp); struct i1905_interface *iface = i1905_interface_priv(ifpriv); struct i1905_private *priv = (struct i1905_private *)ifpriv->i1905private; - int res; for (;;) { struct cmdu_buff *rxf = NULL; int eth_hdrsize = 14; + int res; rxf = cmdu_alloc_default(); @@ -1475,7 +1475,6 @@ struct i1905_interface *i1905_setup_interface(struct i1905_private *priv, int i1905_add_master_interface(struct i1905_private *priv, const char *ifname) { struct i1905_master_interface *m = NULL; - int ret; list_for_each_entry(m, &priv->dm.self.miflist, list) { @@ -1487,6 +1486,7 @@ int i1905_add_master_interface(struct i1905_private *priv, const char *ifname) if (m) { struct ip_address ips[32] = {0}; int num = 32; + int ret; snprintf(m->ifname, 16, "%s", ifname); m->ifindex = if_nametoindex(ifname); @@ -1771,7 +1771,7 @@ int i1905_handle_iflink_change(struct i1905_private *priv, const char *ifname) return ret; } -int i1905_rebind_interface(struct i1905_private *priv, struct i1905_interface *iface) +int i1905_rebind_interface(const struct i1905_private *priv, struct i1905_interface *iface) { int ret; diff --git a/src/i1905.h b/src/i1905.h index bc9b005fdddbefc5b93cec1184ce18997bc2ab7f..4da09f92a9dd4e43d009f0691e85845af4000e3c 100644 --- a/src/i1905.h +++ b/src/i1905.h @@ -138,7 +138,7 @@ struct i1905_interface *i1905_setup_interface(struct i1905_private *priv, const char *ifname); void i1905_teardown_interface(struct i1905_private *priv, const char *ifname); -int i1905_rebind_interface(struct i1905_private *priv, struct i1905_interface *iface); +int i1905_rebind_interface(const struct i1905_private *priv, struct i1905_interface *iface); void i1905_print_interfaces(struct i1905_private *priv); diff --git a/src/i1905_al.c b/src/i1905_al.c index 23cc09fc6281de55b6480ab0faebd781837036de..7f2e00de21b8d51c253be00bae0a814d82cfb58b 100644 --- a/src/i1905_al.c +++ b/src/i1905_al.c @@ -76,7 +76,6 @@ int i1905_dm_update_interface_linkmetrics(struct i1905_private *priv, { struct i1905_neighbor_interface *nif = NULL; struct if_stats s = {0}; - int ret; if (!iface) @@ -84,6 +83,8 @@ int i1905_dm_update_interface_linkmetrics(struct i1905_private *priv, if (!IS_MEDIA_WIFI(iface->media)) { + int ret; + ret = if_getstats(iface->ifname, &s); if (ret) return -1; @@ -302,7 +303,7 @@ int i1905_dm_update_interface_self(struct i1905_private *p, } iface->mediatype = IF_MEDIA_WIFI; - ret = platform_wifi_get_mediainfo(iface->ifname, + platform_wifi_get_mediainfo(iface->ifname, &iface->media, &iface->num_mediainfo, (void **)&iface->mediainfo); @@ -328,10 +329,11 @@ int i1905_dm_update_master_interfaces(struct i1905_private *p) struct i1905_master_interface *m, *tmp; struct ip_address ips[32] = {0}; int num = 32; - int ifindex; int ret; list_for_each_entry_safe(m, tmp, &p->dm.self.miflist, list) { + int ifindex; + ifindex = if_nametoindex(m->ifname); if (!ifindex) { err("%s: %s vanished!\n", __func__, m->ifname); @@ -371,12 +373,13 @@ int i1905_dm_update_master_interfaces(struct i1905_private *p) int i1905_dm_refresh_self(struct i1905_private *p) { struct i1905_interface *iface = NULL, *tmp; - int ifindex; int ret; i1905_print_interfaces(p); list_for_each_entry_safe(iface, tmp, &p->dm.self.iflist, list) { + int ifindex; + ifindex = if_nametoindex(iface->ifname); if (!ifindex) { err("%s: %s vanished! Removing object....\n", __func__, iface->ifname); diff --git a/src/i1905_cmd.c b/src/i1905_cmd.c index 8ac3d8d1414ca09f6548e59b5cc159caf1a75823..f2c134da5033e478ad222a97fe097dc16c7b05b4 100644 --- a/src/i1905_cmd.c +++ b/src/i1905_cmd.c @@ -302,7 +302,8 @@ static void i1905_process_cmdline(struct i1905_private *priv, int cli, char *buf i1905_dbg(LOG_CMD, "%s: cli command: %s\n", __func__, cmdname); if (args && strlen(args)) { i1905_dbg(LOG_CMD, "%s: args: %s (len = %zu)\n", __func__, args, strlen(args)); - while (i <= strlen(args) && args[i++] == ' '); + while (i <= strlen(args) && args[i++] == ' ') + ; /* NOOP */ if (i <= strlen(args)) { /* json validate the passed args */ jmsg = json_tokener_parse(args); diff --git a/src/i1905_cmdresp.c b/src/i1905_cmdresp.c index a57680c14dd113b903de1d159225a0d2dd5bfce6..7a654b25df6a3eff2d39de382cb0edee746fd38d 100644 --- a/src/i1905_cmdresp.c +++ b/src/i1905_cmdresp.c @@ -130,12 +130,13 @@ int i1905_dump_neighbors(struct i1905_private *priv, void *out) struct i1905_selfdevice *self = &priv->dm.self; struct blob_buf *bb = (struct blob_buf *)out; struct i1905_device *nbr; - void *a, *aa; + void *a; a = blobmsg_open_array(bb, "neighbors"); list_for_each_entry(nbr, &self->topology.devlist, list) { char nbr_almacstr[18] = {0}; + void *aa; aa = blobmsg_open_table(bb, ""); hwaddr_ntoa(nbr->aladdr, nbr_almacstr); @@ -156,14 +157,15 @@ int i1905_dump_links(struct i1905_private *priv, void *out) struct blob_buf *bb = (struct blob_buf *)out; struct i1905_neighbor_interface *link = NULL; struct i1905_interface *iface; - void *a, *aa; - void *t, *tt; + void *a; a = blobmsg_open_array(bb, "interfaces"); list_for_each_entry(iface, &self->iflist, list) { char ifstr[18] = {0}; char alstr[18] = {0}; + void *aa; + void *t; t = blobmsg_open_table(bb, ""); hwaddr_ntoa(iface->aladdr, alstr); @@ -177,6 +179,7 @@ int i1905_dump_links(struct i1905_private *priv, void *out) list_for_each_entry(link, &iface->nbriflist, list) { char nalstr[18] = {0}; char nifstr[18] = {0}; + void *tt; tt = blobmsg_open_table(bb, ""); hwaddr_ntoa(link->aladdr, nalstr); @@ -326,11 +329,10 @@ int i1905_dump_info(struct i1905_private *priv, void *out) blobmsg_add_u32(bb, "num_mediainfo", iface->num_mediainfo); if (iface->mediatype == IF_MEDIA_WIFI) { - struct i1905_wifi_mediainfo *w; - if (iface->num_mediainfo> 0) { a = blobmsg_open_array(bb, "mediainfo"); for (int i = 0; i < iface->num_mediainfo; i++) { + struct i1905_wifi_mediainfo *w; void *t; w = (struct i1905_wifi_mediainfo *)iface->mediainfo + i; @@ -680,10 +682,11 @@ int i1905_dump_info(struct i1905_private *priv, void *out) if (IS_MEDIA_WIFI(rif->media)) { struct ieee80211_info *winfo; - char rif_bssidstr[18] = {0}; winfo = (struct ieee80211_info *)rif->mediainfo; if (winfo) { + char rif_bssidstr[18] = {0}; + hwaddr_ntoa(winfo->bssid, rif_bssidstr); blobmsg_add_string(bb, "bssid", rif_bssidstr); blobmsg_add_string(bb, "role", role_type2str(winfo->role)); @@ -828,7 +831,6 @@ int i1905_set_cmdu_fragment_scheme(struct i1905_private *priv, void *args, void struct blob_attr *msg = (struct blob_attr *)args; struct blob_buf *bb = (struct blob_buf *)out; struct blob_attr *tb[NUM_CMDU_FRAG_POLICY]; - char dst_macstr[18] = {0}; uint8_t dst[6] = {0}; uint8_t scheme = 0; int ret; @@ -842,6 +844,8 @@ int i1905_set_cmdu_fragment_scheme(struct i1905_private *priv, void *args, void return -EINVAL; if (tb[CMDU_FRAG_DST]) { + char dst_macstr[18] = {0}; + strncpy(dst_macstr, blobmsg_data(tb[CMDU_FRAG_DST]), sizeof(dst_macstr)-1); @@ -1091,13 +1095,10 @@ int i1905_do_cmdu_rx(struct i1905_private *priv, void *args, void *out) struct blob_buf *bb = (struct blob_buf *)out; struct i1905_interface *iface; struct cmdu_buff *rxf = NULL; - char src_macstr[18] = {0}; char ifname[16] = {0}; uint8_t src[6] = {0}; char *data_str = NULL; uint8_t *data = NULL; - uint16_t type = 0; - uint16_t mid = 0; int data_len = 0; int ret = 0; @@ -1120,6 +1121,8 @@ int i1905_do_cmdu_rx(struct i1905_private *priv, void *args, void *out) } if (tb[CMDU_RX_SRC]) { + char src_macstr[18] = {0}; + strncpy(src_macstr, blobmsg_data(tb[CMDU_RX_SRC]), sizeof(src_macstr)-1); @@ -1170,12 +1173,15 @@ int i1905_do_cmdu_rx(struct i1905_private *priv, void *args, void *out) if (tb[CMDU_RX_TYPE]) { const char *typestr = blobmsg_data(tb[CMDU_RX_TYPE]); + uint16_t type = 0; type = strtoul(typestr, NULL, 16); cmdu_set_type(rxf, type); } if (tb[CMDU_RX_MID]) { + uint16_t mid = 0; + mid = blobmsg_get_u32(tb[CMDU_RX_MID]); cmdu_set_mid(rxf, mid); } @@ -1433,7 +1439,6 @@ int i1905_do_apconfig(struct i1905_private *priv, void *args, void *out) struct blob_attr *msg = (struct blob_attr *)args; struct blob_attr *tb[NUM_I1905_APCONFIG_POLICY]; struct blob_buf *bb = (struct blob_buf *)out; - char ifname[16] = { 0 }; char action[64] = { 0 }; uint8_t band = 0xff; int ret = 0; @@ -1444,6 +1449,8 @@ int i1905_do_apconfig(struct i1905_private *priv, void *args, void *out) if (tb[I1905_APCONFIG_IFNAME]) { + char ifname[16] = { 0 }; + strncpy(ifname, blobmsg_data(tb[I1905_APCONFIG_IFNAME]), 16); ifname[15] = '\0'; } diff --git a/src/i1905_dm.c b/src/i1905_dm.c index b39df7f777156f2a335cd742031d2beed837fdb4..b68092e0adc7490982f270632b4c3d36461aa995 100644 --- a/src/i1905_dm.c +++ b/src/i1905_dm.c @@ -487,10 +487,11 @@ void i1905_free_interface_neighbors(struct i1905_interface *iface) priv = i1905_selfdevice_to_context(self); list_for_each_entry(link, &iface->nbriflist, list) { struct i1905_device *rdev = NULL; - uint8_t aladdr[6] = {0}; rdev = i1905_dm_neighbor_lookup(iface, link->aladdr); if (rdev) { + uint8_t aladdr[6] = {0}; + memcpy(aladdr, rdev->aladdr, 6); i1905_dm_neighbor_remove(rdev); i1905_send_topology_notification(priv, iface->ifname); @@ -769,10 +770,8 @@ int i1905_is_tlv_device_bridge_caps_valid(struct tlv *t) int i; - if (brcaps->num_tuples > 0 && remlen >= sizeof(struct device_bridge_tuple)) { + if (brcaps->num_tuples > 0 && remlen >= sizeof(struct device_bridge_tuple)) ptr += sizeof(struct tlv_device_bridge_caps); - tuple = (struct device_bridge_tuple *)ptr; - } for (i = 0; i < brcaps->num_tuples; i++) { if (remlen < sizeof(struct device_bridge_tuple)) @@ -853,10 +852,8 @@ int i1905_is_tlv_device_info_valid(struct tlv *t) int i; - if (devinfo->num_interface > 0 && remlen >= sizeof(struct local_interface)) { + if (devinfo->num_interface > 0 && remlen >= sizeof(struct local_interface)) ptr += sizeof(struct tlv_device_info); - tif = (struct local_interface *)ptr; - } for (i = 0; i < devinfo->num_interface; i++) { if (remlen < sizeof(struct local_interface)) diff --git a/src/i1905_netlink.c b/src/i1905_netlink.c index c206ced7640e042da976f328bc120c8694e9f158..a57148b1521805154d136ffef9912419f3006498 100644 --- a/src/i1905_netlink.c +++ b/src/i1905_netlink.c @@ -852,7 +852,7 @@ static int i1905_enum_interfaces(struct list_head *head, int *num) if (ret) { nl_socket_free(sk); ret = -errno; - return -1; + return ret; } nl_cache_foreach(cache, enum_interfaces_cb, &arg); diff --git a/src/i1905_wsc.c b/src/i1905_wsc.c index ba60749f92a9a987473f48f2f1c109c240b34775..af0ce61888d99e5ba38e14cf3d92e62ef0776beb 100644 --- a/src/i1905_wsc.c +++ b/src/i1905_wsc.c @@ -82,7 +82,6 @@ int wps_kdf(const uint8_t *key, const uint8_t *label_prefix, int i, iter; uint8_t hash[SHA256_MAC_LEN], *opos; size_t left; - int ret = 0; buf_put_be32(key_bits, res_len * 8); @@ -100,6 +99,8 @@ int wps_kdf(const uint8_t *key, const uint8_t *label_prefix, left = res_len; for (i = 1; i <= iter; i++) { + int ret = 0; + buf_put_be32(i_buf, i); ret = PLATFORM_HMAC_SHA256(key, SHA256_MAC_LEN, 4, addr, len, hash); if (ret) diff --git a/src/main.c b/src/main.c index 8aa7a95d5c4f80d4b2871d5110c24d9a850ede66..0243640fcab79c84b18db8ce213f1c98a1cae723 100644 --- a/src/main.c +++ b/src/main.c @@ -60,7 +60,7 @@ int main(int argc, char **argv) .lo = true, .registrar = false, }; - static struct option lopts[] = { + struct option lopts[] = { { "version", 0, 0, 0 }, { "alid", 1, 0, 0 }, { "config", 1, 0, 0 }, @@ -69,9 +69,10 @@ int main(int argc, char **argv) { 0, 0, 0, 0 }, }; int lidx = 0; - int ch; for (;;) { + int ch; + ch = getopt_long(argc, argv, "vhdDRp:s:O:d:c:o:fl", lopts, &lidx); if (ch == -1) break; diff --git a/src/neigh.c b/src/neigh.c index f078710131933068faa0e16661a7b060298194a6..50e9fa3828d3008e47b48925de0b4f06ddc94628 100644 --- a/src/neigh.c +++ b/src/neigh.c @@ -143,7 +143,7 @@ static void neigh_entry_ageout(struct neigh_queue *st, struct hlist_head *head, getcurrtime(&now); hlist_for_each_entry_safe(e, tmp, head, hlist) { - if (!timercmp(&e->ageing_tmo, &now, >)) { + if (!timercmp(&e->ageing_tmo, &now, >)) { /* cppcheck-suppress syntaxError */ /* schedule a one-time probing for this entry after 1s. * If it does not become reachable within 15s, then delete it. */ diff --git a/src/util.c b/src/util.c index 1b4dbdbf88f66a75311f4937eb42e2227d963106..ff17c587b323ffcb0177bc5350d1545ac1867cb0 100644 --- a/src/util.c +++ b/src/util.c @@ -43,7 +43,7 @@ int uuid_strtob(char *uuidstr, uint8_t *uuid) { uint8_t elen[] = {8, 4, 4, 4, 12}; - char *ptr, *pos, *end; + char *ptr, *end; uint8_t *ret; int idx = 0; int i = 0; @@ -57,6 +57,7 @@ int uuid_strtob(char *uuidstr, uint8_t *uuid) while (ptr < end) { char tmp[32] = {0}; + char *pos; pos = ptr; ptr = strchr(pos, '-');