summaryrefslogtreecommitdiff
path: root/src/basic/memory-util.h
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-03-14 12:55:37 +0100
committerLennart Poettering <lennart@poettering.net>2019-03-14 13:25:51 +0100
commit090a9c1eba44eb3ec8d327febdc7722674fc40ba (patch)
tree151850bc48e577165d7b0d12faa8faa48b546e9a /src/basic/memory-util.h
parentcfb4a8494257d2cc44b0347dd59e969dc0e0ef0b (diff)
downloadsystemd-090a9c1eba44eb3ec8d327febdc7722674fc40ba.tar.gz
util: move some raw memory functions from string-util.h → memory-util.h
Diffstat (limited to 'src/basic/memory-util.h')
-rw-r--r--src/basic/memory-util.h26
1 files changed, 26 insertions, 0 deletions
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