session/vcl: fix coverity warnings
[vpp.git] / src / vcl / ldp.c
index ab4cfbf..2468bcc 100644 (file)
@@ -261,7 +261,12 @@ ldp_init (void)
        }
     }
 
-  clib_time_init (&ldpw->clib_time);
+  /* *INDENT-OFF* */
+  pool_foreach (ldpw, ldp->workers, ({
+    clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
+  }));
+  /* *INDENT-ON* */
+
   LDBG (0, "LDP initialization: done!");
 
   return 0;
@@ -641,6 +646,9 @@ ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
 {
   uword fd;
 
+  if (!libcb)
+    return;
+
   /* *INDENT-OFF* */
   clib_bitmap_foreach (fd, result, ({
     FD_SET ((int)fd, libcb);
@@ -668,6 +676,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       return -1;
     }
 
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
+
   if (timeout)
     {
       time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
@@ -720,7 +731,8 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       goto done;
     }
 
-  libc_tspec = si_bits ? libc_tspec : *timeout;
+  if (!si_bits)
+    libc_tspec = timeout ? *timeout : libc_tspec;
 
   do
     {
@@ -1470,7 +1482,7 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags,
          __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
 {
   vls_handle_t sid;
-  ssize_t size;
+  ssize_t size, rv;
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1487,7 +1499,11 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags,
          size = vls_recvfrom (sid, buf, n, flags, &ep);
 
          if (size > 0)
-           size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
+           {
+             rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
+             if (rv < 0)
+               size = rv;
+           }
        }
       else
        size = vls_recvfrom (sid, buf, n, flags, NULL);
@@ -2165,6 +2181,8 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
       return -1;
     }
 
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
   time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
   max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
 
@@ -2232,14 +2250,15 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
   int rv, i, n_revents = 0;
   vls_handle_t vlsh;
   vcl_poll_t *vp;
-  double wait_for_time;
+  double max_time;
 
   LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
 
-  if (timeout >= 0)
-    wait_for_time = (f64) timeout / 1000;
-  else
-    wait_for_time = -1;
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
+
+  max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
+  max_time += clib_time_now (&ldpw->clib_time);
 
   for (i = 0; i < nfds; i++)
     {
@@ -2299,8 +2318,7 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
          goto done;
        }
     }
-  while ((wait_for_time == -1) ||
-        (clib_time_now (&ldpw->clib_time) < wait_for_time));
+  while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
   rv = 0;
 
 done: