vat2: jsonconvert return checking - coverity 24/31324/3
authorOle Troan <ot@cisco.com>
Wed, 17 Feb 2021 13:10:04 +0000 (14:10 +0100)
committerNeale Ranns <neale@graphiant.com>
Thu, 18 Feb 2021 08:18:46 +0000 (08:18 +0000)
Type: fix
Signed-off-by: Ole Troan <ot@cisco.com>
Change-Id: I8348645927519800d2390d27e01fae612602a6eb

src/vat2/jsonconvert.c
src/vat2/jsonconvert.h
src/vat2/test/vat2_test.api
src/vat2/test/vat2_test.c

index d62cda1..645f7d4 100644 (file)
@@ -270,7 +270,8 @@ vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
     if (!p)
       return -1;
     unformat_init_string (&input, p, strlen(p));
-    unformat(&input, "%U", unformat_ip4_address, a);
+    if (!unformat (&input, "%U", unformat_ip4_address, a))
+      return -1;
     return 0;
 }
 
@@ -283,7 +284,9 @@ vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
     if (!p)
       return -1;
     unformat_init_string (&input, p, strlen(p));
-    unformat(&input, "%U/%d", unformat_ip4_address, &a->address, &a->len);
+    if (!unformat (&input, "%U/%d", unformat_ip4_address, &a->address,
+                  &a->len))
+      return -1;
     return 0;
 }
 
@@ -302,7 +305,8 @@ vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
     if (!p)
       return -1;
     unformat_init_string (&input, p, strlen(p));
-    unformat(&input, "%U", unformat_ip6_address, a);
+    if (!unformat (&input, "%U", unformat_ip6_address, a))
+      return -1;
     return 0;
 }
 
@@ -315,7 +319,8 @@ vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
   if (!p)
     return -1;
   unformat_init_string (&input, p, strlen(p));
-  unformat(&input, "%U/%d", unformat_ip6_address, &a->address, &a->len);
+  if (!unformat (&input, "%U/%d", unformat_ip6_address, &a->address, &a->len))
+    return -1;
   return 0;
 }
 
@@ -408,7 +413,8 @@ vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
 
   char *p = cJSON_GetStringValue(o);
   unformat_init_string (&input, p, strlen(p));
-  unformat(&input, "%U", unformat_mac_address, a);
+  if (!unformat (&input, "%U", unformat_mac_address, a))
+    return -1;
   return 0;
 }
 
@@ -438,16 +444,6 @@ format_vl_api_interface_index_t (u8 *s, va_list *args)
   return format (s, "%u", *a);
 }
 
-uword
-unformat_vl_api_interface_index_t (unformat_input_t * input, va_list * args)
-{
-    u32 *a = va_arg (*args, u32 *);
-
-    if (!unformat (input, "%u", a))
-        return 0;
-    return 1;
-}
-
 void
 vl_api_string_cJSON_AddToObject(cJSON * const object, const char * const name, vl_api_string_t *astr)
 {
index ee1355f..038ad74 100644 (file)
 
 #define _(T)                                    \
   int vl_api_ ##T## _fromjson(cJSON *o, T *d);
-  foreach_vat2_fromjson
+foreach_vat2_fromjson
 #undef _
 
-int vl_api_bool_fromjson(cJSON *o, bool *d);
-  int vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
-                                    vl_api_ip4_address_t *a);
-  int vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                                   vl_api_ip4_prefix_t *a);
-  int vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                                                vl_api_ip4_prefix_t *a);
-  int vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
-                                    vl_api_ip6_address_t *a);
-  int vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                                   vl_api_ip6_prefix_t *a);
-  int vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                                                vl_api_ip6_prefix_t *a);
-  int vl_api_address_t_fromjson (void **mp, int *len, cJSON *o,
-                                vl_api_address_t *a);
-  int vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                               vl_api_prefix_t *a);
-  int vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
-                                            vl_api_prefix_t *a);
-  int vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
-                                    vl_api_mac_address_t *a);
+  /* Prototypes */
+  int
+  vl_api_bool_fromjson (cJSON *o, bool *d);
+int vl_api_ip4_address_t_fromjson (void **mp, int *len, cJSON *o,
+                                  vl_api_ip4_address_t *a);
+int vl_api_ip4_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                                 vl_api_ip4_prefix_t *a);
+int vl_api_ip4_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                                              vl_api_ip4_prefix_t *a);
+int vl_api_ip6_address_t_fromjson (void **mp, int *len, cJSON *o,
+                                  vl_api_ip6_address_t *a);
+int vl_api_ip6_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                                 vl_api_ip6_prefix_t *a);
+int vl_api_ip6_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                                              vl_api_ip6_prefix_t *a);
+int vl_api_address_t_fromjson (void **mp, int *len, cJSON *o,
+                              vl_api_address_t *a);
+int vl_api_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                             vl_api_prefix_t *a);
+int vl_api_address_with_prefix_t_fromjson (void **mp, int *len, cJSON *o,
+                                          vl_api_prefix_t *a);
+int vl_api_mac_address_t_fromjson (void **mp, int *len, cJSON *o,
+                                  vl_api_mac_address_t *a);
 
