From e2639cb53af0362b9dfb8dc021dc76e872135203 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 6 Nov 2021 00:35:39 +0100 Subject: [PATCH] core/include/kernel_defines.h: Fix index_of() Calculate the size of the element based on the array given, not based on the element pointer. The element might as well be given as a `void *` via a callback. In that case, if the user forgets to cast the `void *` to the array element type, the calculation returns false values. Disarm this foot gun by basing the element size off the given array. --- core/include/kernel_defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/include/kernel_defines.h b/core/include/kernel_defines.h index f52830e669..2d80b8dd90 100644 --- a/core/include/kernel_defines.h +++ b/core/include/kernel_defines.h @@ -71,7 +71,7 @@ extern "C" { * @param[in] ELEMENT pointer to an array element * @return Index of the element in the array */ -#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof(*(ELEMENT))) +#define index_of(ARRAY, ELEMENT) (((uintptr_t)(ELEMENT) - (uintptr_t)(ARRAY)) / sizeof((ARRAY)[0])) /** * @def NORETURN