diff options
author | Lennart Poettering <lennart@poettering.net> | 2019-03-14 12:55:37 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2019-03-14 13:25:51 +0100 |
commit | 090a9c1eba44eb3ec8d327febdc7722674fc40ba (patch) | |
tree | 151850bc48e577165d7b0d12faa8faa48b546e9a | |
parent | cfb4a8494257d2cc44b0347dd59e969dc0e0ef0b (diff) | |
download | systemd-090a9c1eba44eb3ec8d327febdc7722674fc40ba.tar.gz |
util: move some raw memory functions from string-util.h → memory-util.h
-rw-r--r-- | src/basic/memory-util.c | 18 | ||||
-rw-r--r-- | src/basic/memory-util.h | 26 | ||||
-rw-r--r-- | src/basic/string-util.c | 22 | ||||
-rw-r--r-- | src/basic/string-util.h | 26 | ||||
-rw-r--r-- | src/reply-password/reply-password.c | 1 |
5 files changed, 47 insertions, 46 deletions
diff --git a/src/basic/memory-util.c b/src/basic/memory-util.c index 3b078af20b..5f327ef0d7 100644 --- a/src/basic/memory-util.c +++ b/src/basic/memory-util.c @@ -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 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 diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 93917bc0f0..49c2679e98 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -10,14 +10,15 @@ #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; diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 38070abb22..b5328e0e8a 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -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); diff --git a/src/reply-password/reply-password.c b/src/reply-password/reply-password.c index ee7a0ea130..f8f6c2d3ec 100644 --- a/src/reply-password/reply-password.c +++ b/src/reply-password/reply-password.c @@ -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" |