[systemd-commits] 2 commits - src/libsystemd src/shared
Lennart Poettering
lennart at kemper.freedesktop.org
Thu May 14 02:32:57 PDT 2015
src/libsystemd/sd-bus/bus-socket.c | 2 +-
src/shared/util.h | 27 +++++++++++++++++++++------
2 files changed, 22 insertions(+), 7 deletions(-)
New commits:
commit 6e6c21c894904d5d9ce75f1a56e4fa5f82b199c1
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu May 14 11:30:59 2015 +0200
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
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
index 94a5c04..24b9c4d 100644
--- a/src/libsystemd/sd-bus/bus-socket.c
+++ b/src/libsystemd/sd-bus/bus-socket.c
@@ -177,7 +177,7 @@ static int bus_socket_auth_verify_client(sd_bus *b) {
/* We expect two response lines: "OK" and possibly
* "AGREE_UNIX_FD" */
- e = memmem(b->rbuffer, b->rbuffer_size, "\r\n", 2);
+ e = memmem_safe(b->rbuffer, b->rbuffer_size, "\r\n", 2);
if (!e)
return 0;
diff --git a/src/shared/util.h b/src/shared/util.h
index 637934f..0c81e3d 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);
commit aeb24f3081ad4971a82d90a9dac4cbe8da3bb228
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu May 14 11:30:14 2015 +0200
util: use GNU comparison function prototype for qsort_safe()
diff --git a/src/shared/util.h b/src/shared/util.h
index 8565fd6..637934f 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -779,12 +779,12 @@ int shall_restore_state(void);
* Normal qsort requires base to be nonnull. Here were require
* that only if nmemb > 0.
*/
-static inline void qsort_safe(void *base, size_t nmemb, size_t size,
- int (*compar)(const void *, const void *)) {
- if (nmemb) {
- assert(base);
- qsort(base, nmemb, size, compar);
- }
+static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar) {
+ if (nmemb <= 0)
+ return;
+
+ assert(base);
+ qsort(base, nmemb, size, compar);
}
int proc_cmdline(char **ret);
More information about the systemd-commits
mailing list