-  uword unformat_ip4_address (unformat_input_t *input, va_list *args);
-  uword unformat_ip6_address (unformat_input_t *input, va_list *args);
-  u8 *format_ip6_address (u8 *s, va_list *args);
-  uword unformat_mac_address (unformat_input_t *input, va_list *args);
-  u8 *format_ip4_address (u8 *s, va_list *args);
-  u8 *format_vl_api_interface_index_t (u8 *s, va_list *args);
-  uword unformat_vl_api_interface_index_t (unformat_input_t *input,
-                                          va_list *args);
-  u8 *format_vl_api_timestamp_t (u8 *s, va_list *args);
-  u8 *format_vl_api_timedelta_t (u8 *s, va_list *args);
-  uword unformat_vl_api_timedelta_t (unformat_input_t *input, va_list *args);
-  uword unformat_vl_api_timestamp_t (unformat_input_t *input, va_list *args);
-  u8 *format_vl_api_gbp_scope_t (u8 *s, va_list *args);
-  uword unformat_vl_api_gbp_scope_t (unformat_input_t *input, va_list *args);
+uword unformat_ip4_address (unformat_input_t *input, va_list *args);
+uword unformat_ip6_address (unformat_input_t *input, va_list *args);
+u8 *format_ip6_address (u8 *s, va_list *args);
+uword unformat_mac_address (unformat_input_t *input, va_list *args);
+u8 *format_ip4_address (u8 *s, va_list *args);
+u8 *format_vl_api_interface_index_t (u8 *s, va_list *args);
+u8 *format_vl_api_timestamp_t (u8 *s, va_list *args);
+u8 *format_vl_api_timedelta_t (u8 *s, va_list *args);
+uword unformat_vl_api_timedelta_t (unformat_input_t *input, va_list *args);
+uword unformat_vl_api_timestamp_t (unformat_input_t *input, va_list *args);
+u8 *format_vl_api_gbp_scope_t (u8 *s, va_list *args);
+uword unformat_vl_api_gbp_scope_t (unformat_input_t *input, va_list *args);
 
-  int vl_api_c_string_to_api_string (const char *buf, vl_api_string_t *str);
-  void vl_api_string_cJSON_AddToObject (cJSON *const object,
-                                       const char *const name,
-                                       vl_api_string_t *astr);
+int vl_api_c_string_to_api_string (const char *buf, vl_api_string_t *str);
+void vl_api_string_cJSON_AddToObject (cJSON *const object,
+                                     const char *const name,
+                                     vl_api_string_t *astr);
 
-  u8 *u8string_fromjson (cJSON *o, char *fieldname);
-  int u8string_fromjson2 (cJSON *o, char *fieldname, u8 *data);
-  int vl_api_u8_string_fromjson (cJSON *o, u8 *s, int len);
+u8 *u8string_fromjson (cJSON *o, char *fieldname);
+int u8string_fromjson2 (cJSON *o, char *fieldname, u8 *data);
+int vl_api_u8_string_fromjson (cJSON *o, u8 *s, int len);
 
 #define foreach_vat2_tojson                     \
   _(ip4_address)                                \
index afc8afa..6d21155 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 import "vnet/ip/ip_types.api";
+import "vnet/interface_types.api";
 
 autoreply define test_prefix {
   vl_api_prefix_t pref;
@@ -94,3 +95,8 @@ autoreply define test_empty {
   u32 client_index;
   u32 context;
 };
+
+/* Interface */
+autoreply define test_interface {
+  vl_api_interface_index_t sw_if_index;
+};
index 89714c4..1ac4652 100644 (file)
@@ -162,6 +162,11 @@ struct msgs msgs[] = {
     .tojson = (tojson_fn_t) vl_api_test_empty_t_tojson,
     .fromjson = (fromjson_fn_t) vl_api_test_empty_t_fromjson,
   },
+  {
+    .name = "test_interface",
+    .tojson = (tojson_fn_t) vl_api_test_interface_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_interface_t_fromjson,
+  },
 };
 
 struct tests tests[] = {
@@ -190,6 +195,7 @@ struct tests tests[] = {
   { .s = "{\"_msgname\": \"test_addresses3\", \"n\": 2, \"a\": "
         "[\"2001:db8::23\", \"2001:db8::23\"] }" },
   { .s = "{\"_msgname\": \"test_empty\"}" },
+  { .s = "{\"_msgname\": \"test_interface\", \"sw_if_index\": 100 }" },
 };
 
 int main (int argc, char **argv)