1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:32:45 +01:00

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.
This commit is contained in:
Benjamin Valentin 2021-11-06 00:35:39 +01:00
parent 4c61a99125
commit e2639cb53a

View File

@ -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