diff options
Diffstat (limited to 'source3/lib/util.c')
-rw-r--r-- | source3/lib/util.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/source3/lib/util.c b/source3/lib/util.c index cf8d49c502..809071662d 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -3124,3 +3124,26 @@ int this_is_smp(void) return 0; #endif } + +/**************************************************************** + Return a safe offset into a buffer, or NULL. +****************************************************************/ + +char *get_safe_offset(const char *buf_base, size_t buf_len, char *ptr, size_t off) +{ + const char *end_base = buf_base + buf_len; + const char *end_ptr = ptr + off; + + if (!buf_base || !ptr) { + return NULL; + } + + if (end_base < buf_base || end_ptr < ptr) { + return NULL; /* wrap. */ + } + + if (end_ptr < end_base) { + return ptr; + } + return NULL; +} |