diff --git a/include/zephyr/sys/util.h b/include/zephyr/sys/util.h index f352e7b7a80..208d71e4776 100644 --- a/include/zephyr/sys/util.h +++ b/include/zephyr/sys/util.h @@ -25,6 +25,7 @@ #include #include +#include /** @brief Number of bits that make up a type */ #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))) +/** + * @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 } #endif