diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 0b04278ab4..e2b543cb8f 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -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) diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 264189c36b..9283e2013c 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -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) {