strncpy() has a lot of pitfalls.  A while back we replaced all its uses by
calls to ovs_strlcpy() or ovs_strzcpy(), but some more have crept in.  This
commit fixes them.

Reported-by: Russell Bryant <rbry...@redhat.com>
Signed-off-by: Ben Pfaff <b...@nicira.com>
---
 lib/netdev-bsd.c             |   20 ++++++++++----------
 lib/ovs-router.c             |    4 ++--
 lib/tnl-arp-cache.c          |    4 ++--
 lib/tnl-ports.c              |    2 +-
 ofproto/ofproto-dpif-xlate.c |    2 +-
 5 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/lib/netdev-bsd.c b/lib/netdev-bsd.c
index a10b529..00e8bcd 100644
--- a/lib/netdev-bsd.c
+++ b/lib/netdev-bsd.c
@@ -361,7 +361,7 @@ netdev_bsd_construct_tap(struct netdev *netdev_)
 
     /* Turn device UP */
     ifr_set_flags(&ifr, IFF_UP);
-    strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
+    ovs_strlcpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
     error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
     if (error) {
         destroy_tap(netdev->tap_fd, kernel_name);
@@ -875,8 +875,8 @@ netdev_bsd_get_carrier(const struct netdev *netdev_, bool 
*carrier)
         struct ifmediareq ifmr;
 
         memset(&ifmr, 0, sizeof(ifmr));
-        strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
-                sizeof ifmr.ifm_name);
+        ovs_strlcpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
+                    sizeof ifmr.ifm_name);
 
         error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
         if (!error) {
@@ -1021,8 +1021,8 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct 
netdev_stats *stats)
     int error;
 
     memset(&ifdr, 0, sizeof(ifdr));
-    strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
-            sizeof(ifdr.ifdr_name));
+    ovs_strlcpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
+                sizeof(ifdr.ifdr_name));
     error = af_link_ioctl(SIOCGIFDATA, &ifdr);
     if (!error) {
         convert_stats(netdev_, stats, &ifdr.ifdr_data);
@@ -1126,7 +1126,7 @@ netdev_bsd_get_features(const struct netdev *netdev,
     /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
 
     memset(&ifmr, 0, sizeof(ifmr));
-    strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
+    ovs_strlcpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
 
     /* We make two SIOCGIFMEDIA ioctl calls.  The first to determine the
      * number of supported modes, and a second with a buffer to retrieve
@@ -1722,7 +1722,7 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int 
hwaddr_family OVS_UNUSED,
     int error;
 
     memset(&ifr, 0, sizeof ifr);
-    strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
+    ovs_strlcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
     ifr.ifr_addr.sa_family = hwaddr_family;
     ifr.ifr_addr.sa_len = hwaddr_len;
     memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
@@ -1748,7 +1748,7 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int 
hwaddr_family OVS_UNUSED,
         return EOPNOTSUPP;
     }
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.addr.ss_len = sizeof(req.addr);
     req.addr.ss_family = hwaddr_family;
     sdl = (struct sockaddr_dl *)&req.addr;
@@ -1764,7 +1764,7 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int 
hwaddr_family OVS_UNUSED,
     oldaddr = req.addr;
 
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.flags = IFLR_ACTIVE;
     sdl = (struct sockaddr_dl *)&req.addr;
     sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
@@ -1777,7 +1777,7 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int 
hwaddr_family OVS_UNUSED,
     }
 
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.addr = oldaddr;
     return af_link_ioctl(SIOCDLIFADDR, &req);
 #else
diff --git a/lib/ovs-router.c b/lib/ovs-router.c
index 8de2e2d..7f2611a 100644
--- a/lib/ovs-router.c
+++ b/lib/ovs-router.c
@@ -72,7 +72,7 @@ ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], 
ovs_be32 *gw)
     if (cr) {
         struct ovs_router_entry *p = ovs_router_entry_cast(cr);
 
-        strncpy(output_bridge, p->output_bridge, IFNAMSIZ);
+        ovs_strlcpy(output_bridge, p->output_bridge, IFNAMSIZ);
         *gw = p->gw;
         return true;
     }
@@ -110,7 +110,7 @@ ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, 
uint8_t plen,
     rt_init_match(&match, ip_dst, plen);
 
     p = xzalloc(sizeof *p);
-    strncpy(p->output_bridge, output_bridge, IFNAMSIZ);
+    ovs_strlcpy(p->output_bridge, output_bridge, IFNAMSIZ);
     p->gw = gw;
     p->nw_addr = match.flow.nw_dst;
     p->plen = plen;
diff --git a/lib/tnl-arp-cache.c b/lib/tnl-arp-cache.c
index 59ec0bc..c219fa3 100644
--- a/lib/tnl-arp-cache.c
+++ b/lib/tnl-arp-cache.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Nicira, Inc.
+ * Copyright (c) 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ tnl_arp_snoop(const struct flow *flow, struct 
flow_wildcards *wc,
     arp->ip = flow->nw_src;
     memcpy(arp->mac, flow->arp_sha, ETH_ADDR_LEN);
     arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
-    strncpy(arp->br_name, name, IFNAMSIZ);
+    ovs_strlcpy(arp->br_name, name, IFNAMSIZ);
     cmap_insert(&table, &arp->cmap_node, (OVS_FORCE uint32_t) arp->ip);
     ovs_mutex_unlock(&mutex);
     return 0;
diff --git a/lib/tnl-ports.c b/lib/tnl-ports.c
index e6739b9..c8b2615 100644
--- a/lib/tnl-ports.c
+++ b/lib/tnl-ports.c
@@ -101,7 +101,7 @@ tnl_port_map_insert(odp_port_t port, ovs_be32 ip_dst, 
ovs_be16 udp_port,
 
         cls_rule_init(&p->cr, &match, 0);   /* Priority == 0. */
         ovs_refcount_init(&p->ref_cnt);
-        strncpy(p->dev_name, dev_name, IFNAMSIZ);
+        ovs_strlcpy(p->dev_name, dev_name, IFNAMSIZ);
 
         classifier_insert(&cls, &p->cr, NULL, 0);
     }
diff --git a/ofproto/ofproto-dpif-xlate.c b/ofproto/ofproto-dpif-xlate.c
index 2f97aa5..8209e55 100644
--- a/ofproto/ofproto-dpif-xlate.c
+++ b/ofproto/ofproto-dpif-xlate.c
@@ -2639,7 +2639,7 @@ build_tunnel_send(const struct xlate_ctx *ctx, const 
struct xport *xport,
         struct xc_entry *entry;
 
         entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_ARP);
-        strncpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name, 
IFNAMSIZ);
+        ovs_strlcpy(entry->u.tnl_arp_cache.br_name, out_dev->xbridge->name, 
IFNAMSIZ);
         entry->u.tnl_arp_cache.d_ip = d_ip;
     }
     err = tnl_port_build_header(xport->ofport, flow,
-- 
1.7.10.4

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to