summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOle Troan <otroan@employees.org>2024-08-01 14:06:24 +0200
committerBeno�t Ganne <bganne@cisco.com>2024-08-07 11:46:51 +0000
commit9aa833b144200e88a382f3583196533c95d5e68f (patch)
tree086f8a7e38570d4bcb7e87513711eb423323f917 /src
parentfa7b7a41e7ca9400dda2266a10dd9179be40c128 (diff)
vppapigen: ensure address types are nul terminated
A string generated from format() may not be nul terminated. Type: fix Change-Id: I88452e446c3504d70758e9009c65be5466034d92 Signed-off-by: Ole Troan <otroan@employees.org>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/tools/vppapigen/vppapigen_c.py16
-rw-r--r--src/vppinfra/format.c10
-rw-r--r--src/vppinfra/format.h2
-rw-r--r--src/vppinfra/jsonformat.c13
4 files changed, 27 insertions, 14 deletions
diff --git a/src/tools/vppapigen/vppapigen_c.py b/src/tools/vppapigen/vppapigen_c.py
index c2e1e7da7b7..2cd3c7989b3 100755
--- a/src/tools/vppapigen/vppapigen_c.py
+++ b/src/tools/vppapigen/vppapigen_c.py
@@ -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")
diff --git a/src/vppinfra/format.c b/src/vppinfra/format.c
index cf17b8a1acb..642d3e20654 100644
--- a/src/vppinfra/format.c
+++ b/src/vppinfra/format.c
@@ -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
diff --git a/src/vppinfra/format.h b/src/vppinfra/format.h
index a1a70a2d64f..14bac869f89 100644
--- a/src/vppinfra/format.h
+++ b/src/vppinfra/format.h
@@ -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 */
/*
diff --git a/src/vppinfra/jsonformat.c b/src/vppinfra/jsonformat.c
index 1aa3864be04..73cb94769d8 100644
--- a/src/vppinfra/jsonformat.c
+++ b/src/vppinfra/jsonformat.c
@@ -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 _