vpp-189 Clean up more coverity warnings 29/2229/1
authorDave Barach <[email protected]>
Fri, 5 Aug 2016 14:10:18 +0000 (10:10 -0400)
committerDave Barach <[email protected]>
Fri, 5 Aug 2016 14:10:38 +0000 (10:10 -0400)
Time to make the donuts

Change-Id: I528937800f7daefce19723dda0216e58d857942c
Signed-off-by: Dave Barach <[email protected]>
23 files changed:
vlib-api/vlibsocket/socksvr_vlib.c
vlib/vlib/unix/cli.c
vnet/vnet/ip/ip_input_acl.c
vnet/vnet/ip/udp_local.c
vnet/vnet/mpls-gre/mpls.c
vnet/vnet/policer/node_funcs.c
vnet/vnet/policer/xlate.c
vnet/vnet/rewrite.c
vnet/vnet/sr/sr.c
vnet/vnet/sr/sr_replicate.c
vnet/vnet/unix/tapcli.c
vnet/vnet/unix/tuntap.c
vnet/vnet/vxlan-gpe/decap.c
vnet/vnet/vxlan/decap.c
vpp/app/vpe_cli.c
vppapigen/lex.c
vppapigen/node.c
vppinfra/tools/elftool.c
vppinfra/vppinfra/asm_x86.c
vppinfra/vppinfra/bitmap.h
vppinfra/vppinfra/elf.c
vppinfra/vppinfra/elf_clib.c
vppinfra/vppinfra/elog.c

index eb8dd33..354c4f1 100644 (file)
@@ -575,12 +575,14 @@ socksvr_api_init (vlib_main_t * vm)
   rv = ioctl (sockfd, FIONBIO, &one);
   if (rv < 0)
     {
+      close (sockfd);
       return clib_error_return_unix (0, "FIONBIO");
     }
 
   rv = setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one));
   if (rv < 0)
     {
+      close (sockfd);
       return clib_error_return_unix (0, "SO_REUSEADDR");
     }
 
index 7d5e10b..22f07f9 100644 (file)
@@ -2397,8 +2397,8 @@ unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
       if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
        {
          /* Capture terminal resize events */
+          memset (&sa, 0, sizeof(sa));
          sa.sa_handler = unix_cli_resize_interrupt;
-         sa.sa_flags = 0;
          if (sigaction (SIGWINCH, &sa, 0) < 0)
            clib_panic ("sigaction");
 
index eaf7f34..6331cf4 100644 (file)
@@ -322,7 +322,7 @@ ip_inacl_inline (vlib_main_t * vm,
               t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
               t->next_index = next0;
               t->table_index = t0 ? t0 - vcm->tables : ~0;
-              t->offset = e0 ? vnet_classify_get_offset (t0, e0): ~0;
+              t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0): ~0;
             }
 
           /* verify speculative enqueue, maybe switch current next frame */
index 9b70147..4928d55 100644 (file)
@@ -242,8 +242,8 @@ udp46_input_inline (vlib_main_t * vm,
                                                    b0, sizeof (*tr));
               if (b0->error != node->errors[UDP_ERROR_LENGTH_ERROR])
                 {
-                  tr->src_port = h0->src_port;
-                  tr->dst_port = h0->dst_port;
+                  tr->src_port = h0 ? h0->src_port : 0;
+                  tr->dst_port = h0 ? h0->dst_port : 0;
                   tr->bound = (next0 != UDP_INPUT_NEXT_ICMP4_ERROR &&
                                next0 != UDP_INPUT_NEXT_ICMP6_ERROR);
                 }
@@ -254,8 +254,8 @@ udp46_input_inline (vlib_main_t * vm,
                                                    b1, sizeof (*tr));
               if (b1->error != node->errors[UDP_ERROR_LENGTH_ERROR])
                 {
-                  tr->src_port = h1->src_port;
-                  tr->dst_port = h1->dst_port;
+                  tr->src_port = h1 ? h1->src_port : 0;
+                  tr->dst_port = h1 ? h1->dst_port : 0;
                   tr->bound = (next1 != UDP_INPUT_NEXT_ICMP4_ERROR &&
                                next1 != UDP_INPUT_NEXT_ICMP6_ERROR);
                 }
