From 090a9c1eba44eb3ec8d327febdc7722674fc40ba Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 14 Mar 2019 12:55:37 +0100 Subject: =?UTF-8?q?util:=20move=20some=20raw=20memory=20functions=20from?= =?UTF-8?q?=20string-util.h=20=E2=86=92=20memory-util.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/basic/memory-util.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/basic/memory-util.h') diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 2d74b14a20..e1e6624d3b 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -51,3 +51,29 @@ static inline void *mempset(void *s, int c, size_t n) { memset(s, c, n); return (uint8_t*)s + n; } + +/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ +static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { + + if (needlelen <= 0) + return (void*) haystack; + + if (haystacklen < needlelen) + return NULL; + + assert(haystack); + assert(needle); + + return memmem(haystack, haystacklen, needle, needlelen); +} + +#if HAVE_EXPLICIT_BZERO +static inline void* explicit_bzero_safe(void *p, size_t l) { + if (l > 0) + explicit_bzero(p, l); + + return p; +} +#else +void *explicit_bzero_safe(void *p, size_t l); +#endif -- cgit v1.2.1