VCL-LDPRELOAD: Fix Coverity Warning CID 178776

- Fix broken unit test code.
- Check for NULL ptr in vppcom_session_attr()
  VPPCOM_ATTR_GET_*_ADDR endpoint parameters.

Change-Id: I01169edce098ee855a9cba08893c2b08c93ea6c3
Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
diff --git a/src/vcl/sock_test_server.c b/src/vcl/sock_test_server.c
index 23fd46d..b30dcf9 100644
--- a/src/vcl/sock_test_server.c
+++ b/src/vcl/sock_test_server.c
@@ -450,15 +450,6 @@
   if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS,
 			   buffer, &buflen) != VPPCOM_OK)
     printf ("\nGET_FLAGS2:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
-
-  buflen = BUFLEN;
-  if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_PEER_ADDR,
-			   buffer, &buflen) != VPPCOM_OK)
-    printf ("\nGET_PEER_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
-  buflen = BUFLEN;
-  if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_LCL_ADDR,
-			   buffer, &buflen) != VPPCOM_OK)
-    printf ("\nGET_LCL_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
 #endif
 #else
   rv =
@@ -612,20 +603,28 @@
 	    {
 #ifdef VCL_TEST
 #if VPPCOM_SESSION_ATTR_UNIT_TEST
-	      buflen = BUFLEN;
-	      if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_NREAD,
-				       buffer, &buflen) < VPPCOM_OK)
-		printf ("\nNREAD: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
-	      if (vppcom_session_attr (client_fd,
-				       VPPCOM_ATTR_GET_PEER_ADDR,
-				       buffer, &buflen) != VPPCOM_OK)
-		printf ("\nGET_PEER_ADDR: Oh no, Mr. "
-			"Biiiiiiiiiiiilllllll ! ! ! !\n");
-	      buflen = BUFLEN;
-	      if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_LCL_ADDR,
-				       buffer, &buflen) != VPPCOM_OK)
-		printf ("\nGET_LCL_ADDR: Oh no, Mr. "
-			"Biiiiiiiiiiiilllllll ! ! ! !\n");
+	      {
+		vppcom_endpt_t ep;
+		uint8_t addr[16];
+
+		ep.ip = addr;
+		buflen = BUFLEN;
+		if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_NREAD,
+					 buffer, &buflen) < VPPCOM_OK)
+		  printf ("\nNREAD: Oh no, Mr. "
+			  "Biiiiiiiiiiiilllllll ! ! ! !\n");
+		buflen = sizeof (ep);
+		if (vppcom_session_attr (client_fd,
+					 VPPCOM_ATTR_GET_PEER_ADDR,
+					 &ep, &buflen) != VPPCOM_OK)
+		  printf ("\nGET_PEER_ADDR: Oh no, Mr. "
+			  "Biiiiiiiiiiiilllllll ! ! ! !\n");
+		buflen = sizeof (ep);
+		if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_LCL_ADDR,
+					 &ep, &buflen) != VPPCOM_OK)
+		  printf ("\nGET_LCL_ADDR: Oh no, Mr. "
+			  "Biiiiiiiiiiiilllllll ! ! ! !\n");
+	      }
 #endif
 #endif
 	      rx_bytes = sock_test_read (client_fd, conn->buf,
diff --git a/src/vcl/vcom_socket.c b/src/vcl/vcom_socket.c
index 81a4b76..13e008c 100644
--- a/src/vcl/vcom_socket.c
+++ b/src/vcl/vcom_socket.c
@@ -1298,12 +1298,43 @@
 }
 
 static inline int
-vcom_session_getsockname (int sid, vppcom_endpt_t * ep)
+vcom_socket_copy_ep_to_sockaddr (__SOCKADDR_ARG __addr,
+				 socklen_t * __restrict __len,
+				 vppcom_endpt_t * ep)
 {
-  int rv;
-  uint32_t size = sizeof (*ep);
+  int rv = 0;
+  int sa_len, copy_len;
 
-  rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, ep, &size);
+  __addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
+  switch (__addr->sa_family)
+    {
+    case AF_INET:
+      ((struct sockaddr_in *) __addr)->sin_port = ep->port;
+      if (*__len > sizeof (struct sockaddr_in))
+	*__len = sizeof (struct sockaddr_in);
+      sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
+      copy_len = *__len - sa_len;
+      if (copy_len > 0)
+	memcpy (&((struct sockaddr_in *) __addr)->sin_addr, ep->ip, copy_len);
+      break;
+
+    case AF_INET6:
+      ((struct sockaddr_in6 *) __addr)->sin6_port = ep->port;
+      if (*__len > sizeof (struct sockaddr_in6))
+	*__len = sizeof (struct sockaddr_in6);
+      sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
+      copy_len = *__len - sa_len;
+      if (copy_len > 0)
+	memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
+		__in6_u.__u6_addr8, ep->ip, copy_len);
+      break;
+
+    default:
+      /* Not possible */
+      rv = -EAFNOSUPPORT;
+      break;
+    }
+
   return rv;
 }
 
