]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
memory-util: add mempmem_safe()
authorLennart Poettering <lennart@poettering.net>
Tue, 10 Aug 2021 12:37:00 +0000 (14:37 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 10 Aug 2021 12:55:50 +0000 (14:55 +0200)
This is like memmem_safe() but returns a pointer after the needle,
instead to the beginning of the needle.

This is then used at one place. Not much, but it makes me sleep safer at
night, as it avoids the manual counting done so far.

src/basic/memory-util.h
src/boot/bootctl.c

index 0b04278ab47a6deaa6a7e89732a7fe8a5a78e1e6..e2b543cb8f26a95120dee151388468f347749223 100644 (file)
@@ -71,6 +71,16 @@ static inline void *memmem_safe(const void *haystack, size_t haystacklen, const
         return memmem(haystack, haystacklen, needle, needlelen);
 }
 
+static inline void *mempmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
+        const uint8_t *p;
+
+        p = memmem_safe(haystack, haystacklen, needle, needlelen);
+        if (!p)
+                return NULL;
+
+        return (uint8_t*) p + needlelen;
+}
+
 #if HAVE_EXPLICIT_BZERO
 static inline void* explicit_bzero_safe(void *p, size_t l) {
         if (l > 0)
index 264189c36b93957af37bd1347ff9a7acd6268001..9283e2013cfb0784f55f5790110e8b2cb5433451 100644 (file)
@@ -159,10 +159,9 @@ static int get_file_version(int fd, char **v) {
         if (buf == MAP_FAILED)
                 return log_error_errno(errno, "Failed to memory map EFI binary: %m");
 
-        s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17);
+        s = mempmem_safe(buf, st.st_size - 8, "#### LoaderInfo: ", 17);
         if (!s)
                 goto finish;
-        s += 17;
 
         e = memmem(s, st.st_size - (s - buf), " ####", 5);
         if (!e || e - s < 3) {