]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: move some raw memory functions from string-util.h → memory-util.h
authorLennart Poettering <lennart@poettering.net>
Thu, 14 Mar 2019 11:55:37 +0000 (12:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Mar 2019 12:25:51 +0000 (13:25 +0100)
src/basic/memory-util.c
src/basic/memory-util.h
src/basic/string-util.c
src/basic/string-util.h
src/reply-password/reply-password.c

index 3b078af20bd0953b8c4e02a8c3c5d45f74590518..5f327ef0d7aaacf3abaa6b7f197e6f5d41e533fd 100644 (file)
@@ -37,3 +37,21 @@ bool memeqzero(const void *data, size_t length) {
         /* Now we know first 16 bytes are NUL, memcmp with self.  */
         return memcmp(data, p + i, length) == 0;
 }
+
+#if !HAVE_EXPLICIT_BZERO
+/*
+ * The pointer to memset() is volatile so that compiler must de-reference the pointer and can't assume that
+ * it points to any function in particular (such as memset(), which it then might further "optimize"). This
+ * approach is inspired by openssl's crypto/mem_clr.c.
+ */
+typedef void *(*memset_t)(void *,int,size_t);
+
+static volatile memset_t memset_func = memset;
+
+void* explicit_bzero_safe(void *p, size_t l) {
+        if (l > 0)
+                memset_func(p, '\0', l);
+
+        return p;
+}
+#endif
index 2d74b14a2080505b0d59006e25a7e1da99d07f83..e1e6624d3b0c64b248763e2b7f6c72ce812cf8a1 100644 (file)
@@ -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
index 93917bc0f08ec9b99362be7983ed98275dd4b35a..49c2679e988f3df10d58b1a8596fc3e71d2644e3 100644 (file)
 
 #include "alloc-util.h"
 #include "escape.h"
+#include "fileio.h"
 #include "gunicode.h"
 #include "locale-util.h"
 #include "macro.h"
+#include "memory-util.h"
 #include "string-util.h"
 #include "terminal-util.h"
 #include "utf8.h"
 #include "util.h"
-#include "fileio.h"
 
 int strcmp_ptr(const char *a, const char *b) {
 
@@ -1048,25 +1049,6 @@ int free_and_strndup(char **p, const char *s, size_t l) {
         return 1;
 }
 
-#if !HAVE_EXPLICIT_BZERO
-/*
- * Pointer to memset is volatile so that compiler must de-reference
- * the pointer and can't assume that it points to any function in
- * particular (such as memset, which it then might further "optimize")
- * This approach is inspired by openssl's crypto/mem_clr.c.
- */
-typedef void *(*memset_t)(void *,int,size_t);
-
-static volatile memset_t memset_func = memset;
-
-void* explicit_bzero_safe(void *p, size_t l) {
-        if (l > 0)
-                memset_func(p, '\0', l);
-
-        return p;
-}
-#endif
-
 char* string_erase(char *x) {
         if (!x)
                 return NULL;
index 38070abb22f89ecd2a78702055c6e5840b61aaf0..b5328e0e8ad2b1c4082808017264e9cf8ef287d0 100644 (file)
@@ -184,32 +184,6 @@ int split_pair(const char *s, const char *sep, char **l, char **r);
 int free_and_strdup(char **p, const char *s);
 int free_and_strndup(char **p, const char *s, size_t l);
 
-/* 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
-
 char *string_erase(char *x);
 
 char *string_free_erase(char *s);
index ee7a0ea1305c144930ff74dccec4df7639f57ade..f8f6c2d3ec12573c2a10d399ed42644009721323 100644 (file)
@@ -11,6 +11,7 @@
 #include "fileio.h"
 #include "log.h"
 #include "macro.h"
+#include "memory-util.h"
 #include "socket-util.h"
 #include "string-util.h"
 #include "util.h"