include: util: Add Z_DETECT_POINTER_OVERFLOW()

The Z_DETECT_POINTER_OVERFLOW() macro is intended detect whether
or not a buffer spans a region of memory that goes beyond the
highest possible address (thereby overflowing the pointer).

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
This commit is contained in:
Peter Mitsis 2023-07-31 15:47:23 -04:00 committed by Carles Cufí
commit 2143502f54

View file

@ -25,6 +25,7 @@
#include <zephyr/types.h> #include <zephyr/types.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
/** @brief Number of bits that make up a type */ /** @brief Number of bits that make up a type */
#define NUM_BITS(t) (sizeof(t) * 8) #define NUM_BITS(t) (sizeof(t) * 8)
@ -588,6 +589,22 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
*/ */
#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x))) #define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
/**
* @brief Determine if a buffer exceeds highest address
*
* This macro determines if a buffer identified by a starting address @a addr
* and length @a buflen spans a region of memory that goes beond the highest
* possible address (thereby resulting in a pointer overflow).
*
* @param addr Buffer starting address
* @param buflen Length of the buffer
*
* @return true if pointer overflow detected, false otherwise
*/
#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
(((buflen) != 0) && \
((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif