From 82effc97f9f2d1c258ea50cb11b130753b8ba805 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 1 Jan 2015 16:59:35 -0600 Subject: [PATCH] Switch a lot of strncpy() calls to xstrncpy(). --- toys/other/login.c | 2 +- toys/other/losetup.c | 2 +- toys/pending/arp.c | 4 ++-- toys/pending/arping.c | 4 ++-- toys/pending/brctl.c | 16 ++++++++-------- toys/pending/dhcp.c | 6 ++---- toys/pending/dhcpd.c | 6 ++---- toys/pending/ip.c | 24 ++++++++++++------------ toys/pending/netstat.c | 11 +---------- toys/pending/route.c | 2 +- toys/pending/tar.c | 6 +++--- 11 files changed, 35 insertions(+), 48 deletions(-) diff --git a/toys/other/login.c b/toys/other/login.c index 91523d4..0d6d364 100644 --- a/toys/other/login.c +++ b/toys/other/login.c @@ -165,7 +165,7 @@ void login_main(void) tcflush(0, TCIFLUSH); username[sizeof(username)-1] = 0; - if (*toys.optargs) strncpy(username, *toys.optargs, sizeof(username)-1); + if (*toys.optargs) xstrncpy(username, *toys.optargs, sizeof(username)); else { read_user(username, sizeof(username)); if (!*username) continue; diff --git a/toys/other/losetup.c b/toys/other/losetup.c index e3094ef..9568627 100644 --- a/toys/other/losetup.c +++ b/toys/other/losetup.c @@ -112,7 +112,7 @@ static void loopback_setup(char *device, char *file) if (ioctl(lfd, LOOP_SET_FD, ffd)) perror_exit("%s=%s", device, file); loop->lo_offset = TT.offset; loop->lo_sizelimit = TT.size; - strncpy((char *)loop->lo_file_name, s, LO_NAME_SIZE); + xstrncpy((char *)loop->lo_file_name, s, LO_NAME_SIZE); s[LO_NAME_SIZE-1] = 0; if (ioctl(lfd, LOOP_SET_STATUS64, loop)) perror_exit("%s=%s", device, file); if (flags & FLAG_s) printf("%s", device); diff --git a/toys/pending/arp.c b/toys/pending/arp.c index 1153ebc..e725112 100644 --- a/toys/pending/arp.c +++ b/toys/pending/arp.c @@ -175,7 +175,7 @@ static int set_entry(void) flags = ATF_PERM | ATF_COM; if (toys.optargs[2]) check_flags(&flags, (toys.optargs+2)); req.arp_flags = flags; - strncpy(req.arp_dev, TT.device, sizeof(req.arp_dev)); + xstrncpy(req.arp_dev, TT.device, sizeof(req.arp_dev)); xioctl(TT.sockfd, SIOCSARP, &req); if (toys.optflags & FLAG_v) xprintf("Entry set for %s\n", toys.optargs[0]); @@ -204,7 +204,7 @@ static int delete_entry(void) flags = ATF_PERM; if (toys.optargs[1]) check_flags(&flags, (toys.optargs+1)); req.arp_flags = flags; - strncpy(req.arp_dev, TT.device, sizeof(req.arp_dev)); + xstrncpy(req.arp_dev, TT.device, sizeof(req.arp_dev)); xioctl(TT.sockfd, SIOCDARP, &req); if (toys.optflags & FLAG_v) xprintf("Delete entry for %s\n", toys.optargs[0]); diff --git a/toys/pending/arping.c b/toys/pending/arping.c index bc1fa5e..3e522bd 100644 --- a/toys/pending/arping.c +++ b/toys/pending/arping.c @@ -63,7 +63,7 @@ static void get_interface(char *interface, int *ifindex, uint32_t *oip, int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); req.ifr_addr.sa_family = AF_INET; - strncpy(req.ifr_name, interface, IFNAMSIZ); + xstrncpy(req.ifr_name, interface, IFNAMSIZ); req.ifr_name[IFNAMSIZ-1] = '\0'; xioctl(fd, SIOCGIFFLAGS, &req); @@ -215,7 +215,7 @@ void arping_main(void) TT.sockfd = xsocket(AF_PACKET, SOCK_DGRAM, 0); memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, TT.iface, IFNAMSIZ); + xstrncpy(ifr.ifr_name, TT.iface, IFNAMSIZ); get_interface(TT.iface, &if_index, NULL, NULL); src_pk.sll_ifindex = if_index; diff --git a/toys/pending/brctl.c b/toys/pending/brctl.c index fecbf68..e3b1526 100644 --- a/toys/pending/brctl.c +++ b/toys/pending/brctl.c @@ -49,7 +49,7 @@ static void get_ports(char *bridge, int *indices) memset(ifindices, 0, MAX_BRIDGES); args[1] = (unsigned long)ifindices; - strncpy(ifr.ifr_name, bridge, IFNAMSIZ); + xstrncpy(ifr.ifr_name, bridge, IFNAMSIZ); ifr.ifr_data = (char *)args; xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr); if (indices) memcpy(indices, ifindices, sizeof(ifindices)); @@ -62,7 +62,7 @@ void get_br_info(char *bridge, struct __bridge_info *info) (unsigned long) info, 0, 0 }; memset(info, 0, sizeof(*info)); - strncpy(ifr.ifr_name, bridge, IFNAMSIZ); + xstrncpy(ifr.ifr_name, bridge, IFNAMSIZ); ifr.ifr_data = (char *)args; if (ioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr) < 0) { @@ -118,7 +118,7 @@ void br_addbr(char **argv) #ifdef SIOCBRADDBR xioctl(TT.sockfd, SIOCBRADDBR, argv[0]); #else - strncpy(br, argv[0], IFNAMSIZ); + xstrncpy(br, argv[0], IFNAMSIZ); xioctl(TT.sockfd, SIOCSIFBR, args); #endif } @@ -131,7 +131,7 @@ void br_delbr(char **argv) #ifdef SIOCBRDELBR xioctl(TT.sockfd, SIOCBRDELBR, argv[0]); #else - strncpy(br, argv[0], IFNAMSIZ); + xstrncpy(br, argv[0], IFNAMSIZ); xioctl(TT.sockfd, SIOCSIFBR, args); #endif } @@ -148,7 +148,7 @@ void br_addif(char **argv) xioctl(TT.sockfd, SIOCBRADDIF, &ifr); #else args[1] = index; - strncpy(ifr.ifr_name, argv[0], IFNAMSIZ); + xstrncpy(ifr.ifr_name, argv[0], IFNAMSIZ); ifr.ifr_data = (char *)args; xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr); #endif @@ -166,7 +166,7 @@ void br_delif(char **argv) xioctl(TT.sockfd, SIOCBRDELIF, &ifr); #else args[1] = index; - strncpy(ifr.ifr_name, argv[0], IFNAMSIZ); + xstrncpy(ifr.ifr_name, argv[0], IFNAMSIZ); ifr.ifr_data = (char *)args; xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr); #endif @@ -194,7 +194,7 @@ void set_time(char *br, unsigned long cmd, unsigned long val) struct ifreq ifr; unsigned long args[4] = {cmd, val, 0, 0}; - strncpy(ifr.ifr_name, br, IFNAMSIZ); + xstrncpy(ifr.ifr_name, br, IFNAMSIZ); ifr.ifr_data = (char *)args; xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr); } @@ -270,7 +270,7 @@ void set_cost_prio(char *br, char *port, unsigned long cmd, unsigned long val) } if (i >= MAX_BRIDGES) error_exit("%s not in bridge", port); args[1] = i; - strncpy(ifr.ifr_name, br, IFNAMSIZ); + xstrncpy(ifr.ifr_name, br, IFNAMSIZ); ifr.ifr_data = (char *)args; xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr); } diff --git a/toys/pending/dhcp.c b/toys/pending/dhcp.c index c7ed1bf..f8226db 100644 --- a/toys/pending/dhcp.c +++ b/toys/pending/dhcp.c @@ -1230,16 +1230,14 @@ static void renew(void) // Sends a IP release request. static void release(void) { - int len = sizeof("255.255.255.255\0"); - char buffer[len]; + char buffer[sizeof("255.255.255.255\0")]; struct in_addr temp_addr; mode_app(); // send release packet if (state->status == STATE_BOUND || state->status == STATE_RENEWING || state->status == STATE_REBINDING) { temp_addr.s_addr = htonl(server); - strncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer)); - buffer[len - 1] = '\0'; + xstrncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer)); temp_addr.s_addr = state->ipaddr.s_addr; infomsg( infomode, "Unicasting a release of %s to %s", inet_ntoa(temp_addr), buffer); dhcpc_sendmsg(DHCPRELEASE); diff --git a/toys/pending/dhcpd.c b/toys/pending/dhcpd.c index 2f0c3a5..9c3389c 100644 --- a/toys/pending/dhcpd.c +++ b/toys/pending/dhcpd.c @@ -246,8 +246,7 @@ static int get_interface(const char *interface, int *ifindex, uint32_t *oip, uin int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); req.ifr_addr.sa_family = AF_INET; - strncpy(req.ifr_name, interface, IFNAMSIZ); - req.ifr_name[IFNAMSIZ-1] = '\0'; + xstrncpy(req.ifr_name, interface, IFNAMSIZ); xioctl(fd, SIOCGIFFLAGS, &req); @@ -625,8 +624,7 @@ static int open_listensock(void) return -1; } memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, gconfig.interface, IFNAMSIZ); - ifr.ifr_name[IFNAMSIZ -1] = '\0'; + xstrncpy(ifr.ifr_name, gconfig.interface, IFNAMSIZ); setsockopt(gstate.listensock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)); memset(&addr, 0, sizeof(addr)); diff --git a/toys/pending/ip.c b/toys/pending/ip.c index be4d8bb..9a01d95 100644 --- a/toys/pending/ip.c +++ b/toys/pending/ip.c @@ -646,7 +646,7 @@ static int link_set(char **argv) memset(&req, 0, sizeof(req)); if (!*argv) error_exit("\"dev\" missing"); - strncpy(req.ifr_name, *argv, IF_NAMESIZE); + xstrncpy(req.ifr_name, *argv, IF_NAMESIZE); fd = xsocket(AF_INET, SOCK_DGRAM, 0); xioctl(fd, SIOCGIFINDEX, &req); for (++argv; *argv;) { @@ -674,9 +674,9 @@ static int link_set(char **argv) ++argv; break; case 5: - strncpy(req.ifr_ifru.ifru_newname, *argv, IF_NAMESIZE); + xstrncpy(req.ifr_ifru.ifru_newname, *argv, IF_NAMESIZE); xioctl(fd, SIOCSIFNAME, &req); - strncpy(req.ifr_name, *argv++, IF_NAMESIZE); + xstrncpy(req.ifr_name, *argv++, IF_NAMESIZE); xioctl(fd, SIOCGIFINDEX, &req); break; case 6: @@ -839,7 +839,7 @@ static int get_link_info(struct nlmsghdr* h,struct linkdata* link,char **argv) link->next = link->prev = 0; link->iface_type = iface->ifi_type; if (!lname) error_exit("Invalid link."); - strncpy(link->type, lname, IFNAMSIZ); + xstrncpy(link->type, lname, IFNAMSIZ); free(lname); link->iface_idx = iface->ifi_index; link->flags = iface->ifi_flags; @@ -886,7 +886,7 @@ static int get_link_info(struct nlmsghdr* h,struct linkdata* link,char **argv) {"DORMANT", 5}, {"UP", 6}, {NULL, -1}}; if (!(lname = get_flag_string(flags, *((int*)(RTA_DATA(attr))), 0))) error_exit("Invalid state."); - strncpy(link->state, lname,IFNAMSIZ); + xstrncpy(link->state, lname,IFNAMSIZ); free(lname); } break; @@ -974,7 +974,7 @@ static int print_addrinfo(struct nlmsghdr *h, int flag_l) if (flag_l && addrinfo.label && ifa->ifa_family == AF_INET6) return 0; if ((rta_tb[IFA_LABEL])) { - strncpy(label, RTA_DATA(rta_tb[IFA_LABEL]), 256); + xstrncpy(label, RTA_DATA(rta_tb[IFA_LABEL]), 256); label[255] = '\0'; if (addrinfo.label && fnmatch(addrinfo.label, label, 0)) return 0; @@ -2330,8 +2330,8 @@ static int tnl_ioctl(char *dev, int rtype, struct ip_tunnel_parm *ptnl) int fd, ret = 0; if ((rtype == SIOCCHGTUNNEL || rtype == SIOCDELTUNNEL) && *ptnl->name) - strncpy(req.ifr_name, ptnl->name, IF_NAMESIZE); - else strncpy(req.ifr_name, dev, IF_NAMESIZE); + xstrncpy(req.ifr_name, ptnl->name, IF_NAMESIZE); + else xstrncpy(req.ifr_name, dev, IF_NAMESIZE); if (rtype != SIOCGIFHWADDR) req.ifr_ifru.ifru_data = (void*)ptnl; fd = xsocket(AF_INET, SOCK_DGRAM, 0); @@ -2448,7 +2448,7 @@ static void parse_iptunnel_args(struct ip_tunnel_parm *ptnl, char **argv, // frag_off is measured in units of 8 octets (64 bits) ptnl->iph.frag_off = htons(IP_DF); if (*argv && ipt_opt_idx <= 2 && string_to_idx(*argv, opts) == -1) { - strncpy(ptnl->name, *argv, IF_NAMESIZE); + xstrncpy(ptnl->name, *argv, IF_NAMESIZE); if (ipt_opt_idx == 1) { struct ip_tunnel_parm iptnl_old; @@ -2545,7 +2545,7 @@ static void parse_iptunnel_args(struct ip_tunnel_parm *ptnl, char **argv, struct ifreq req; int fd; - strncpy(req.ifr_name, *argv, IFNAMSIZ); + xstrncpy(req.ifr_name, *argv, IFNAMSIZ); fd = xsocket(AF_INET, SOCK_DGRAM, 0); xioctl(fd, SIOCGIFINDEX, &req); close(fd); @@ -2578,12 +2578,12 @@ static void parse_iptunnel_args(struct ip_tunnel_parm *ptnl, char **argv, if (*ptnl->name) error_exit("invalid tunnel"); else { if (!*++argv) error_exit("name is missing"); - strncpy(ptnl->name, *argv, IF_NAMESIZE); + xstrncpy(ptnl->name, *argv, IF_NAMESIZE); } break; default: if (*ptnl->name) error_exit("invalid tunnel"); - strncpy(ptnl->name, *argv, IF_NAMESIZE); + xstrncpy(ptnl->name, *argv, IF_NAMESIZE); break; } } diff --git a/toys/pending/netstat.c b/toys/pending/netstat.c index 5935563..fbb9eb1 100644 --- a/toys/pending/netstat.c +++ b/toys/pending/netstat.c @@ -76,15 +76,6 @@ static const char *get_basename(char *name) if (c) return c + 1; return name; } -/* - * copy string from src to dest -> only number of bytes. - */ -static char *safe_strncpy(char *dst, char *src, size_t size) -{ - if(!size) return dst; - dst[--size] = '\0'; - return strncpy(dst, src, size); -} /* * locate character in string. @@ -410,7 +401,7 @@ static void add2list(long inode, char *progname) } PID_LIST *new = (PID_LIST *)xzalloc(sizeof(PID_LIST)); new->inode = inode; - safe_strncpy(new->name, progname, PROGNAME_LEN-1); + xstrncpy(new->name, progname, PROGNAME_LEN); new->next = pid_list; pid_list = new; } diff --git a/toys/pending/route.c b/toys/pending/route.c index 163ce6b..ac1bbef 100644 --- a/toys/pending/route.c +++ b/toys/pending/route.c @@ -411,7 +411,7 @@ static void setroute_inet6(char **argv) if (dev_name) { char ifre_buf[sizeof(struct ifreq)] = {0,}; struct ifreq *ifre = (struct ifreq*)ifre_buf; - strncpy(ifre->ifr_name, dev_name, IFNAMSIZ-1); + xstrncpy(ifre->ifr_name, dev_name, IFNAMSIZ); xioctl(sockfd, SIOGIFINDEX, ifre); rt.rtmsg_ifindex = ifre->ifr_ifindex; } diff --git a/toys/pending/tar.c b/toys/pending/tar.c index 5b060c6..dc1480c 100644 --- a/toys/pending/tar.c +++ b/toys/pending/tar.c @@ -189,7 +189,7 @@ static void add_file(struct archive_handler *tar, char **nam, struct stat *st) } memset(&hdr, 0, sizeof(hdr)); - strncpy(hdr.name, hname, sizeof(hdr.name)); + xstrncpy(hdr.name, hname, sizeof(hdr.name)); itoo(hdr.mode, sizeof(hdr.mode), st->st_mode &07777); itoo(hdr.uid, sizeof(hdr.uid), st->st_uid); itoo(hdr.gid, sizeof(hdr.gid), st->st_gid); @@ -202,7 +202,7 @@ static void add_file(struct archive_handler *tar, char **nam, struct stat *st) hdr.type = '1'; if (strlen(node->arg) > sizeof(hdr.link)) write_longname(tar, hname, 'K'); //write longname LINK - strncpy(hdr.link, node->arg, sizeof(hdr.link)); + xstrncpy(hdr.link, node->arg, sizeof(hdr.link)); } else if (S_ISREG(st->st_mode)) { hdr.type = '0'; if (st->st_size <= (off_t)0777777777777LL) @@ -219,7 +219,7 @@ static void add_file(struct archive_handler *tar, char **nam, struct stat *st) } if (strlen(lnk) > sizeof(hdr.link)) write_longname(tar, hname, 'K'); //write longname LINK - strncpy(hdr.link, lnk, sizeof(hdr.link)); + xstrncpy(hdr.link, lnk, sizeof(hdr.link)); free(lnk); } else if (S_ISDIR(st->st_mode)) hdr.type = '5'; -- 2.7.4