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:
parent
eb3a56d8cd
commit
2143502f54
1 changed files with 17 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/** @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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue