vppapigen: coverity issues in autogenerated code pass 3. 45/31245/2
authorOle Troan <ot@cisco.com>
Fri, 12 Feb 2021 10:48:12 +0000 (11:48 +0100)
committerNeale Ranns <neale@graphiant.com>
Fri, 12 Feb 2021 14:51:41 +0000 (14:51 +0000)
Type: fix
Signed-off-by: Ole Troan <ot@cisco.com>
Change-Id: I5ee2e8aba3ee7281bbca11825dece79983e52f06

src/tools/vppapigen/vppapigen_c.py
src/vat2/test/vat2_test.api
src/vat2/test/vat2_test.c

index e7045db..9b16413 100644 (file)
@@ -393,8 +393,8 @@ class FromJSON():
         cJSON *array = cJSON_GetObjectItem(o, "{n}");
         int size = cJSON_GetArraySize(array);
         {lfield} = size;
-        *{msgvar} = realloc({msgvar}, {msgsize} + sizeof({t}) * size);
-        {t} *d = (void *){msgvar} + {msgsize};
+        {realloc} = realloc({realloc}, {msgsize} + sizeof({t}) * size);
+        {t} *d = (void *){realloc} + {msgsize};
         {msgsize} += sizeof({t}) * size;
         for (i = 0; i < size; i++) {{
             cJSON *e = cJSON_GetArrayItem(array, i);
@@ -409,6 +409,7 @@ class FromJSON():
 
         lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
         msgvar = "(void **)&a" if toplevel else "mp"
+        realloc = "a" if toplevel else "*mp"
         msgsize = "l" if toplevel else "*len"
 
         if o.fieldtype == 'u8':
@@ -418,10 +419,10 @@ class FromJSON():
                 write('    if (!s) return 0;\n')
                 write('    {} = vec_len(s);\n'.format(lfield))
 
-                write('    *{msgvar} = realloc({msgvar}, {msgsize} + '
-                      'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
-                write('    memcpy((void *){msgvar} + {msgsize}, s, '
-                      'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize))
+                write('    {realloc} = realloc({realloc}, {msgsize} + '
+                      'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc))
+                write('    memcpy((void *){realloc} + {msgsize}, s, '
+                      'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize))
                 write('    {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))
 
                 write('    vec_free(s);\n')
@@ -443,7 +444,7 @@ class FromJSON():
                                      t=o.fieldtype,
                                      n=o.fieldname,
                                      call=call,
-                                     msgvar=msgvar,
+                                     realloc=realloc,
                                      msgsize=msgsize))
         else:
             if is_bt:
@@ -457,6 +458,7 @@ class FromJSON():
                                  n=o.fieldname,
                                  call=call,
                                  msgvar=msgvar,
+                                 realloc=realloc,
                                  msgsize=msgsize))
 
     _dispatch['Array'] = print_array
@@ -480,20 +482,20 @@ class FromJSON():
     def print_enum_flag(self, o):
         '''Convert to JSON enum(string) to VPP API enum (int)'''
         write = self.stream.write
-        write('static inline void *vl_api_{n}_t_fromjson '
-              '(void *mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
+        write('static inline int vl_api_{n}_t_fromjson '
+              '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
               .format(n=o.name))
         write('   int i;\n')
         write('   *a = 0;\n')
         write('   for (i = 0; i < cJSON_GetArraySize(o); i++) {\n')
         write('       cJSON *e = cJSON_GetArrayItem(o, i);\n')
         write('       char *p = cJSON_GetStringValue(e);\n')
-        write('       if (!p) return 0;\n')
+        write('       if (!p) return -1;\n')
         for b in o.block:
             write('       if (strcmp(p, "{}") == 0) *a |= {};\n'
                   .format(b[0], b[1]))
         write('    }\n')
-        write('   return mp;\n')
+        write('   return 0;\n')
         write('}\n')
 
     _dispatch['EnumFlag'] = print_enum_flag
@@ -513,7 +515,6 @@ class FromJSON():
             write('\n    item = cJSON_GetObjectItem(o, "{}");\n'
                   .format(t.fieldname))
             write('    if (!item) goto error;\n')
-
             self._dispatch[t.type](self, t)
 
         write('\n    return 0;\n')
@@ -580,7 +581,7 @@ class FromJSON():
             return
 
         t = o.using
-        write('static inline void *vl_api_{name}_t_fromjson (void *mp, '
+        write('static inline int vl_api_{name}_t_fromjson (void **mp, '
               'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
               .format(name=o.name))
         if 'length' in o.alias:
@@ -593,7 +594,7 @@ class FromJSON():
             write('    vl_api_{t}_fromjson(o, ({t} *)a);\n'
                   .format(t=t.fieldtype))
 
-        write('    return mp;\n')
+        write('    return 0;\n')
         write('}\n')
 
     _dispatch['Typedef'] = print_typedef
index fe96dd7..ff503f0 100644 (file)
@@ -36,6 +36,44 @@ typedef test_stringtype {
 autoreply define test_string {
   vl_api_test_stringtype_t str;
 };
+
 autoreply define test_string2 {
   string str[];
 };
+
+/* Test of toplevel VLA with basetype */
+autoreply define test_vla {
+  u32 count;
+  u32 vla[count];
+};
+
+/* Test of toplevel VLA with u8 */
+autoreply define test_vla2 {
+  u32 count;
+  u8 vla[count];
+};
+/* Test of toplevel VLA with user type */
+typedef test_vlatype {
+  u32 data;
+};
+autoreply define test_vla3 {
+  u32 count;
+  vl_api_test_vlatype_t vla[count];
+};
+/* Test of typedefed VLA */
+typedef test_vlatype2 {
+  u32 count;
+  u32 vla[count];
+};
+autoreply define test_vla4 {
+  vl_api_test_vlatype2_t data;
+};
+/* u8 string in typedef */
+typedef test_vlatype3 {
+  u32 count;
+  u8 vla[count];
+};
+autoreply define test_vla5 {
+  vl_api_test_vlatype3_t data;
+};
+
index b5346ee..10cfee4 100644 (file)
@@ -84,6 +84,7 @@ runtest (char *s, bool should_fail)
   assert(name);
 
   uword *p = hash_get_mem(function_by_name_tojson, name);
+  printf ("Message name: %s\n", name);
   assert(p);
   tojson_fn_t tojson = (tojson_fn_t)p[0];
 
@@ -116,6 +117,31 @@ struct msgs msgs[] = {
     .tojson = (tojson_fn_t) vl_api_test_string2_t_tojson,
     .fromjson = (fromjson_fn_t) vl_api_test_string2_t_fromjson,
   },
+  {
+    .name = "test_vla",
+    .tojson = (tojson_fn_t) vl_api_test_vla_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_vla_t_fromjson,
+  },
+  {
+    .name = "test_vla2",
+    .tojson = (tojson_fn_t) vl_api_test_vla2_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_vla2_t_fromjson,
+  },
+  {
+    .name = "test_vla3",
+    .tojson = (tojson_fn_t) vl_api_test_vla3_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_vla3_t_fromjson,
+  },
+  {
+    .name = "test_vla4",
+    .tojson = (tojson_fn_t) vl_api_test_vla4_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_vla4_t_fromjson,
+  },
+  {
+    .name = "test_vla5",
+    .tojson = (tojson_fn_t) vl_api_test_vla5_t_tojson,
+    .fromjson = (fromjson_fn_t) vl_api_test_vla5_t_fromjson,
+  },
 };
 
 struct tests tests[] = {
@@ -128,6 +154,15 @@ struct tests tests[] = {
         "type\"}}" },
   { .s =
       "{\"_msgname\": \"test_string2\", \"str\": \"Test string toplevel\"}" },
+  { .s = "{\"_msgname\": \"test_vla\", \"count\": 5, \"vla\": [1,2,3,4,5]}" },
+  { .s = "{\"_msgname\": \"test_vla2\", \"count\": 5, \"vla\": "
+        "\"0xaabbccddee\"}" },
+  { .s = "{\"_msgname\": \"test_vla3\", \"count\": 2, \"vla\": [{\"data\": 1} "
+        ", {\"data\": 2} ] }" },
+  { .s = "{\"_msgname\": \"test_vla4\", \"data\": { \"count\": 5, \"vla\": "
+        "[1,2,3,4,5] }}" },
+  { .s = "{\"_msgname\": \"test_vla5\", \"data\": { \"count\": 5, \"vla\": "
+        "\"0xaabbccddee\" }}" },
 };
 
 int main (int argc, char **argv)