aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlibapi/api_shared.c
diff options
context:
space:
mode:
authorJakub Grajciar <jgrajcia@cisco.com>2020-02-07 11:30:26 +0100
committerOle Trøan <otroan@employees.org>2020-02-26 08:51:03 +0000
commit2dbee9361e74d03727a8b618ba80a5e28c006011 (patch)
tree443b6c39e99e0e46b62ef0dfd002e31bb1fa7665 /src/vlibapi/api_shared.c
parent8e755a16a71c55555f12381c8a12e22ae7138536 (diff)
api: improve api string safety
- Remove vl_api_from_api_string to prevent use of not nul-terminated strings. - Rename vl_api_from_api_to_vec -> vl_api_from_api_to_new_vec to imply a new vector is created. NOT nul terminated. - Add vl_api_from_api_to_new_c_string. Returns nul terminated string in a new vector. - Add vl_api_c_string_to_api_string. Convert nul terminated string to vl_api_string_t - Add vl_api_vec_to_api_string. Convert NON nul terminated vector to vl_api_string_t Type: fix Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com> Change-Id: Iadd59b612c0d960a34ad0dd07a9d17f56435c6ea Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Diffstat (limited to 'src/vlibapi/api_shared.c')
-rw-r--r--src/vlibapi/api_shared.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/src/vlibapi/api_shared.c b/src/vlibapi/api_shared.c
index aba853dc997..28e9f481650 100644
--- a/src/vlibapi/api_shared.c
+++ b/src/vlibapi/api_shared.c
@@ -1091,15 +1091,19 @@ vl_msg_pop_heap (void *oldheap)
vl_msg_pop_heap_w_region (am->vlib_rp, oldheap);
}
+/* Must be nul terminated */
int
-vl_api_to_api_string (u32 len, const char *buf, vl_api_string_t * str)
+vl_api_c_string_to_api_string (const char *buf, vl_api_string_t * str)
{
- if (len)
+ /* copy without nul terminator */
+ u32 len = strlen (buf);
+ if (len > 0)
clib_memcpy_fast (str->buf, buf, len);
str->length = htonl (len);
return len + sizeof (u32);
}
+/* Must NOT be nul terminated */
int
vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
{
@@ -1109,13 +1113,6 @@ vl_api_vec_to_api_string (const u8 * vec, vl_api_string_t * str)
return len + sizeof (u32);
}
-/* Return a pointer to the API string (not nul terminated */
-u8 *
-vl_api_from_api_string (vl_api_string_t * astr)
-{
- return astr->buf;
-}
-
u32
vl_api_string_len (vl_api_string_t * astr)
{
@@ -1132,15 +1129,32 @@ vl_api_format_string (u8 * s, va_list * args)
/*
* Returns a new vector. Remember to free it after use.
+ * NOT nul terminated.
*/
u8 *
-vl_api_from_api_to_vec (vl_api_string_t * astr)
+vl_api_from_api_to_new_vec (vl_api_string_t * astr)
{
u8 *v = 0;
vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
return v;
}
+/*
+ * Returns a new vector. Remember to free it after use.
+ * Nul terminated.
+ */
+char *
+vl_api_from_api_to_new_c_string (vl_api_string_t * astr)
+{
+ char *v = 0;
+ if (clib_net_to_host_u32 (astr->length) > 0)
+ {
+ vec_add (v, astr->buf, clib_net_to_host_u32 (astr->length));
+ vec_add1 (v, 0);
+ }
+ return v;
+}
+
void
vl_api_set_elog_main (elog_main_t * m)
{