misc: fix coverity warnings 14/25214/3
authorDave Barach <dave@barachs.net>
Mon, 17 Feb 2020 14:13:26 +0000 (09:13 -0500)
committerFlorin Coras <florin.coras@gmail.com>
Mon, 17 Feb 2020 17:18:18 +0000 (17:18 +0000)
Add an ALWAYS_ASSERT (...) macro, to (a) shut up coverity, and (b)
check the indicated condition in production images.

As in:
 p = hash_get(...);
 ALWAYS_ASSERT(p) /* was ASSERT(p) */
 elt = pool_elt_at_index(pool, p[0]);

This may not be the best way to handle a specific case, but failure to
check return values at all followed by e.g. a pointer dereference
isn't ok.

Type: fix
Ticket: VPP-1837
Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: Ia97c641cefcfb7ea7d77ea5a55ed4afea0345acb

src/plugins/gtpu/gtpu.c
src/vnet/dpo/load_balance_map.c
src/vnet/fib/fib_attached_export.c
src/vnet/geneve/encap.c
src/vnet/geneve/geneve.c
src/vnet/ipsec/ipsec_output.c
src/vnet/session/application_worker.c
src/vnet/vxlan-gbp/vxlan_gbp.c
src/vnet/vxlan-gpe/vxlan_gpe.c
src/vnet/vxlan/vxlan.c
src/vppinfra/error_bootstrap.h

index 905ca5e..b0d7839 100644 (file)
@@ -318,7 +318,7 @@ vtep_addr_unref (ip46_address_t * ip)
   uword *vtep = ip46_address_is_ip4 (ip) ?
     hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
     hash_get_mem (gtpu_main.vtep6, &ip->ip6);
-  ASSERT (vtep);
+  ALWAYS_ASSERT (vtep);
   if (--(*vtep) != 0)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
index bf5d1fb..7da360b 100644 (file)
@@ -274,7 +274,7 @@ load_balance_map_db_remove (load_balance_map_t *lbm)
     {
         p = hash_get(lb_maps_by_path_index, lbmp->lbmp_index);
 
-        ASSERT(NULL != p);
+        ALWAYS_ASSERT(NULL != p);
 
         fib_node_list_remove(p[0], lbmp->lbmp_sibling);
     }
@@ -312,7 +312,7 @@ load_balance_map_fill (load_balance_map_t *lbm)
                 tmp_buckets[jj++] = bucket++;
             }
         }
-        else 
+        else
         {
             bucket += lbmp->lbmp_weight;
         }
index 3c9c2bf..4a33b88 100644 (file)
@@ -364,7 +364,7 @@ fib_attached_export_purge (fib_entry_t *fib_entry)
 
             fed = fib_entry_delegate_find(export_entry,
                                           FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
-            ASSERT(NULL != fed);
+            ALWAYS_ASSERT(NULL != fed);
 
            export = pool_elt_at_index(fib_ae_export_pool, fed->fd_index);
 
@@ -391,7 +391,7 @@ fib_attached_export_purge (fib_entry_t *fib_entry)
        pool_put(fib_ae_import_pool, import);
         fib_entry_delegate_remove(fib_entry,
                                   FIB_ENTRY_DELEGATE_ATTACHED_IMPORT);
-    }  
+    }
 }
 
 void
