Skip to content

Commit

Permalink
Fix static analysis issues with strncpy()
Browse files Browse the repository at this point in the history
Replace strncpy() with strlcpy() to silence static analysis warnings
about use of deprecated functions.

Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
  • Loading branch information
Peng Xu authored and jmalinen committed May 14, 2017
1 parent 769731a commit b8fc5cc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 57 deletions.
16 changes: 8 additions & 8 deletions ap.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static int get_hwaddr(const char *ifname, unsigned char *hwaddr)
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl");
close(s);
Expand Down Expand Up @@ -2665,7 +2665,7 @@ static int owrt_ap_config_vap_anqp(struct sigma_dut *dut)

memset(&ifr, 0, sizeof(ifr));
ifname = "ath0";
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl");
close(s);
Expand All @@ -2675,7 +2675,7 @@ static int owrt_ap_config_vap_anqp(struct sigma_dut *dut)

memset(&ifr, 0, sizeof(ifr));
ifname = "ath01";
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl");
close(s);
Expand Down Expand Up @@ -6558,7 +6558,7 @@ static int ap_inject_frame(struct sigma_dut *dut, struct sigma_conn *conn,
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl");
close(s);
Expand Down Expand Up @@ -7371,7 +7371,7 @@ static int cmd_ap_get_mac_address(struct sigma_dut *dut,
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl");
close(s);
Expand Down Expand Up @@ -7656,7 +7656,7 @@ static int cmd_ap_set_hs2(struct sigma_dut *dut, struct sigma_conn *conn,
"errorCode,PLMN_MCC too long");
return 0;
}
strncpy(mcc, val, sizeof(mcc));
strlcpy(mcc, val, sizeof(mcc));
start = mcc;
while ((end = strchr(start, ';'))) {
/* process all except the last */
Expand Down Expand Up @@ -7695,7 +7695,7 @@ static int cmd_ap_set_hs2(struct sigma_dut *dut, struct sigma_conn *conn,
"errorCode,PLMN_MNC too long");
return 0;
}
strncpy(mnc, val, sizeof(mnc));
strlcpy(mnc, val, sizeof(mnc));
start = mnc;
while ((end = strchr(start, ';'))) {
*end = '\0';
Expand Down Expand Up @@ -7814,7 +7814,7 @@ static int cmd_ap_set_hs2(struct sigma_dut *dut, struct sigma_conn *conn,
val = get_param(cmd, "OSU_SSID");
if (val) {
if (strlen(val) > 0 && strlen(val) <= 32) {
strncpy(dut->ap_osu_ssid, val,
strlcpy(dut->ap_osu_ssid, val,
sizeof(dut->ap_osu_ssid));
sigma_dut_print(dut, DUT_MSG_INFO,
"ap_osu_ssid %s",
Expand Down
5 changes: 2 additions & 3 deletions miracast.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ static int get_peer_ip_p2p_client(struct sigma_dut *dut, char *ipAddr,
sigma_dut_print(dut, DUT_MSG_INFO, "Peer IP: %u", ipaddress);
if (strlen(ipaddr(serveraddr)) > 8) {
/* connected */
strncpy(ipAddr, ipaddr(serveraddr), 16);
strlcpy(ipAddr, ipaddr(serveraddr), 16);
break;
}
sleep(1);
Expand Down Expand Up @@ -627,8 +627,7 @@ static void * auto_go_thread_entry(void *ptr)
}

peer++;
strncpy(macaddress, peer, 17 /* Size of MAC */);
macaddress[17] = '\0';
strlcpy(macaddress, peer, sizeof(macaddress));
if (get_peer_ip_p2p_go(dut, peer_ip_address, macaddress, 30) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR, "Could not get peer IP");
goto THR_EXIT;
Expand Down
14 changes: 7 additions & 7 deletions p2p.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ static int p2p_group_add(struct sigma_dut *dut, const char *ifname,
if (grp == NULL)
return -1;
memset(grp, 0, sizeof(*grp));
strncpy(grp->ifname, ifname, IFNAMSIZ);
strlcpy(grp->ifname, ifname, IFNAMSIZ);
grp->go = go;
strncpy(grp->grpid, grpid, P2P_GRP_ID_LEN);
strncpy(grp->ssid, ssid, sizeof(grp->ssid));
strlcpy(grp->grpid, grpid, P2P_GRP_ID_LEN);
strlcpy(grp->ssid, ssid, sizeof(grp->ssid));

grp->next = dut->groups;
dut->groups = grp;
Expand Down Expand Up @@ -378,7 +378,7 @@ static struct wfa_cs_p2p_group * p2p_group_get(struct sigma_dut *dut,
return NULL;
memcpy(go_dev_addr, grpid, pos - grpid);
go_dev_addr[pos - grpid] = '\0';
strncpy(ssid, pos + 1, sizeof(ssid));
strlcpy(ssid, pos + 1, sizeof(ssid));
ssid[sizeof(ssid) - 1] = '\0';
printf("Trying to find suitable interface for group: go_dev_addr='%s' "
"grpid='%s'\n", go_dev_addr, grpid);
Expand Down Expand Up @@ -1637,7 +1637,7 @@ static int cmd_sta_wps_read_pin(struct sigma_dut *dut, struct sigma_conn *conn,
}
}

strncpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
strlcpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
dut->wps_method = WFA_CS_WPS_PIN_DISPLAY;
done:
snprintf(resp, sizeof(resp), "PIN,%s", pin);
Expand Down Expand Up @@ -1669,7 +1669,7 @@ static int cmd_sta_wps_read_label(struct sigma_dut *dut,
}
}

strncpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
strlcpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
dut->wps_method = WFA_CS_WPS_PIN_LABEL;
snprintf(resp, sizeof(resp), "LABEL,%s", pin);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
Expand Down Expand Up @@ -1702,7 +1702,7 @@ static int cmd_sta_wps_enter_pin(struct sigma_dut *dut,
}
}

strncpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
strlcpy(dut->wps_pin, pin, sizeof(dut->wps_pin));
dut->wps_pin[sizeof(dut->wps_pin) - 1] = '\0';
dut->wps_method = WFA_CS_WPS_PIN_KEYPAD;

Expand Down
52 changes: 20 additions & 32 deletions sta.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,21 +483,21 @@ int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
struct sockaddr_in saddr;

memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
"%s IP address: %s",
ifname, strerror(errno));
} else {
memcpy(&saddr, &ifr.ifr_addr,
sizeof(struct sockaddr_in));
strncpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
}

if (ioctl(s, SIOCGIFNETMASK, &ifr) == 0) {
memcpy(&saddr, &ifr.ifr_addr,
sizeof(struct sockaddr_in));
strncpy(mask, inet_ntoa(saddr.sin_addr), sizeof(mask));
strlcpy(mask, inet_ntoa(saddr.sin_addr), sizeof(mask));
}
close(s);
}
Expand All @@ -517,21 +517,14 @@ int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
}

snprintf(tmp, sizeof(tmp), "dhcp.%s.dns1", ifname);
if (property_get(tmp, prop, NULL) != 0) {
strncpy(dns, prop, sizeof(dns));
dns[sizeof(dns) - 1] = '\0';
} else {
if (property_get("net.dns1", prop, NULL) != 0) {
strncpy(dns, prop, sizeof(dns));
dns[sizeof(dns) - 1] = '\0';
}
}
if (property_get(tmp, prop, NULL) != 0)
strlcpy(dns, prop, sizeof(dns));
else if (property_get("net.dns1", prop, NULL) != 0)
strlcpy(dns, prop, sizeof(dns));

snprintf(tmp, sizeof(tmp), "dhcp.%s.dns2", ifname);
if (property_get(tmp, prop, NULL) != 0) {
strncpy(sec_dns, prop, sizeof(sec_dns));
sec_dns[sizeof(sec_dns) - 1] = '\0';
}
if (property_get(tmp, prop, NULL) != 0)
strlcpy(sec_dns, prop, sizeof(sec_dns));
#else /* ANDROID */
#ifdef __linux__
if (get_driver_type() == DRIVER_OPENWRT)
Expand Down Expand Up @@ -575,13 +568,10 @@ int get_ip_config(struct sigma_dut *dut, const char *ifname, char *buf,
}
pos2++;
}
if (!dns[0]) {
strncpy(dns, pos, sizeof(dns));
dns[sizeof(dns) - 1] = '\0';
} else if (!sec_dns[0]) {
strncpy(sec_dns, pos, sizeof(sec_dns));
sec_dns[sizeof(sec_dns) - 1] = '\0';
}
if (!dns[0])
strlcpy(dns, pos, sizeof(dns));
else if (!sec_dns[0])
strlcpy(sec_dns, pos, sizeof(sec_dns));
}
fclose(f);
}
Expand Down Expand Up @@ -1193,7 +1183,7 @@ static int cmd_sta_get_bssid(struct sigma_dut *dut, struct sigma_conn *conn,

if (get_wpa_status(get_station_ifname(), "bssid", bssid, sizeof(bssid))
< 0)
strncpy(bssid, "00:00:00:00:00:00", sizeof(bssid));
strlcpy(bssid, "00:00:00:00:00:00", sizeof(bssid));

snprintf(resp, sizeof(resp), "bssid,%s", bssid);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
Expand Down Expand Up @@ -2631,7 +2621,7 @@ static void ath_sta_set_noack(struct sigma_dut *dut, const char *intf,
char buf[100];
char *saveptr;

strncpy(token, val, sizeof(token));
strlcpy(token, val, sizeof(token));
token[sizeof(token) - 1] = '\0';
result = strtok_r(token, ":", &saveptr);
while (result) {
Expand Down Expand Up @@ -4077,7 +4067,7 @@ static int sta_set_60g_pcp(struct sigma_dut *dut, struct sigma_conn *conn,
return -1;
}

strncpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
strlcpy(dut->ap_ssid, val, sizeof(dut->ap_ssid));
}

val = get_param(cmd, "CHANNEL");
Expand Down Expand Up @@ -4960,8 +4950,7 @@ static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
int value, config_val = 0;
char *saveptr;

strncpy(token, val, sizeof(token));
token[sizeof(token) - 1] = '\0';
strlcpy(token, val, sizeof(token));
result = strtok_r(token, delim, &saveptr);

/* Extract the NSS information */
Expand Down Expand Up @@ -5047,8 +5036,7 @@ static int cmd_sta_set_wireless_vht(struct sigma_dut *dut,
unsigned int vht_mcsmap = 0;
char *saveptr;

strncpy(token, val, sizeof(token));
token[sizeof(token) - 1] = '\0';
strlcpy(token, val, sizeof(token));
result = strtok_r(token, ";", &saveptr);
if (!result) {
sigma_dut_print(dut, DUT_MSG_ERROR,
Expand Down Expand Up @@ -6164,7 +6152,7 @@ static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
struct sockaddr_in saddr;

memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "Failed to get "
"%s IP address: %s",
Expand All @@ -6174,7 +6162,7 @@ static int cmd_sta_send_frame_hs2_arpannounce(struct sigma_dut *dut,
} else {
memcpy(&saddr, &ifr.ifr_addr,
sizeof(struct sockaddr_in));
strncpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
strlcpy(ip, inet_ntoa(saddr.sin_addr), sizeof(ip));
}
close(s);

Expand Down
10 changes: 4 additions & 6 deletions traffic_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ static int cmd_traffic_agent_config(struct sigma_dut *dut,

val = get_param(cmd, "tagName");
if (val) {
strncpy(s->test_name, val, sizeof(s->test_name));
s->test_name[sizeof(s->test_name) - 1] = '\0';
strlcpy(s->test_name, val, sizeof(s->test_name));
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Traffic agent: U-APSD console tagname %s",
s->test_name);
Expand Down Expand Up @@ -469,7 +468,7 @@ static void send_file(struct sigma_stream *s)
if (pkt == NULL)
return;
memset(pkt, 1, s->payload_size);
strncpy(pkt, "1345678", s->payload_size);
strlcpy(pkt, "1345678", s->payload_size);

if (s->frame_rate == 0 && s->no_timestamps) {
send_file_fast(s, pkt);
Expand Down Expand Up @@ -577,7 +576,7 @@ static void send_transaction(struct sigma_stream *s)
return;
}
memset(pkt, 1, s->payload_size);
strncpy(pkt, "1345678", s->payload_size);
strlcpy(pkt, "1345678", s->payload_size);

gettimeofday(&stop, NULL);
stop.tv_sec += s->duration;
Expand Down Expand Up @@ -1193,9 +1192,8 @@ static int cmd_traffic_agent_receive_start(struct sigma_dut *dut,
*/
s->dut = dut;
val = get_param(cmd, "Interface");
strncpy(s->ifname, (val ? val : get_station_ifname()),
strlcpy(s->ifname, (val ? val : get_station_ifname()),
sizeof(s->ifname));
s->ifname[sizeof(s->ifname) - 1] = '\0';

sigma_dut_print(dut, DUT_MSG_DEBUG, "Traffic agent: start "
"receive for stream %d", streams[i]);
Expand Down
2 changes: 1 addition & 1 deletion wlantest.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static int open_wlantest(void)

memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
strlcpy(addr.sun_path + 1, WLANTEST_SOCK_NAME,
sizeof(addr.sun_path) - 1);
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("connect");
Expand Down

0 comments on commit b8fc5cc

Please sign in to comment.