index 4b36c5d..d914b4c 100644 (file)
@@ -399,7 +399,7 @@ int vnet_mpls_add_del_decap (u32 rx_fib_id,
       tx_fib_index_or_output_swif_index = tx_fib_id;
     }
 
-  key = ((u64)rx_fib_index<<32) | ((u64) (label_host_byte_order<<12))
+  key = ((u64) rx_fib_index<<32) | ((u64) label_host_byte_order<<12)
     | ((u64) s_bit<<8);
 
   p = hash_get (mm->mpls_decap_by_rx_fib_and_label, key);
@@ -748,8 +748,8 @@ int mpls_fib_reset_labels (u32 fib_id)
   
   vec_foreach (s, records)
     {
-      key = ((u64)fib_index <<32) | ((u64)(s->dest<<12)) |
-        ((u64)s->s_bit);
+       key = ((u64) fib_index <<32) | ((u64) s->dest<<12) |
+        ((u64) s->s_bit);
       
       hash_unset (mm->mpls_decap_by_rx_fib_and_label, key);
       pool_put_index (mm->decaps, s->entry_index);
index 0858a86..61a2973 100644 (file)
@@ -804,7 +804,7 @@ policer_classify_inline (vlib_main_t * vm,
               t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
               t->next_index = next0;
               t->table_index = t0 ? t0 - vcm->tables : ~0;
-              t->offset = e0 ? vnet_classify_get_offset (t0, e0): ~0;
+              t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0): ~0;
               t->policer_index = e0 ? e0->next_index: ~0;
             }
 
index 934c488..61976b1 100644 (file)
@@ -468,7 +468,7 @@ sse2_pol_convert_cfg_rates_to_hw (sse2_qos_pol_cfg_params_st *cfg,
                             &exp, &hi_mant);
     }
 
-    denom = (1 << exp);
+    denom = (1ULL << exp);
     if (hi_rate == eir_hw) {
         hw->peak_rate_man = (uint16_t)hi_mant;
         rc = sse2_qos_pol_round((uint64_t)cir_hw, denom, &rnd_value,
index a538eef..3435b0f 100644 (file)
@@ -209,7 +209,7 @@ void vnet_rewrite_for_sw_interface (vnet_main_t * vnm,
   ASSERT (hc->set_rewrite);
   n_rw_tmp = hc->set_rewrite (vnm, sw_if_index, packet_type, dst_address, rw_tmp, max_rewrite_bytes);
 
-  ASSERT (n_rw_tmp >= 0 && n_rw_tmp < max_rewrite_bytes);
+  ASSERT (n_rw_tmp < max_rewrite_bytes);
   vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rw_tmp, n_rw_tmp);
 }
 
index 05d8d81..d935b45 100644 (file)
@@ -724,8 +724,10 @@ find_or_add_shared_secret (ip6_sr_main_t * sm, u8 * secret, u32 * indexp)
       for (i = 0; i < vec_len (sm->hmac_keys); i++)
         {
           if (sm->hmac_keys[i].shared_secret == 0)
-            key = sm->hmac_keys + i;
-          goto found;
+            {
+              key = sm->hmac_keys + i;
+              goto found;
+            }
         }
       vec_validate (sm->hmac_keys, i);
       key = sm->hmac_keys + i;
@@ -970,7 +972,6 @@ int ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a)
   memset(&adj, 0, sizeof (adj));
 
   /* Create an adjacency and add to v6 fib */
-  adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
   adj.lookup_next_index = sm->ip6_lookup_sr_next_index;
   adj.explicit_fib_index = ~0;
   
