diff options
author | Klement Sekera <klement.sekera@gmail.com> | 2024-04-02 12:51:10 +0200 |
---|---|---|
committer | Matthew Smith <mgsmith@netgate.com> | 2024-04-05 14:03:40 +0000 |
commit | e8fb3cfe892ca1c562d63eb8854386180a297d80 (patch) | |
tree | d98966ff14eaf33cbc4166a303ff40121597700a | |
parent | 37d8c185b8902d9c69859160aee6e34ec72062fd (diff) |
api: fix [un]formatting in vpp/api/types.c
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>
-rw-r--r-- | src/vpp/api/types.c | 49 | ||||
-rw-r--r-- | src/vppinfra/format.h | 6 | ||||
-rw-r--r-- | src/vppinfra/unformat.c | 25 |
3 files changed, 54 insertions, 26 deletions
diff --git a/src/vpp/api/types.c b/src/vpp/api/types.c index a30736fbb9f..92bbdb30ac9 100644 --- a/src/vpp/api/types.c +++ b/src/vpp/api/types.c @@ -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); } diff --git a/src/vppinfra/format.h b/src/vppinfra/format.h index 24511735a53..a1a70a2d64f 100644 --- a/src/vppinfra/format.h +++ b/src/vppinfra/format.h @@ -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); diff --git a/src/vppinfra/unformat.c b/src/vppinfra/unformat.c index fe1a46e4a12..522517888c3 100644 --- a/src/vppinfra/unformat.c +++ b/src/vppinfra/unformat.c @@ -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 |