changeset 1640:41efba077b75 draft

Switch a lot of strncpy() calls to xstrncpy().
author Rob Landley <rob@landley.net>
date Thu, 01 Jan 2015 16:59:35 -0600
parents 856b544f8fce
children 382d5b9ff2d5
files toys/other/login.c toys/other/losetup.c toys/pending/arp.c toys/pending/arping.c toys/pending/brctl.c toys/pending/dhcp.c toys/pending/dhcpd.c toys/pending/ip.c toys/pending/netstat.c toys/pending/route.c toys/pending/tar.c
diffstat 11 files changed, 35 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/login.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/other/login.c	Thu Jan 01 16:59:35 2015 -0600
@@ -165,7 +165,7 @@
     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;
--- a/toys/other/losetup.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/other/losetup.c	Thu Jan 01 16:59:35 2015 -0600
@@ -112,7 +112,7 @@
     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);
--- a/toys/pending/arp.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/arp.c	Thu Jan 01 16:59:35 2015 -0600
@@ -175,7 +175,7 @@
   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 @@
   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]);
--- a/toys/pending/arping.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/arping.c	Thu Jan 01 16:59:35 2015 -0600
@@ -63,7 +63,7 @@
   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 @@
   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;
 
--- a/toys/pending/brctl.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/brctl.c	Thu Jan 01 16:59:35 2015 -0600
@@ -49,7 +49,7 @@
 
   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 @@
     (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 @@
 #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 @@
 #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 @@
   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 @@
   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 @@
   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 @@
   }
   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);
 }
--- a/toys/pending/dhcp.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/dhcp.c	Thu Jan 01 16:59:35 2015 -0600
@@ -1230,16 +1230,14 @@
 // 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);
--- a/toys/pending/dhcpd.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/dhcpd.c	Thu Jan 01 16:59:35 2015 -0600
@@ -246,8 +246,7 @@
   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 @@
       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));
--- a/toys/pending/ip.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/ip.c	Thu Jan 01 16:59:35 2015 -0600
@@ -646,7 +646,7 @@
 
   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 @@
         ++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 @@
   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 @@
             {"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 @@
 
   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 @@
   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 @@
   // 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 @@
           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 @@
         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;
     }
   }
--- a/toys/pending/netstat.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/netstat.c	Thu Jan 01 16:59:35 2015 -0600
@@ -76,15 +76,6 @@
   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 @@
   }
   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;
 }
--- a/toys/pending/route.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/route.c	Thu Jan 01 16:59:35 2015 -0600
@@ -411,7 +411,7 @@
     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;
     }          
--- a/toys/pending/tar.c	Thu Jan 01 16:49:55 2015 -0600
+++ b/toys/pending/tar.c	Thu Jan 01 16:59:35 2015 -0600
@@ -189,7 +189,7 @@
   }
 
   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 @@
     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 @@
     }
     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';