@@ -1315,7 +1346,12 @@
   vcom_socket_main_t *vsm = &vcom_socket_main;
   uword *p;
   vcom_socket_t *vsock;
+  vppcom_endpt_t ep;
+  u8 addr_buf[sizeof (struct in6_addr)];
+  uint32_t size = sizeof (ep);
 
+  if (!__addr || !__len)
+    return -EFAULT;
 
   p = hash_get (vsm->sockidx_by_fd, __fd);
   if (!p)
@@ -1328,34 +1364,10 @@
   if (vsock->type != SOCKET_TYPE_VPPCOM_BOUND)
     return -EINVAL;
 
-  if (!__addr || !__len)
-    return -EFAULT;
-
-  vppcom_endpt_t ep;
-  ep.ip = (u8 *) & ((const struct sockaddr_in *) __addr)->sin_addr;
-  rv = vcom_session_getsockname (vsock->sid, &ep);
-  if (rv == 0)
-    {
-      if (ep.vrf == VPPCOM_VRF_DEFAULT)
-	{
-	  __addr->sa_family = ep.is_ip4 == VPPCOM_IS_IP4 ? AF_INET : AF_INET6;
-	  switch (__addr->sa_family)
-	    {
-	    case AF_INET:
-	      ((struct sockaddr_in *) __addr)->sin_port = ep.port;
-	      *__len = sizeof (struct sockaddr_in);
-	      break;
-
-	    case AF_INET6:
-	      ((struct sockaddr_in6 *) __addr)->sin6_port = ep.port;
-	      *__len = sizeof (struct sockaddr_in6);
-	      break;
-
-	    default:
-	      break;
-	    }
-	}
-    }
+  ep.ip = addr_buf;
+  rv = vppcom_session_attr (vsock->sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
+  if (rv == VPPCOM_OK)
+    rv = vcom_socket_copy_ep_to_sockaddr (__addr, __len, &ep);
 
   return rv;
 }
@@ -1414,47 +1426,6 @@
   return rv;
 }
 
-static inline int
-vcom_socket_copy_ep_to_sockaddr (__SOCKADDR_ARG __addr,
-				 socklen_t * __restrict __len,
-				 vppcom_endpt_t * ep)
-{
-  int rv = 0;
-  int sa_len, copy_len;
-
-  __addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
-  switch (__addr->sa_family)
-    {
-    case AF_INET:
-      ((struct sockaddr_in *) __addr)->sin_port = ep->port;
-      if (*__len > sizeof (struct sockaddr_in))
-	*__len = sizeof (struct sockaddr_in);
-      sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
-      copy_len = *__len - sa_len;
-      if (copy_len > 0)
-	memcpy (&((struct sockaddr_in *) __addr)->sin_addr, ep->ip, copy_len);
-      break;
-
-    case AF_INET6:
-      ((struct sockaddr_in6 *) __addr)->sin6_port = ep->port;
-      if (*__len > sizeof (struct sockaddr_in6))
-	*__len = sizeof (struct sockaddr_in6);
-      sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
-      copy_len = *__len - sa_len;
-      if (copy_len > 0)
-	memcpy (((struct sockaddr_in6 *) __addr)->sin6_addr.
-		__in6_u.__u6_addr8, ep->ip, copy_len);
-      break;
-
-    default:
-      /* Not possible */
-      rv = -EAFNOSUPPORT;
-      break;
-    }
-
-  return rv;
-}
-
 int
 vcom_socket_getpeername (int __fd, __SOCKADDR_ARG __addr,
 			 socklen_t * __restrict __len)
diff --git a/src/vcl/vppcom.c b/src/vcl/vppcom.c
index ce222a7..2e3af1b 100644
--- a/src/vcl/vppcom.c
+++ b/src/vcl/vppcom.c
@@ -3797,7 +3797,7 @@
       break;
 
     case VPPCOM_ATTR_GET_PEER_ADDR:
-      if (buffer && buflen && (*buflen >= sizeof (*ep)))
+      if (buffer && buflen && (*buflen >= sizeof (*ep)) && ep->ip)
 	{
 	  ep->vrf = session->vrf;
 	  ep->is_ip4 = session->peer_addr.is_ip4;
@@ -3821,7 +3821,7 @@
       break;
 
     case VPPCOM_ATTR_GET_LCL_ADDR:
-      if (buffer && buflen && (*buflen >= sizeof (*ep)))
+      if (buffer && buflen && (*buflen >= sizeof (*ep)) && ep->ip)
 	{
 	  ep->vrf = session->vrf;
 	  ep->is_ip4 = session->lcl_addr.is_ip4;