diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1bc4677ff94056c814f49a8b4eba1c068cd00dd9..c837ce2e2496a1360d3cc3aa15a578007222cd3b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,5 +8,5 @@ stages: variables: DEBUG: 'TRUE' SOURCE_FOLDER: "libdsl libethernet libwifi" - RUN_CPPCHECK: "cppcheck --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=unusedStructMember --suppress=unusedLabel --suppress=variableScope --suppress=unreadVariable --suppress=knownConditionTrueFalse --suppress=constParameter --suppress=objectIndex ." + RUN_CPPCHECK: "cppcheck --enable=all --error-exitcode=1 --suppress=unusedFunction --suppress=unusedStructMember --suppress=unusedLabel --suppress=variableScope --suppress=unreadVariable --suppress=knownConditionTrueFalse --suppress=constParameter --suppress=constVariable --suppress=objectIndex ." COMMON_IMAGE: iopsys/code-analysis:0.27 diff --git a/libdsl/common.h b/libdsl/common.h index 36de08516025bdfb38b084721d3dc1df65776a1d..00427a425b1052bddfc0c013bfa21b3b5a3aea42 100644 --- a/libdsl/common.h +++ b/libdsl/common.h @@ -28,7 +28,7 @@ extern "C" { #include <stdio.h> -#define LIBDSL_LOG(log_level, format...) fprintf(stderr, ##format) +#define LIBDSL_LOG(log_level, format...) fprintf(stderr, ##format) /* Flawfinder: ignore */ /** enum dsl_status - operational status of a line, channel or an interface */ enum itf_status { diff --git a/libdsl/test_stub/stub.c b/libdsl/test_stub/stub.c index 6de15bab6ef729ea9625e90bd9b81b0e1d2b8901..ce0672933d8d66d2726d37f19f9837f9bd9aa96a 100644 --- a/libdsl/test_stub/stub.c +++ b/libdsl/test_stub/stub.c @@ -84,17 +84,17 @@ static void log_test(const char *fmt, ...) time_t now = time(NULL); struct tm *tm_now = localtime(&now); - const char *tm_fmt = "[%d-%02d-%02d %02d:%02d:%02d] "; + const char *tm_fmt = "[%4d-%02d-%02d %02d:%02d:%02d] "; va_start(args, fmt); - fprintf(fp, tm_fmt, + fprintf(fp, tm_fmt, /* Flawfinder: ignore */ tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec); - vfprintf(fp, fmt, args); + vfprintf(fp, fmt, args); /* Flawfinder: ignore */ va_end(args); fflush(fp); @@ -881,7 +881,7 @@ int dsl_configure(int line_num, struct dsl_config_params *cfg_params) strncpy(str_vdsl2_profiles, str, sizeof(str_vdsl2_profiles) - 1); total_len += len; } else if (total_len + len + 1 < sizeof(str_vdsl2_profiles)) { - sprintf(str_vdsl2_profiles + total_len, ",%s", str); + snprintf(str_vdsl2_profiles + total_len, sizeof(str_vdsl2_profiles), ",%s", str); total_len += (len + 1); } } @@ -898,7 +898,7 @@ int dsl_configure(int line_num, struct dsl_config_params *cfg_params) strncpy(str_fast_profiles, str, sizeof(str_fast_profiles) - 1); total_len += len; } else if (total_len + len + 1 < sizeof(str_fast_profiles)) { - sprintf(str_fast_profiles + total_len, ",%s", str); + snprintf(str_fast_profiles + total_len, sizeof(str_fast_profiles), ",%s", str); total_len += (len + 1); } } diff --git a/libeasy/debug.c b/libeasy/debug.c index a17ef80a309e6ccb51ce5b4c0b770d00ff3b1db2..37b90978317fcf45aa95ea7f6c5d6f1ba918e49d 100644 --- a/libeasy/debug.c +++ b/libeasy/debug.c @@ -34,6 +34,6 @@ void LIBEASY_API log_stderr(int level, const char *fmt, ...) return; va_start(args, fmt); - vfprintf(stderr, fmt, args); + vfprintf(stderr, fmt, args); /* Flawfinder: ignore */ va_end(args); } diff --git a/libeasy/utils.c b/libeasy/utils.c index 71135d78b24a6f2a4a6e78583a3a25f17229f3ec..bb92e3654977f5ce2b8c187a8a7ea7aa2f4cc72e 100644 --- a/libeasy/utils.c +++ b/libeasy/utils.c @@ -325,7 +325,7 @@ int vsnsystemf(char *output, size_t output_size, const char *format, va_list ap) FILE *stream; char *line; - stream = popen(cmdline, "r"); + stream = popen(cmdline, "r"); /* Flawfinder: ignore */ if (!stream) goto out_stream; diff --git a/libwifi/chlist.c b/libwifi/chlist.c index 1dc8c9469ade65c6ebc44db12e854596e2a2c675..5daa09cb820b2a2d6560ebf128b60beabed35012 100644 --- a/libwifi/chlist.c +++ b/libwifi/chlist.c @@ -1590,8 +1590,9 @@ static int radio_get_supported_opclass(const char *name, int *num_opclass, return ret; rd_opclass = calloc(max, sizeof(struct wifi_opclass)); + //cppcheck-suppress memleak if (WARN_ON(!rd_opclass)) - return -1; //cppcheck-suppress memleak + return -1; rd_num_opclass = max; ret = _radio_get_supported_opclass(name, &rd_num_opclass, rd_opclass); diff --git a/libwifi/modules/broadcom/bcm.c b/libwifi/modules/broadcom/bcm.c index df75925f1da87b131ab7aca1aa611e226229ad34..266638c8dd727bbad9e937fc66630cbdc0305a34 100644 --- a/libwifi/modules/broadcom/bcm.c +++ b/libwifi/modules/broadcom/bcm.c @@ -77,7 +77,7 @@ wl_ether_etoa(const struct wl_ether_addr *n) static int wl_ioctl(const char *ifname, int cmd, void *buf, int len) { struct ifreq ifr; - wl_ioctl_t wioc; + wl_ioctl_t wioc = {0}; int s; wioc.cmd = cmd; @@ -115,7 +115,7 @@ static int wl_iovar(const char *ifname, int set, char *iovar, if (buflen < total_len) return -1; - strcpy(bufptr, iovar); + snprintf(bufptr, buflen, "%s", iovar); memcpy((uint8_t *)bufptr + len, param, paramlen); if (set) @@ -1729,7 +1729,7 @@ static int bcm_disconnect_sta(const char *ifname, uint8_t *sta, uint16_t reason) { char cmd[256] = { 0 }; - snprintf(cmd, 255, "wlctl -i %s deauthenticate " MACFMT, + snprintf(cmd, 255, "wlctl -i %16s deauthenticate " MACFMT, /* Flawfinder: ignore */ ifname, MAC2STR(sta)); if (reason > 0) diff --git a/libwifi/modules/broadcom/brcm.c b/libwifi/modules/broadcom/brcm.c index b2c2367902f7a939a19c38ae1b824c20ec70d4fd..f1bfd76bc7ff70c4864a6eae4c679e0c3be6e1e2 100644 --- a/libwifi/modules/broadcom/brcm.c +++ b/libwifi/modules/broadcom/brcm.c @@ -961,8 +961,8 @@ static int register_event(const char *ifname, struct event_struct *ev, libwifi_dbg("%s %s called family %s group %s\n", ifname, __func__, ev->family, ev->group); ctx = calloc(1, sizeof(struct event_ctx)); - if (WARN_ON(!ctx)) - return -1; //cppcheck-suppress memleak + if (WARN_ON(!ctx)) //cppcheck-suppress memleak + return -1; if (!strcmp(ev->family, "bcmwl")) ret = bcmwl_register_event(ifname, ev, &handle); diff --git a/libwifi/modules/broadcom/wlctrl.c b/libwifi/modules/broadcom/wlctrl.c index 51f4422de38ce193a0650407d7777370832c6bee..40c9e7e1cce6c67fa8e969d1dd9eaf6b26da81f7 100644 --- a/libwifi/modules/broadcom/wlctrl.c +++ b/libwifi/modules/broadcom/wlctrl.c @@ -139,7 +139,8 @@ static int wl_ioctl(struct wl_arg *arg) len = strlen(arg->iovar) + 1; if (WARN_ON(len > sizeof(buf))) goto exit; - strcpy(buf, arg->iovar); + + snprintf(buf, WLC_IOCTL_MAXLEN, "%s", arg->iovar); if (arg->cmd == WLC_SET_VAR) wioc.len = len + arg->paramlen; } @@ -285,7 +286,8 @@ static int wl_vendor(struct wl_arg *arg) len = strlen(arg->iovar) + 1; if (WARN_ON((len + sizeof(*req)) > sizeof(in))) return -1; - strcpy((char *)&in[req->offset], arg->iovar); + + snprintf(in + req->offset, sizeof(in), "%s", arg->iovar); inlen += len; } @@ -453,7 +455,7 @@ static int wl_ioctl_get_version(const char *name, char *version, size_t *len) } if (*len > strlen(buf)) { *len = strlen(buf); - strcpy(version, buf); + strncpy(version, buf, *len); } else { *len -= 1; strncpy(version, buf, *len); @@ -3319,7 +3321,7 @@ static int bcmwl_event_handle(const char *ifname, bcm_event_t *event, int size, btostr((uint8_t *) (event + 1), datalen, raw); /* Fill macaddr */ - snprintf(macaddr, sizeof(macaddr), MACSTR, MAC2STR(event->event.addr.octet)); + snprintf(macaddr, sizeof(macaddr), MACSTR, MAC2STR(event->event.addr.octet)); /* Flawfinder: ignore */ switch (event_type) { @@ -3547,7 +3549,7 @@ static int bcmwl_event_handle_if(const char *ifname, bcm_event_t *event, int siz break; } - snprintf(macaddr, sizeof(macaddr), MACSTR, MAC2STR(ev->peer_addr.octet)); + snprintf(macaddr, sizeof(macaddr), MACSTR, MAC2STR(ev->peer_addr.octet)); /* Flawfinder: ignore */ /* TODO check if this is correct */ snprintf(name, sizeof(name), "wds%u.0.%u", ev->bssidx, ev->ifidx); diff --git a/libwifi/modules/intel/intel.c b/libwifi/modules/intel/intel.c index 76d011db70370e1dd61d4df6f19cfd6937c80d87..01b3346fab97b8a173437613ab97a954cd5b7900 100644 --- a/libwifi/modules/intel/intel.c +++ b/libwifi/modules/intel/intel.c @@ -727,7 +727,7 @@ static int intel_disconnect_sta(const char *ifname, uint8_t *sta, uint16_t reaso { char buf[256] = {0}; - snprintf(buf, 255, "hostapd_cli -i %s deauthenticate %s " MACFMT, + snprintf(buf, 255, "hostapd_cli -i %16s deauthenticate %16s " MACFMT, /* Flawfinder: ignore */ ifname, ifname, MAC2STR(sta)); if (reason > 0) snprintf(buf + strlen(buf), 255, " reason=%d", reason); @@ -747,7 +747,7 @@ int intel_monitor_sta(const char *ifname, uint8_t *sta, struct wifi_monsta_confi if (!sta) return -1; - sprintf(sta_macstr, MACFMT, MAC2STR(sta)); + snprintf(sta_macstr, sizeof(sta_macstr), MACFMT, MAC2STR(sta)); /* Flawfinder: ignore */ /* Setup sta monitoring. Monitored data is * reported through vendor events. */ @@ -1007,7 +1007,7 @@ static int intel_add_neighbor(const char *ifname, struct nbr nbr) if (ret) return ret; - sprintf(nbr_bssidstr, MACFMT, MAC2STR(nbr.bssid)); + snprintf(nbr_bssidstr, sizeof(nbr_bssidstr), MACFMT, MAC2STR(nbr.bssid)); /* Flawfinder: ignore */ snprintf(buf, sizeof(buf), "hostapd_cli -i %s set_neighbor_per_vap %s %s ", ifname, ifname, nbr_bssidstr); @@ -1039,7 +1039,7 @@ static int intel_del_neighbor(const char *ifname, unsigned char *bssid) if (ret) return ret; - sprintf(nbr_bssidstr, MACFMT, MAC2STR(bssid)); + snprintf(nbr_bssidstr, sizeof(nbr_bssidstr), MACFMT, MAC2STR(bssid)); /* Flawfinder: ignore */ snprintf(buf, sizeof(buf), "hostapd_cli -i %s remove_neighbor_per_vap %s %s ", ifname, ifname, nbr_bssidstr); @@ -1074,7 +1074,7 @@ static int intel_req_beacon_report(const char *ifname, uint8_t *sta, if (ret) return ret; - sprintf(da_macstr, MACFMT, MAC2STR(sta)); + snprintf(da_macstr, sizeof(da_macstr), MACFMT, MAC2STR(sta)); /* Flawfinder: ignore */ channel = 255; random_int = 1000; dur = 50; @@ -1118,7 +1118,7 @@ int intel_req_bss_transition(const char *ifname, unsigned char *sta, if (!pnbr) return -1; - sprintf(macstr, MACFMT, MAC2STR(sta)); + snprintf(macstr, sizeof(macstr), MACFMT, MAC2STR(sta)); /* Flawfinder: ignore */ snprintf(buf, sizeof(buf), "hostapd_cli -i %s bss_tm_req %s ", ifname, macstr); diff --git a/libwifi/modules/nlwifi/nlwifi.c b/libwifi/modules/nlwifi/nlwifi.c index bf6939a2adfafa5fedb824eb7c3224b8392e10a5..44701f8e6ca5b500d22f769930ca93a17080f145 100644 --- a/libwifi/modules/nlwifi/nlwifi.c +++ b/libwifi/modules/nlwifi/nlwifi.c @@ -2433,8 +2433,8 @@ int nlwifi_channels_info(const char *name, struct chan_entry *channel, int *num) /* Sync channels with survey data */ entry_num = *num; entry = calloc(entry_num, sizeof(*entry)); - if (WARN_ON(!entry)) - return -1; //cppcheck-suppress memleak + if (WARN_ON(!entry)) //cppcheck-suppress memleak + return -1; if (WARN_ON(nlwifi_surveys_get(name, entry, &entry_num))) { free(entry); diff --git a/libwifi/modules/test/test.c b/libwifi/modules/test/test.c index fb286ff1208f21794c83ed50feb30376479dc8eb..63905849a5e7f5ccc13388e0b7e31d6c227ca673 100644 --- a/libwifi/modules/test/test.c +++ b/libwifi/modules/test/test.c @@ -55,17 +55,17 @@ void log_test(const char *fmt, ...) time_t now = time(NULL); struct tm *tm_now = localtime(&now); - const char *tm_fmt = "[%d-%02d-%02d %02d:%02d:%02d] "; + const char *tm_fmt = "[%4d-%02d-%02d %02d:%02d:%02d] "; va_start(args, fmt); - fprintf(fp, tm_fmt, + fprintf(fp, tm_fmt, /* Flawfinder: ignore */ tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec); - vfprintf(fp, fmt, args); + vfprintf(fp, fmt, args); /* Flawfinder: ignore */ va_end(args); fflush(fp); @@ -469,7 +469,7 @@ int test_req_bss_transition(const char *ifname, unsigned char *sta, snprintf(macstr, sizeof(macstr), "\"" MACFMT "\"", bsss[offset + 0], bsss[offset + 1], bsss[offset + 2], bsss[offset + 3], bsss[offset + 4], bsss[offset + 5]); - strncat(bsss_str, macstr, sizeof(macstr)); + snprintf(bsss_str + strlen(bsss_str), sizeof(bsss_str), "%s", macstr); strncat(bsss_str, ",", 1); } diff --git a/libwifi/modules/wpactrl/wpactrl_util.c b/libwifi/modules/wpactrl/wpactrl_util.c index fe7e8ad4ed8d68c99755e517f534198e63b3fa60..5d2e709b05b8286e34ddfc0a822b8dc108c2fd39 100644 --- a/libwifi/modules/wpactrl/wpactrl_util.c +++ b/libwifi/modules/wpactrl/wpactrl_util.c @@ -87,9 +87,9 @@ static int get_param(const char *buf, const char *param, char *value, size_t val loc = strstr(line, param); if (!loc || loc != line) continue; - snprintf(search_str, sizeof(search_str), "%s=%%%zu[^\n]s", param, value_max); + snprintf(search_str, sizeof(search_str), "%64s=%%%zu[^\n]s", param, value_max); - if (WARN_ON(sscanf(line, search_str, value) != 1)) { + if (WARN_ON(sscanf(line, search_str, value) != 1)) { /* Flawfinder: ignore */ free(origin_p); return ret; } @@ -149,7 +149,7 @@ struct wpa_ctrl { static struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) { struct wpa_ctrl *ctrl; - static int counter = 0; + static int counter = 0; /* FIXME-CR */ int ret; int tries = 0; int flags; @@ -172,7 +172,7 @@ static struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path) ctrl->local.sun_family = AF_UNIX; counter++; try_again: - ret = snprintf(ctrl->local.sun_path, + ret = snprintf(ctrl->local.sun_path, /* Flawfinder: ignore */ sizeof(ctrl->local.sun_path), CONFIG_CTRL_IFACE_CLIENT_DIR "/" CONFIG_CTRL_IFACE_CLIENT_PREFIX "%d-%d", @@ -842,7 +842,7 @@ int hostapd_cli_iface_get_sta_info(const char *ifname, uint8_t *addr, struct wif libwifi_dbg("%s %s \n", ifname, __func__); - snprintf(cmd, sizeof(cmd), "sta " MACSTR, MAC2STR(addr)); + snprintf(cmd, sizeof(cmd), "sta " MACSTR, MAC2STR(addr)); /* Flawfinder: ignore */ if (WARN_ON(hostapd_cli_get(ifname, cmd, buf, sizeof(buf)))) return -1; @@ -918,7 +918,7 @@ int hostapd_cli_iface_add_neighbor(const char *ifname, struct nbr *nbr, size_t n if (hostapd_cli_get_ssid(ifname, ssid, sizeof(ssid))) return -1; - snprintf(bssid, sizeof(bssid), MACSTR, MAC2STR(nbr->bssid)); + snprintf(bssid, sizeof(bssid), MACSTR, MAC2STR(nbr->bssid)); /* Flawfinder: ignore */ btostr((uint8_t *) nbr, nbr_size, nr); snprintf(cmd, sizeof(cmd), "set_neighbor " MACSTR " ssid=\\\"%s\\\" nr=%s", @@ -1093,8 +1093,8 @@ int hostapd_cli_iface_get_neighbor_list(const char *ifname, struct nbr *nbrs, in /* make sure we have place for show_neighbor output */ buf = calloc(*nr, 256); - if (WARN_ON(!buf)) - return -1; //cppcheck-suppress memleak + if (WARN_ON(!buf)) //cppcheck-suppress memleak + return -1; ret = hostapd_cli_get(ifname, "show_neighbor", buf, *nr * 256); if (WARN_ON(ret)) { @@ -1246,7 +1246,7 @@ int hostapd_cli_disconnect_sta(const char *ifname, uint8_t *sta, uint16_t reason { char cmd[1024] = { 0 }; - snprintf(cmd, sizeof(cmd), "deauthenticate " MACSTR, MAC2STR(sta)); + snprintf(cmd, sizeof(cmd), "deauthenticate " MACSTR, MAC2STR(sta)); /* Flawfinder: ignore */ if (reason > 0) snprintf(cmd + strlen(cmd), sizeof(cmd), " reason=%d", reason); @@ -1277,8 +1277,8 @@ int hostapd_cli_iface_req_bss_transition(const char *ifname, unsigned char *sta, int ret; nbrs = calloc(nr, sizeof(*nbr)); - if (WARN_ON(!nbrs)) - return -1; //cppcheck-suppress memleak + if (WARN_ON(!nbrs)) //cppcheck-suppress memleak + return -1; if (wifi_get_neighbor_list(ifname, nbrs, &nr)) { free(nbrs); @@ -1294,8 +1294,8 @@ int hostapd_cli_iface_req_bss_transition(const char *ifname, unsigned char *sta, buf = malloc(buflen); if (WARN_ON(!buf)) { - free(nbrs); - return -1; //cppcheck-suppress memleak + free(nbrs); //cppcheck-suppress memleak + return -1; } len = 0; @@ -1401,7 +1401,7 @@ int hostapd_cli_is_wds_sta(const char *ifname, uint8_t *sta_mac, char *ifname_wd char buf[2048] = { 0 }; int is_wds = 0; - snprintf(cmd, sizeof(cmd), "sta " MACSTR, MAC2STR(sta_mac)); + snprintf(cmd, sizeof(cmd), "sta " MACSTR, MAC2STR(sta_mac)); /* Flawfinder: ignore */ if (hostapd_cli_get(ifname, cmd, buf, sizeof(buf))) return 0; @@ -1496,7 +1496,7 @@ int hostapd_cli_probe_sta(const char *ifname, uint8_t *sta) if (WARN_ON(!sta)) return -1; - snprintf(cmd, sizeof(cmd), "poll_sta " MACSTR, MAC2STR(sta)); + snprintf(cmd, sizeof(cmd), "poll_sta " MACSTR, MAC2STR(sta)); /* Flawfinder: ignore */ return hostapd_cli_set(ifname, cmd, true); } @@ -1515,7 +1515,7 @@ int hostapd_ubus_iface_get_monitor_sta(const char *ifname, uint8_t *sta, struct snprintf(format, sizeof(format), "{\"sta\":{\"macaddr\":\"" MACSTR "\",\"rssi\":%%d,\"age\":%%d}}", MAC2STR(sta)); /* Format: {"sta":{"macaddr":"30:10:b3:6d:8d:ba","rssi":-29,"age":782}} */ - if (WARN_ON(sscanf(buf, format, &rssi, &age) != 2)) + if (WARN_ON(sscanf(buf, format, &rssi, &age) != 2)) /* Flawfinder: ignore */ return -1; memcpy(mon->macaddr, sta, 6);