From 7c82d0d7d38590b49b7a8cf0dc3272c23b5be807 Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Sat, 24 May 2025 12:33:36 +0100 Subject: [PATCH] libkmod: use strstartswith() over memcmp() In the cases where we have a string literal, we can use our helper without adding performance overhead. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/354 Signed-off-by: Lucas De Marchi --- libkmod/libkmod-config.c | 10 ++++------ libkmod/libkmod-signature.c | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index 38b162d5..9ca9d2f2 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -267,10 +267,9 @@ static int kmod_config_add_softdep(struct kmod_config *config, const char *modna } plen = s - p; - if (plen == strlen("pre:") && memcmp(p, "pre:", strlen("pre:")) == 0) + if (plen == strlen("pre:") && strstartswith(p, "pre:")) mode = S_PRE; - else if (plen == strlen("post:") && - memcmp(p, "post:", strlen("post:")) == 0) + else if (plen == strlen("post:") && strstartswith(p, "post:")) mode = S_POST; else if (*s != '\0' || (*s == '\0' && !was_space)) { if (mode == S_PRE) { @@ -351,10 +350,9 @@ static int kmod_config_add_softdep(struct kmod_config *config, const char *modna } plen = s - p; - if (plen == strlen("pre:") && memcmp(p, "pre:", strlen("pre:")) == 0) + if (plen == strlen("pre:") && strstartswith(p, "pre:")) mode = S_PRE; - else if (plen == strlen("post:") && - memcmp(p, "post:", strlen("post:")) == 0) + else if (plen == strlen("post:") && strstartswith(p, "post:")) mode = S_POST; else if (*s != '\0' || (*s == '\0' && !was_space)) { if (mode == S_PRE) { diff --git a/libkmod/libkmod-signature.c b/libkmod/libkmod-signature.c index 4f5023ed..2cb67f6e 100644 --- a/libkmod/libkmod-signature.c +++ b/libkmod/libkmod-signature.c @@ -314,7 +314,7 @@ bool kmod_module_signature_info(const struct kmod_file *file, if (size < (off_t)strlen(SIG_MAGIC)) return false; size -= strlen(SIG_MAGIC); - if (memcmp(SIG_MAGIC, mem + size, strlen(SIG_MAGIC)) != 0) + if (!strstartswith(mem + size, SIG_MAGIC)) return false; if (size < (off_t)sizeof(struct module_signature)) -- 2.47.3