Fix issues reported by coverity
[vpp.git] / vlib-api / vlibapi / api_shared.c
index 09502c6..2b2d81c 100644 (file)
@@ -781,7 +781,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
   if (!(statb.st_mode & S_IFREG) || (statb.st_size < sizeof (*hp)))
     {
       vlib_cli_output (vm, "File not plausible: %s\n", filename);
-      close(fd);
+      close (fd);
       return;
     }
 
@@ -847,6 +847,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       if (!cfgp)
        {
          vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
+         munmap (hp, file_size);
          return;
        }
       size = cfgp->size;
@@ -872,6 +873,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       if (!cfgp)
        {
          vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
+         munmap (hp, file_size);
          return;
        }
       size = cfgp->size;
@@ -894,6 +896,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
              || (am->msg_endian_handlers[msg_id] == 0))
            {
              vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
+             munmap (hp, file_size);
              return;
            }
          endian_fp = am->msg_endian_handlers[msg_id];
@@ -1083,8 +1086,22 @@ api_trace_command_fn (vlib_main_t * vm,
            }
          rv = vl_msg_api_trace_save (am, which, fp);
          fclose (fp);
-         if (rv < 0)
-           vlib_cli_output (vm, "ERROR: %d", rv);
+         if (rv == -1)
+           vlib_cli_output (vm, "API Trace data not present\n");
+         else if (rv == -2)
+           vlib_cli_output (vm, "File for writing is closed\n");
+         else if (rv == -10)
+           vlib_cli_output (vm, "Error while writing header to file\n");
+         else if (rv == -11)
+           vlib_cli_output (vm, "Error while writing trace to file\n");
+         else if (rv == -12)
+           vlib_cli_output (vm,
+                            "Error while writing end of buffer trace to file\n");
+         else if (rv == -13)
+           vlib_cli_output (vm,
+                            "Error while writing start of buffer trace to file\n");
+         else if (rv < 0)
+           vlib_cli_output (vm, "Unkown error while saving: %d", rv);
          else
            vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
          vec_free (chroot_filename);
@@ -1295,6 +1312,25 @@ vl_msg_api_get_msg_ids (char *name, int n)
   return rv;
 }
 
+void
+vl_msg_api_add_msg_name_crc (api_main_t * am, char *string, u32 id)
+{
+  uword *p;
+
+  if (am->msg_index_by_name_and_crc == 0)
+    am->msg_index_by_name_and_crc = hash_create_string (0, sizeof (uword));
+
+  p = hash_get_mem (am->msg_index_by_name_and_crc, string);
+  if (p)
+    {
+      clib_warning ("attempt to redefine '%s' ignored...", string);
+      return;
+    }
+
+  hash_set_mem (am->msg_index_by_name_and_crc, string, id);
+}
+
+
 /*
  * fd.io coding-style-patch-verification: ON
  *