@@ -1020,7 +1021,9 @@ int ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a)
          p=hash_get_mem (sm->policy_index_by_policy_name, a->policy_name);
        }
       vec_add1 (pt->tunnel_indices, t - sm->tunnels);
-      t->policy_index = p[0]; /* equiv. to (pt - sm->policies) */
+      if (p == 0)
+        clib_warning ("p is NULL!");
+      t->policy_index = p ? p[0] : ~0; /* equiv. to (pt - sm->policies) */
     }
 
   if (a->name)
index ccc0146..f059ce9 100644 (file)
@@ -304,17 +304,17 @@ sr_replicate_node_fn (vlib_main_t * vm,
               sr_replicate_trace_t *tr = vlib_add_trace (vm, node,
                                                        b0, sizeof (*tr));
               tr->tunnel_index = t0 - sm->tunnels;
+              tr->length = 0;
              if (hdr_ip0)
                {
                  memcpy (tr->src.as_u8, hdr_ip0->src_address.as_u8,
                       sizeof (tr->src.as_u8));
                  memcpy (tr->dst.as_u8, hdr_ip0->dst_address.as_u8,
                       sizeof (tr->dst.as_u8));
+                  if (hdr_ip0->payload_length)
+                      tr->length = clib_net_to_host_u16
+                          (hdr_ip0->payload_length);
                }
-             if (hdr_ip0->payload_length)
-               tr->length = clib_net_to_host_u16(hdr_ip0->payload_length);
-             else
-               tr->length = 0;
               tr->next_index = next_index;
               memcpy (tr->sr, hdr_sr0, sizeof (tr->sr));
             }
index 667e3e4..7e2ee06 100644 (file)
@@ -823,7 +823,8 @@ int vnet_tap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
 
  error:
   close (dev_net_tun_fd);
-  close (dev_tap_fd);
+  if (dev_tap_fd >= 0)
+      close (dev_tap_fd);
 
   return rv;
 }
index 28b2584..f24d71e 100644 (file)
@@ -427,7 +427,8 @@ tuntap_exit (vlib_main_t * vm)
   if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0)
     clib_unix_warning ("TUNSETPERSIST");
   close(tm->dev_tap_fd);
-  close(tm->dev_net_tun_fd);
+  if (tm->dev_net_tun_fd >= 0)
+      close(tm->dev_net_tun_fd);
   close (sfd);
 
   return 0;
@@ -809,7 +810,8 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im,
       if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0)
         clib_unix_warning ("del address");
 
-      close (sockfd);
+      if (sockfd >= 0)
+        close (sockfd);
 
       mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */);
       pool_put (tm->subifs, ap);
