vppapigen: ensure address types are nul terminated 52/41352/9
authorOle Troan <[email protected]>
Thu, 1 Aug 2024 12:06:24 +0000 (14:06 +0200)
committerBeno�t Ganne <[email protected]>
Wed, 7 Aug 2024 11:46:51 +0000 (11:46 +0000)
A string generated from format() may not be nul terminated.

Type: fix
Change-Id: I88452e446c3504d70758e9009c65be5466034d92
Signed-off-by: Ole Troan <[email protected]>
src/tools/vppapigen/vppapigen_c.py
src/vppinfra/format.c
src/vppinfra/format.h
src/vppinfra/jsonformat.c

index c2e1e7d..2cd3c79 100755 (executable)
@@ -171,14 +171,10 @@ class ToJSON:
             write("    {\n")
             # What is length field doing here?
             write(
-                '    u8 *s = format(0, "0x%U", format_hex_bytes, '
+                '    char *s = format_c_string(0, "0x%U", format_hex_bytes_no_wrap, '
                 "&a->{n}, {lfield});\n".format(n=o.fieldname, lfield=lfield)
             )
-            write(
-                '    cJSON_AddStringToObject(o, "{n}", (char *)s);\n'.format(
-                    n=o.fieldname
-                )
-            )
+            write('    cJSON_AddStringToObject(o, "{n}", s);\n'.format(n=o.fieldname))
             write("    vec_free(s);\n")
             write("    }\n")
             return
@@ -275,8 +271,12 @@ class ToJSON:
             "(vl_api_{name}_t *a) {{\n".format(name=o.name)
         )
 
-        write('    u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'.format(o.name))
-        write("    cJSON *o = cJSON_CreateString((char *)s);\n")
+        write(
+            '    char *s = format_c_string(0, "%U", format_vl_api_{}_t, a);\n'.format(
+                o.name
+            )
+        )
+        write("    cJSON *o = cJSON_CreateString(s);\n")
         write("    vec_free(s);\n")
         write("    return o;\n")
         write("}\n")
index cf17b8a..642d3e2 100644 (file)
@@ -833,6 +833,16 @@ done:
   return s;
 }
 
+__clib_export char *
+format_c_string (u8 *s, const char *fmt, ...)
+{
+  va_list args;
+  va_start (args, fmt);
+  s = va_format (s, fmt, &args);
+  va_end (args);
+  vec_add1 (s, '\0');
+  return (char *) s;
+}
 
 /*
  * fd.io coding-style-patch-verification: ON
index a1a70a2..14bac86 100644 (file)
@@ -372,6 +372,8 @@ int test_unformat_main (unformat_input_t * input);
 created circular dependency problems. */
 int test_vec_main (unformat_input_t * input);
 
+char *format_c_string (u8 *s, const char *fmt, ...);
+
 #endif /* included_format_h */
 
 /*
index 1aa3864..73cb947 100644 (file)
@@ -500,12 +500,13 @@ format_vl_api_mac_address_t (u8 * s, va_list * args)
                  mac->bytes[0], mac->bytes[1], mac->bytes[2],
                  mac->bytes[3], mac->bytes[4], mac->bytes[5]);
 }
-#define _(T)                                                \
-  cJSON *vl_api_ ##T## _t_tojson (vl_api_ ##T## _t *a) {   \
-  u8 *s = format(0, "%U", format_vl_api_ ##T## _t, a);      \
-  cJSON *o = cJSON_CreateString((char *)s);                 \
-  vec_free(s);                                              \
-  return o;                                                 \
+#define _(T)                                                                  \
+  cJSON *vl_api_##T##_t_tojson (vl_api_##T##_t *a)                            \
+  {                                                                           \
+    char *s = format_c_string (0, "%U", format_vl_api_##T##_t, a, 0);         \
+    cJSON *o = cJSON_CreateString (s);                                        \
+    vec_free (s);                                                             \
+    return o;                                                                 \
   }
 foreach_type_tojson
 #undef _