virtio: fix coverity warnings 85/9685/2
authorSteven <sluong@cisco.com>
Fri, 1 Dec 2017 00:56:54 +0000 (16:56 -0800)
committerDamjan Marion <dmarion.lists@gmail.com>
Fri, 1 Dec 2017 11:14:52 +0000 (11:14 +0000)
Fix 3 coverity warnings
1. api_format.c: init net_ns = 0 and remove its corresponding vec_add and
vec_free
2. netlink.c (reported in tap.c before the code was removed): resource leaked
due to fd is not close
3. tap.c: subtract 1 for size when calling strncpy to accommodate the
terminated NULL character

Change-Id: Iff4e66604862f0c06dac227b8cfd48d3979e41a5
Signed-off-by: Steven <sluong@cisco.com>
src/vat/api_format.c
src/vnet/devices/netlink.c
src/vnet/devices/virtio/tap.c

index 2a56423..c9c62c7 100644 (file)
@@ -7812,7 +7812,7 @@ api_tap_create_v2 (vat_main_t * vam)
   u8 random_mac = 1;
   u8 name_set = 0;
   u8 *tap_name;
-  u8 *net_ns;
+  u8 *net_ns = 0;
   u8 net_ns_set = 0;
   int ret;
   int rx_ring_sz = 0, tx_ring_sz = 0;
@@ -7875,7 +7875,6 @@ api_tap_create_v2 (vat_main_t * vam)
     }
 
   vec_add1 (tap_name, 0);
-  vec_add1 (net_ns, 0);
 
   /* Construct the API message */
   M (TAP_CREATE_V2, mp);
@@ -7890,7 +7889,6 @@ api_tap_create_v2 (vat_main_t * vam)
     clib_memcpy (mp->net_ns, net_ns, vec_len (net_ns));
 
   vec_free (tap_name);
-  vec_free (net_ns);
 
   /* send it... */
   S (mp);
index b2a6513..b05daf2 100644 (file)
@@ -48,16 +48,16 @@ vnet_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
 
   memset (&req, 0, sizeof (req));
   if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
-    {
-      err = clib_error_return_unix (0, "socket(AF_NETLINK)");
-      goto error;
-    }
+    return clib_error_return_unix (0, "socket(AF_NETLINK)");
 
   ra.nl_family = AF_NETLINK;
   ra.nl_pid = getpid ();
 
   if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
-    return clib_error_return_unix (0, "bind");
+    {
+      err = clib_error_return_unix (0, "bind");
+      goto error;
+    }
 
   req.nh.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
   req.nh.nlmsg_flags = NLM_F_REQUEST;
@@ -75,6 +75,7 @@ vnet_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
     err = clib_error_return_unix (0, "send");
 
 error:
+  close (sock);
   return err;
 }
 
index f6db2c9..34339c9 100644 (file)
@@ -109,7 +109,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
     }
 
   ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
-  strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE);
+  strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1);
   _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
 
   vif->ifindex = if_nametoindex ((char *) args->name);