@@ -522,7 +522,7 @@ fib_ae_import_format (fib_node_index_t impi,
     s = format(s, "export-sibling:%d ", import->faei_export_sibling);
     s = format(s, "exporter:%d ", import->faei_exporter);
     s = format(s, "export-fib:%d ", import->faei_export_fib);
+
     s = format(s, "import-entry:%d ", import->faei_import_entry);
     s = format(s, "import-fib:%d ", import->faei_import_fib);
 
@@ -544,7 +544,7 @@ fib_ae_export_format (fib_node_index_t expi,
     fib_ae_export_t *export;
 
     export = pool_elt_at_index(fib_ae_export_pool, expi);
-    
+
     s = format(s, "\n  Attached-Export:%d:[", (export - fib_ae_export_pool));
     s = format(s, "export-entry:%d ", export->faee_ei);
 
index af52fd2..2859a9a 100644 (file)
@@ -131,7 +131,7 @@ geneve_encap_inline (vlib_main_t * vm,
              next0 = t0->next_dpo.dpoi_next_node;
            }
 
-         ASSERT (t0 != NULL);
+         ALWAYS_ASSERT (t0 != NULL);
 
          vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
 
@@ -145,7 +145,7 @@ geneve_encap_inline (vlib_main_t * vm,
              next1 = t1->next_dpo.dpoi_next_node;
            }
 
-         ASSERT (t1 != NULL);
+         ALWAYS_ASSERT (t1 != NULL);
 
          vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index;
 
@@ -378,6 +378,9 @@ geneve_encap_inline (vlib_main_t * vm,
              /* Note: change to always set next0 if it may be set to drop */
              next0 = t0->next_dpo.dpoi_next_node;
            }
+
+         ALWAYS_ASSERT (t0 != NULL);
+
          vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
 
          /* Apply the rewrite string. $$$$ vnet_rewrite? */
index 0c551a0..a54a538 100644 (file)
@@ -312,7 +312,7 @@ vtep_addr_unref (ip46_address_t * ip)
   uword *vtep = ip46_address_is_ip4 (ip) ?
     hash_get (geneve_main.vtep4, ip->ip4.as_u32) :
     hash_get_mem (geneve_main.vtep6, &ip->ip6);
-  ASSERT (vtep);
+  ALWAYS_ASSERT (vtep);
   if (--(*vtep) != 0)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
index 2ba965a..d09a027 100644 (file)
@@ -222,7 +222,7 @@ ipsec_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
       if (PREDICT_FALSE (last_sw_if_index != sw_if_index0))
        {
          uword *p = hash_get (im->spd_index_by_sw_if_index, sw_if_index0);
-         ASSERT (p);
+         ALWAYS_ASSERT (p);
          spd_index0 = p[0];
          spd0 = pool_elt_at_index (im->spds, spd_index0);
          last_sw_if_index = sw_if_index0;
index 1db6baa..0b67d29 100644 (file)
@@ -468,7 +468,7 @@ app_worker_get_listen_segment_manager (app_worker_t * app,
 {
   uword *smp;
   smp = hash_get (app->listeners_table, listen_session_get_handle (listener));
-  ASSERT (smp != 0);
+  ALWAYS_ASSERT (smp != 0);
   return segment_manager_get (*smp);
 }
 
index 80eecb4..9a8158e 100644 (file)
@@ -291,7 +291,7 @@ vtep_addr_unref (ip46_address_t * ip)
   uword *vtep = ip46_address_is_ip4 (ip) ?
     hash_get (vxlan_gbp_main.vtep4, ip->ip4.as_u32) :
     hash_get_mem (vxlan_gbp_main.vtep6, &ip->ip6);
-  ASSERT (vtep);
+  ALWAYS_ASSERT (vtep);
   if (--(*vtep) != 0)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
index 86157c0..e5ff4f7 100644 (file)
@@ -404,7 +404,7 @@ vtep_addr_unref (ip46_address_t * ip)
   uword *vtep = ip46_address_is_ip4 (ip) ?
     hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
     hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
-  ASSERT (vtep);
+  ALWAYS_ASSERT (vtep);
   if (--(*vtep) != 0)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
index 2b85251..3264749 100644 (file)
@@ -311,7 +311,7 @@ vtep_addr_unref (ip46_address_t * ip)
   uword *vtep = ip46_address_is_ip4 (ip) ?
     hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
     hash_get_mem (vxlan_main.vtep6, &ip->ip6);
-  ASSERT (vtep);
+  ALWAYS_ASSERT (vtep);
   if (--(*vtep) != 0)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
@@ -337,7 +337,7 @@ mcast_shared_get (ip46_address_t * ip)
 {
   ASSERT (ip46_address_is_multicast (ip));
   uword *p = hash_get_mem (vxlan_main.mcast_shared, ip);
-  ASSERT (p);
+  ALWAYS_ASSERT (p);
   mcast_shared_t ret = {.as_u64 = *p };
   return ret;
 }
index 1cd9105..8dbbc7f 100644 (file)
@@ -79,6 +79,35 @@ do {                                                 \
     }                                                  \
 } while (0)
 
+/*
+ * This version always generates code, and has a Coverity-specific
+ * version to stop Coverity complaining about
+ * ALWAYS_ASSERT(p != 0); p->member...
+ */
+
+#ifndef __COVERITY__
+#define ALWAYS_ASSERT(truth)                           \
+do {                                                   \
+  if (PREDICT_FALSE(!(truth)))                          \
+    {                                                  \
+      _clib_error (CLIB_ERROR_ABORT, 0, 0,             \
+                  "%s:%d (%s) assertion `%s' fails",   \
+                  __FILE__,                            \
+                  (uword) __LINE__,                    \
+                  clib_error_function,                 \
+                  # truth);                            \
+    }                                                  \
+} while (0)
+#else /* __COVERITY__ */
+#define ALWAYS_ASSERT(truth)                    \
+do {                                            \
+  if (PREDICT_FALSE(!(truth)))                  \
+    {                                           \
+      abort();                                  \
+    }                                           \
+} while (0)
+#endif /* __COVERITY */
+
 #if defined(__clang__)
 #define STATIC_ASSERT(truth,...)
 #else