diff options
author | Ole Troan <otroan@employees.org> | 2024-08-01 14:06:24 +0200 |
---|---|---|
committer | Beno�t Ganne <bganne@cisco.com> | 2024-08-07 11:46:51 +0000 |
commit | 9aa833b144200e88a382f3583196533c95d5e68f (patch) | |
tree | 086f8a7e38570d4bcb7e87513711eb423323f917 /src/vppinfra | |
parent | fa7b7a41e7ca9400dda2266a10dd9179be40c128 (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/vppinfra')
-rw-r--r-- | src/vppinfra/format.c | 10 | ||||
-rw-r--r-- | src/vppinfra/format.h | 2 | ||||
-rw-r--r-- | src/vppinfra/jsonformat.c | 13 |
3 files changed, 19 insertions, 6 deletions
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 _ |