vat2: coverity errors in print_template 28/33628/2
authorOle Troan <ot@cisco.com>
Tue, 31 Aug 2021 12:21:03 +0000 (14:21 +0200)
committerDamjan Marion <dmarion@me.com>
Tue, 31 Aug 2021 15:26:29 +0000 (15:26 +0000)
Dereferencing null pointer fix.
Add checking of return values for all calls in print_template()

Type: fix
Signed-off-by: Ole Troan <ot@cisco.com>
Change-Id: I00073b29ab2e76d5d06af9bd3f5ae2846de4d46d

src/vat2/main.c

index b3606f3..208e0c5 100644 (file)
@@ -151,21 +151,36 @@ print_template (char *msgname)
 {
   uword *p = hash_get_mem (function_by_name, msgname);
   if (!p)
-    {
-      fprintf (stderr, "no such message: %s", msgname);
-    }
+    goto error;
+
   cJSON *(*fp) (void *);
   fp = (void *) apifuncs[p[0]].tojson;
-  assert (fp);
+  if (!fp)
+    goto error;
+
   void *scratch = malloc (2048);
+  if (!scratch)
+    goto error;
+
   memset (scratch, 0, 2048);
   cJSON *t = fp (scratch);
+  if (!t)
+    goto error;
   free (scratch);
   char *output = cJSON_Print (t);
+  if (!output)
+    goto error;
+
   cJSON_Delete (t);
   printf ("%s\n", output);
   free (output);
+
+  return;
+
+error:
+  fprintf (stderr, "error printing template for: %s\n", msgname);
 }
+
 static void
 dump_apis (void)
 {