index 34bcccd..7f32cbd 100644 (file)
@@ -73,7 +73,10 @@ vxlan_gpe_input (vlib_main_t * vm,
   u32 cpu_index = os_get_cpu_number ();
   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
 
-  memset (&last_key4, 0xff, sizeof(last_key4));
+  if (is_ip4)
+      memset (&last_key4, 0xff, sizeof(last_key4));
+  else
+      memset (&last_key6, 0xff, sizeof(last_key6));
 
   from = vlib_frame_vector_args (from_frame);
   n_left_from = from_frame->n_vectors;
index dc9a2ff..899b331 100644 (file)
@@ -64,7 +64,10 @@ vxlan_input (vlib_main_t * vm,
   u32 cpu_index = os_get_cpu_number();
   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
 
-  last_key4.as_u64 = ~0;
+  if (is_ip4)
+    last_key4.as_u64 = ~0;
+  else
+    memset (&last_key6, 0xff, sizeof (last_key6));
 
   from = vlib_frame_vector_args (from_frame);
   n_left_from = from_frame->n_vectors;
index 32b6d03..b9f717b 100644 (file)
@@ -67,6 +67,7 @@ virtual_ip_cmd_fn_command_fn (vlib_main_t * vm,
         ip_adjacency_t adj;
         u32 adj_index;
         
+        memset(&adj, 0, sizeof(adj));
         adj.lookup_next_index = IP_LOOKUP_NEXT_REWRITE;
         
         vnet_rewrite_for_sw_interface
index d7d8cba..88744ff 100644 (file)
@@ -390,7 +390,8 @@ int main (int argc, char **argv)
     }
     else {
         fclose (ifp);
-        fclose (ofp);
+        if (ofp)
+            fclose (ofp);
         if (ofile) {
             printf ("Removing %s\n", ofile);
             unlink (ofile);
@@ -426,6 +427,12 @@ void yyerror (char *s)
 
 static char namebuf [MAXNAME];
 
+static inline char
+getc_char (FILE *ifp)
+{
+    return ((char)(getc(ifp) & 0x7f));
+}
+
 /*
  * yylex (well, yylex_1: The real yylex below does crc-hackery)
  */
@@ -433,7 +440,6 @@ static int yylex_1 (void)
 {
     int nameidx=0;
     char c;
-    int at_bol=1;
     enum { LP_INITIAL_WHITESPACE, LP_LINE_NUMBER,
           LP_PRE_FILENAME_WHITESPACE, LP_FILENAME,
           LP_POST_FILENAME,
@@ -446,23 +452,16 @@ static int yylex_1 (void)
          * START state -- looking for something interesting 
          */
     case START_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
 
         switch (c) {
         case '\n':
             the_lexer_linenumber++;
-            at_bol=1;
             goto again;
 
         case '#':
-            if (!at_bol) {
-                fprintf (stderr, "unknown token /%c at line %d\n",
-                         c, the_lexer_linenumber);
-                return (BARF);
-            }
-            
             the_lexer_state = LINE_PRAGMA_STATE;
             lp_substate = LP_INITIAL_WHITESPACE;
             goto again;
@@ -507,7 +506,7 @@ static int yylex_1 (void)
             goto again;
 
         case '/':
-            c = getc (ifp);
+            c = getc_char (ifp);
             if (feof (ifp))
                 return (EOF);
 
@@ -524,7 +523,7 @@ static int yylex_1 (void)
             }
 
         case '\\':
-            c = getc (ifp);
+            c = getc_char (ifp);
             if (feof (ifp))
                 return (EOF);
             
@@ -552,7 +551,7 @@ static int yylex_1 (void)
          * NAME state -- eat the rest of a name 
          */
     case NAME_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         
@@ -573,7 +572,7 @@ static int yylex_1 (void)
          * NUMBER state -- eat the rest of a number
          */
     case NUMBER_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         
@@ -595,11 +594,11 @@ static int yylex_1 (void)
          * C_COMMENT state -- eat a peach
          */
     case C_COMMENT_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         if (c == '*') {
-            c = getc (ifp);
+            c = getc_char (ifp);
             if (feof (ifp))
                 return (EOF);
             if (c == '/') {
@@ -616,7 +615,7 @@ static int yylex_1 (void)
          */
 
     case CPP_COMMENT_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         if (c == '\n') {
@@ -627,12 +626,12 @@ static int yylex_1 (void)
         goto again;
 
     case STRING_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         switch (c) {
         case '\\':
-            c = getc (ifp);
+            c = getc_char (ifp);
             if (feof (ifp))
                 return (EOF);
             namebuf[nameidx++] = c;
@@ -658,12 +657,12 @@ static int yylex_1 (void)
         break;
 
     case HELPER_STATE:
-        c = getc (ifp);
+        c = getc_char (ifp);
         if (feof (ifp))
             return (EOF);
         switch (c) {
         case '\\':
-            c = getc (ifp);
+            c = getc_char (ifp);
             if (feof (ifp))
                 return (EOF);
             namebuf[nameidx] = c;
@@ -703,7 +702,7 @@ static int yylex_1 (void)
         switch (lp_substate) {
 
         case LP_INITIAL_WHITESPACE: /* no number seen yet */
-            c = getc(ifp);
+            c = getc_char(ifp);
             if (feof(ifp))
                 return(EOF);
             if (c >= '0' && c <= '9') {
@@ -719,7 +718,7 @@ static int yylex_1 (void)
            goto again;
 
         case LP_LINE_NUMBER:   /* eating linenumber */
-            c = getc(ifp);
+            c = getc_char(ifp);
             if (feof(ifp))
                 return(EOF);
             if (c >= '0' && c <= '9') {
@@ -736,7 +735,7 @@ static int yylex_1 (void)
             goto again;
 
         case LP_PRE_FILENAME_WHITESPACE: /* awaiting filename */
-            c = getc(ifp);
+            c = getc_char(ifp);
             if (feof(ifp))
                 return(EOF);
             
@@ -753,7 +752,7 @@ static int yylex_1 (void)
             goto again;
 
         case LP_FILENAME:      /* eating filename */
-            c = getc(ifp);
+            c = getc_char(ifp);
             if (feof(ifp))
                 return(EOF);
 
@@ -769,7 +768,7 @@ static int yylex_1 (void)
 
         case LP_POST_FILENAME: /* ignoring rest of line */
         case LP_OTHER:
-            c = getc(ifp);
+            c = getc_char(ifp);
             if (feof(ifp))
                 return(EOF);
 
@@ -789,7 +788,6 @@ static int yylex_1 (void)
                }
            lp_end_of_line:
                 the_lexer_state = START_STATE;
-                at_bol = 1;
                 nameidx = 0;
             }
             goto again;
@@ -986,6 +984,11 @@ char *sxerox (const char *s)
     char *rv;
 
     rv = (char *) malloc (len+1);
+    if (rv == 0) {
+        fprintf(stderr, "Out of memory...");
+        exit (1);
+    }
+        
     strcpy (rv, s);
     return (rv);
 }
index ab8adc5..e66fdce 100644 (file)
@@ -1034,7 +1034,7 @@ char *fixup_input_filename(void)
     if (*cp == '/')
         cp++;
 
-    strcpy (tmpbuf, cp);
+    strncpy (tmpbuf, cp, sizeof(tmpbuf)-1);
 
     cp = tmpbuf;
 
index f5d70b5..d9d3704 100644 (file)
@@ -358,10 +358,12 @@ set_interpreter_rpath (elf_tool_main_t * tm)
     }
 
  done:
-  if (mmap_length > 0)
+  if (mmap_length > 0 && idp)
     munmap (idp, mmap_length);
-  close (ifd);
-  close (ofd);
+  if (ifd >= 0)
+    close (ifd);
+  if (ofd >= 0)
+    close (ofd);
   return error;
 }
 
@@ -408,7 +410,10 @@ int main (int argc, char * argv[])
     }
 
   if (! tm->input_file)
-    clib_error ("no input file");
+    {
+      error = clib_error_return (0, "no input file");
+      goto done;
+    }
 
   /* Do the typical case a stone-simple way... */
   if (tm->quiet && tm->set_interpreter && tm->set_rpath && tm->output_file)
index d89739c..16e41c2 100644 (file)
@@ -1736,6 +1736,7 @@ static u8 * format_x86_reg_operand (u8 * s, va_list * va)
     {
     default:
       ASSERT (0);
+      break;
 
     case 'x':
       ASSERT (reg < 16);
@@ -1816,6 +1817,7 @@ static u8 * format_x86_insn_operand (u8 * s, va_list * va)
     /* Memory or reg field from modrm byte. */
     case 'M':
       ASSERT (p->flags & X86_INSN_IS_ADDRESS);
+      /* FALLTHROUGH */
     case 'E':
       if (p->flags & X86_INSN_IS_ADDRESS)
        s = format (s, "%U", format_x86_mem_operand, p);
@@ -1836,7 +1838,7 @@ static u8 * format_x86_insn_operand (u8 * s, va_list * va)
     case 'I':
       {
        u32 l = x86_insn_log2_immediate_bytes (p, insn);
-       i64 mask = pow2_mask (8 << l);
+       i64 mask = pow2_mask (8ULL << l);
        s = format (s, "$0x%Lx", p->immediate & mask);
       }
       break;
index 35de1b4..17b72ac 100644 (file)
@@ -196,7 +196,7 @@ clib_bitmap_get_multiple (uword * bitmap, uword i, uword n_bits)
   uword i0, i1, result;
   uword l = vec_len (bitmap);
 
-  ASSERT (n_bits >= 0 && n_bits <= BITS (result));
+  ASSERT (n_bits <= BITS (result));
 
   i0 = i / BITS (bitmap[0]);
   i1 = i % BITS (bitmap[0]);
@@ -229,7 +229,7 @@ clib_bitmap_set_multiple (uword * bitmap, uword i, uword value, uword n_bits)
 {
   uword i0, i1, l, t, m;
 
-  ASSERT (n_bits >= 0 && n_bits <= BITS (value));
+  ASSERT (n_bits <= BITS (value));
 
   i0 = i / BITS (bitmap[0]);
   i1 = i % BITS (bitmap[0]);
index 7fe3048..71ae1d3 100644 (file)
@@ -559,7 +559,8 @@ format_elf_main (u8 * s, va_list * args)
     s = format (s, "\nSections %d at file offset 0x%Lx-0x%Lx:\n",
                fh->section_header_count,
                fh->section_header_file_offset,
-               fh->section_header_file_offset + fh->section_header_count * fh->section_header_size);
+               fh->section_header_file_offset + 
+                (u64) fh->section_header_count * fh->section_header_size);
     s = format (s, "%U\n", format_elf_section, em, 0);
     vec_foreach (h, copy)
       s = format (s, "%U\n", format_elf_section, em, h);
@@ -1604,7 +1605,7 @@ static void layout_sections (elf_main_t * em)
 
       fh->section_header_file_offset = file_offset;
       fh->section_header_count = vec_len (em->sections) - n_deleted_sections;
-      file_offset += fh->section_header_count * fh->section_header_size;
+      file_offset += (u64) fh->section_header_count * fh->section_header_size;
     }
 
     {
@@ -1809,7 +1810,11 @@ clib_error_t * elf_write_file (elf_main_t * em, char * file_name)
          continue;
 
        if (fseek (f, s->header.file_offset, SEEK_SET) < 0)
-         return clib_error_return_unix (0, "fseek 0x%Lx", s->header.file_offset);
+          {
+            fclose(f);
+            return clib_error_return_unix (0, "fseek 0x%Lx", 
+                                           s->header.file_offset);
+          }
 
        if (s->header.type == ELF_SECTION_NO_BITS)
          /* don't write for .bss sections */;
index f3d3b32..8c70548 100644 (file)
@@ -82,6 +82,9 @@ path_search (char * file)
   if (file[0] == '.' || file[0] == '/')
     return file;
 
+  if (getenv("PATH") == 0)
+    return file;
+
   ps.path = split_string (getenv ("PATH"), ':');
 
   for (i = 0; i < vec_len (ps.path); i++)
@@ -231,7 +234,10 @@ add_section (struct dl_phdr_info * info, size_t size, void * opaque)
 
       name = path_search (cem->exec_path);
       if (! name)
-       clib_error ("failed to find %s on PATH", cem->exec_path);
+        {
+          clib_error ("failed to find %s on PATH", cem->exec_path);
+          return 0;
+        }
       addr = 0;
     }
 
index 06b97d8..7ae4ea1 100644 (file)
@@ -582,6 +582,8 @@ void elog_merge (elog_main_t * dst, u8 * dst_tag,
   elog_track_t newt;
   int i;
 
+  memset(&newt, 0, sizeof (newt));
+
   elog_get_events (src);
   elog_get_events (dst);