On 10/22/2026 2:50 PM, Philippe Mathieu-Daudé wrote:
> Many functions map an enum or exception number to a name, either
> with a switch statement or by indexing a string table after an
> open-coded bounds check. Add STR_ARRAY_ELEM_OR_NULL(array, index),
> which returns array[index], or NULL when index is out of bounds,
> so such code can use a static table and a single call.
> 
> The array is passed as 'const char *const *', so only arrays of
> strings are accepted; hence the STR_ prefix rather than a generic
> name.
> 
> Misuse is caught at build time:
> - pointer instead of array: "negative width in bit-field" error,
>   plus -Wsizeof-pointer-div (GCC);
> - array of non-strings (int[], char[][N], int *[]):
>   -Wincompatible-pointer-types;
> - array of non-const 'char *': -Wincompatible-pointer-types (GCC),
>   -Wincompatible-pointer-types-discards-qualifiers (clang);
> - pointer as index: -Wint-conversion.
> 
> Suggested-by: Pierrick Bouvier <[email protected]>
> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
> ---
>  include/qemu/cutils.h | 17 +++++++++++++++++
>  include/qemu/osdep.h  |  3 +++
>  util/cutils.c         |  6 ++++++
>  3 files changed, 26 insertions(+)
> 

Reviewed-by: Pierrick Bouvier <[email protected]>

Reply via email to