From 1787871c1ac0f79fca5b2f4f26366c97c073576a Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Fri, 25 Apr 2025 22:00:42 +0200 Subject: [PATCH] vppinfra: silence clang-tidy unsafe warnings for sizeof (a->b[0]) Type: improvement Change-Id: I23919d5c4e2ca7669053302cf52274cd1960bccc Signed-off-by: Damjan Marion --- src/vppinfra/vec.h | 20 +++++++++++--------- src/vppinfra/vec_bootstrap.h | 5 +++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/vppinfra/vec.h b/src/vppinfra/vec.h index 1a64a69a1e6..053c5b07aa2 100644 --- a/src/vppinfra/vec.h +++ b/src/vppinfra/vec.h @@ -446,8 +446,8 @@ _vec_dup (void *v, uword hdr_size, uword align, uword elt_sz) @param DST destination @param SRC source */ -#define vec_copy(DST,SRC) clib_memcpy_fast (DST, SRC, vec_len (DST) * \ - sizeof ((DST)[0])) +#define vec_copy(DST, SRC) \ + clib_memcpy_fast (DST, SRC, vec_len (DST) * _vec_elt_sz (DST)) /** \brief Clone a vector. Make a new vector with the same size as a given vector but possibly with a different type. @@ -480,7 +480,7 @@ _vec_zero_elts (void *v, uword first, uword count, uword elt_sz) { clib_memset_u8 (v + (first * elt_sz), 0, count * elt_sz); } -#define vec_zero_elts(V, F, C) _vec_zero_elts (V, F, C, sizeof ((V)[0])) +#define vec_zero_elts(V, F, C) _vec_zero_elts (V, F, C, _vec_elt_sz (V)) static_always_inline void _vec_validate (void **vp, uword index, uword header_size, uword align, @@ -518,7 +518,7 @@ _vec_validate (void **vp, uword index, uword header_size, uword align, } #define vec_validate_hap(V, I, H, A, P) \ - _vec_validate ((void **) &(V), I, H, _vec_align (V, A), 0, sizeof ((V)[0])) + _vec_validate ((void **) &(V), I, H, _vec_align (V, A), 0, _vec_elt_sz (V)) /** \brief Make sure vector is long enough for given index (no header, unspecified alignment) @@ -1228,11 +1228,13 @@ _vec_is_equal (void *v1, void *v2, uword v1_elt_sz, uword v2_elt_sz) @param vec vector to sort @param f comparison function */ -#define vec_sort_with_function(vec,f) \ -do { \ - if (vec_len (vec) > 1) \ - qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ -} while (0) +#define vec_sort_with_function(vec, f) \ + do \ + { \ + if (vec_len (vec) > 1) \ + qsort (vec, vec_len (vec), _vec_elt_sz (vec), (void *) (f)); \ + } \ + while (0) /** \brief Make a vector containing a NULL terminated c-string. diff --git a/src/vppinfra/vec_bootstrap.h b/src/vppinfra/vec_bootstrap.h index 5d386b1eaad..a4e07511426 100644 --- a/src/vppinfra/vec_bootstrap.h +++ b/src/vppinfra/vec_bootstrap.h @@ -83,7 +83,8 @@ always_inline uword __vec_elt_sz (uword elt_sz, int is_void); (((s) + sizeof (uword) - 1) &~ (sizeof (uword) - 1)) #define _vec_is_void(P) \ __builtin_types_compatible_p (__typeof__ ((P)[0]), void) -#define _vec_elt_sz(V) __vec_elt_sz (sizeof ((V)[0]), _vec_is_void (V)) +#define _vec_elt_sz(V) \ + __vec_elt_sz (sizeof ((V)[0]), _vec_is_void (V)) /* NOLINT */ #define _vec_align(V, A) __vec_align (__alignof__((V)[0]), A) always_inline __clib_nosanitize_addr uword @@ -136,7 +137,7 @@ u32 vec_len_not_inline (void *v); /** \brief Number of data bytes in vector. */ -#define vec_bytes(v) (vec_len (v) * sizeof (v[0])) +#define vec_bytes(v) (vec_len (v) * _vec_elt_sz (v)) /** * Return size of memory allocated for the vector -- 2.16.6