summaryrefslogtreecommitdiff
path: root/src/shared/util.h
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-05-14 11:30:59 +0200
committerLennart Poettering <lennart@poettering.net>2015-05-14 11:32:41 +0200
commit6e6c21c894904d5d9ce75f1a56e4fa5f82b199c1 (patch)
treea8b10465bce587a3369eb5459f6b776a7456bcb6 /src/shared/util.h
parentaeb24f3081ad4971a82d90a9dac4cbe8da3bb228 (diff)
util: introduce memmem_safe() and make use of it
GNU memmem() requires a nonnull first parameter. Let's introduce memmem_safe() that removes this restriction for zero-length parameters, and make use of it where appropriate. http://lists.freedesktop.org/archives/systemd-devel/2015-May/031705.html
Diffstat (limited to 'src/shared/util.h')
-rw-r--r--src/shared/util.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/shared/util.h b/src/shared/util.h
index 637934fecc..0c81e3dc45 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -787,6 +787,21 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_
qsort(base, nmemb, size, compar);
}
+/* 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);
+}
+
int proc_cmdline(char **ret);
int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
int get_proc_cmdline_key(const char *parameter, char **value);