api: fix [un]formatting in vpp/api/types.c 34/40634/4
authorKlement Sekera <klement.sekera@gmail.com>
Tue, 2 Apr 2024 10:51:10 +0000 (12:51 +0200)
committerMatthew Smith <mgsmith@netgate.com>
Fri, 5 Apr 2024 14:03:40 +0000 (14:03 +0000)
vl_api_prefix_t.len is 1 byte only, but unformat %d writes 4 bytes
add helper functions unformat_u(8|16) which don't write more than
appropriate amount of bytes
fix other similar errors in vpp/api/types.c

Type: fix
Change-Id: I74a61a377147c373f8c25ed083052b2287763c39
Signed-off-by: Klement Sekera <klement.sekera@gmail.com>
src/vpp/api/types.c
src/vppinfra/format.h
src/vppinfra/unformat.c

index a30736f..92bbdb3 100644 (file)
@@ -88,8 +88,7 @@ format_vl_api_prefix (u8 * s, va_list * args)
 {
   const vl_api_prefix_t *pfx = va_arg (*args, vl_api_prefix_t *);
 
-  s = format (s, "%U/%d", format_vl_api_address,
-             &pfx->address, pfx->len);
+  s = format (s, "%U/%u", format_vl_api_address, &pfx->address, pfx->len);
 
   return s;
 }
@@ -106,7 +105,7 @@ u8 *
 format_vl_api_version (u8 * s, va_list * args)
 {
   vl_api_version_t *ver = va_arg (*args, vl_api_version_t *);
-  s = format(s, "%d.%d.%d", ver->major, ver->minor, ver->patch);
+  s = format (s, "%u.%u.%u", ver->major, ver->minor, ver->patch);
   if (ver->pre_release[0] != 0)
   {
     s = format(s, "-%v", ver->pre_release);
@@ -176,13 +175,14 @@ unformat_vl_api_ip6_address (unformat_input_t * input, va_list * args)
 }
 
 uword
-unformat_vl_api_prefix (unformat_input_t * input, va_list * args)
+unformat_vl_api_prefix (unformat_input_t *input, va_list *args)
 {
-   vl_api_prefix_t *pfx = va_arg (*args, vl_api_prefix_t *);
+  vl_api_prefix_t *pfx = va_arg (*args, vl_api_prefix_t *);
+
+  if (unformat (input, "%U/%U", unformat_vl_api_address, &pfx->address,
+               unformat_u8, &pfx->len))
+    return (1);
 
-  if (unformat (input, "%U/%d", unformat_vl_api_address, &pfx->address,
-                &pfx->len))
-      return (1);
   return (0);
 }
 
@@ -191,14 +191,14 @@ unformat_vl_api_mprefix (unformat_input_t * input, va_list * args)
 {
    vl_api_mprefix_t *pfx = va_arg (*args, vl_api_mprefix_t *);
 
-   if (unformat (input, "%U/%d",
-                 unformat_vl_api_ip4_address, &pfx->grp_address.ip4,
-                 &pfx->grp_address_length))
-       pfx->af = ADDRESS_IP4;
-   else if (unformat (input, "%U/%d",
-                 unformat_vl_api_ip6_address, &pfx->grp_address.ip6,
-                 &pfx->grp_address_length))
-       pfx->af = ADDRESS_IP6;
+   if (unformat (input, "%U/%U", unformat_vl_api_ip4_address,
+                &pfx->grp_address.ip4, unformat_u16,
+                &pfx->grp_address_length))
+     pfx->af = ADDRESS_IP4;
+   else if (unformat (input, "%U/%U", unformat_vl_api_ip6_address,
+                     &pfx->grp_address.ip6, unformat_u16,
+                     &pfx->grp_address_length))
+     pfx->af = ADDRESS_IP6;
    else if (unformat (input, "%U %U",
                       unformat_vl_api_ip4_address, &pfx->src_address.ip4,
                       unformat_vl_api_ip4_address, &pfx->grp_address.ip4))
@@ -235,17 +235,14 @@ unformat_vl_api_mprefix (unformat_input_t * input, va_list * args)
 
 uword unformat_vl_api_version (unformat_input_t * input, va_list * args)
 {
-vl_api_version_t *ver = va_arg (*args, vl_api_version_t *);
+  vl_api_version_t *ver = va_arg (*args, vl_api_version_t *);
 
-if (unformat (input, "%d.%d.%d-%s+%s",  ver->major, ver->minor, ver->patch, ver->pre_release, ver->build_metadata
-                ))
-      return (1);
-else if (unformat (input, "%d.%d.%d-%s",  ver->major, ver->minor, ver->patch, ver->pre_release
-                ))
-      return (1);
-else if (unformat (input, "%d.%d.%d",  ver->major, ver->minor, ver->patch
-                ))
-      return (1);
+  if (unformat (input, "%u.%u.%u-%s+%s", ver->major, ver->minor, ver->patch,
+               ver->pre_release, ver->build_metadata) ||
+      unformat (input, "%u.%u.%u-%s", ver->major, ver->minor, ver->patch,
+               ver->pre_release) ||
+      unformat (input, "%u.%u.%u", ver->major, ver->minor, ver->patch))
+    return (1);
 
   return (0);
 }
index 2451173..a1a70a2 100644 (file)
@@ -276,6 +276,12 @@ unformat_init_cstring (unformat_input_t * input, char *string)
 /* Setup for unformat of given vector string; vector will be freed by unformat_string. */
 void unformat_init_vector (unformat_input_t * input, u8 * vector_string);
 
+/* Unformat u8 */
+unformat_function_t unformat_u8;
+
+/* Unformat u16 */
+unformat_function_t unformat_u16;
+
 /* Format function for unformat input usable when an unformat error
    has occurred. */
 u8 *format_unformat_error (u8 * s, va_list * va);
index fe1a46e..5225178 100644 (file)
@@ -1185,6 +1185,31 @@ unformat_double_quoted_string (unformat_input_t *input, va_list *va)
 
 #endif /* CLIB_UNIX */
 
+__clib_export uword
+unformat_u8 (unformat_input_t *input, va_list *args)
+{
+  u8 *d = va_arg (*args, u8 *);
+
+  u32 tmp;
+  if (!unformat (input, "%u", &tmp) || tmp > CLIB_U8_MAX)
+    return 0;
+
+  *d = tmp;
+  return 1;
+}
+
+__clib_export uword
+unformat_u16 (unformat_input_t *input, va_list *args)
+{
+  u16 *d = va_arg (*args, u16 *);
+
+  u32 tmp;
+  if (!unformat (input, "%u", &tmp) || tmp > CLIB_U16_MAX)
+    return 0;
+
+  *d = tmp;
+  return 1;
+}
 
 /*
  * fd.io coding-style-patch-verification: ON