Skip to content

Commit eeefd07

Browse files
peter-mitsiscfriedt
authored andcommitted
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 <[email protected]>
1 parent d013132 commit eeefd07

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/sys/util.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <zephyr/types.h>
2727
#include <stddef.h>
28+
#include <stdint.h>
2829

2930
#ifdef __cplusplus
3031
extern "C" {
@@ -61,6 +62,23 @@ extern "C" {
6162
/** @brief 0 if @p cond is true-ish; causes a compile error otherwise. */
6263
#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
6364

65+
/**
66+
* @brief Determine if a buffer exceeds highest address
67+
*
68+
* This macro determines if a buffer identified by a starting address @a addr
69+
* and length @a buflen spans a region of memory that goes beond the highest
70+
* possible address (thereby resulting in a pointer overflow).
71+
*
72+
* @param addr Buffer starting address
73+
* @param buflen Length of the buffer
74+
*
75+
* @return true if pointer overflow detected, false otherwise
76+
*/
77+
#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
78+
(((buflen) != 0) && \
79+
((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
80+
81+
6482
#if defined(__cplusplus)
6583

6684
/* The built-in function used below for type checking in C is not

0 commit comments

Comments
 (0)