From: James Tirta Halim <tirtajames45@gmail.com>
To: linux-hardening@vger.kernel.org, bpf@vger.kernel.org
Cc: James Tirta Halim <tirtajames45@gmail.com>
Subject: [PATCH] strstarts: avoid calling strlen() if first char does not match
Date: Sun, 22 Oct 2023 18:35:47 +0700 [thread overview]
Message-ID: <20231022113547.168081-1-tirtajames45@gmail.com> (raw)
---
include/linux/string.h | 9 ++++++---
tools/bpf/bpftool/gen.c | 2 +-
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index dbfc66400050..1c51039604e7 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -214,7 +214,7 @@ int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
*/
static inline bool strstarts(const char *str, const char *prefix)
{
- return strncmp(str, prefix, strlen(prefix)) == 0;
+ return (*str == *prefix) ? strncmp(str, prefix, strlen(prefix)) == 0 : (*prefix == '\0');
}
size_t memweight(const void *ptr, size_t bytes);
@@ -356,8 +356,11 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
*/
static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
{
- size_t len = strlen(prefix);
- return strncmp(str, prefix, len) == 0 ? len : 0;
+ if (*str == *prefix) {
+ size_t len = strlen(prefix);
+ return strncmp(str, prefix, len) == 0 ? len : 0;
+ }
+ return *prefix == '\0';
}
#endif /* _LINUX_STRING_H_ */
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 2883660d6b67..5f8db7e517bc 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -36,7 +36,7 @@ static void sanitize_identifier(char *name)
static bool str_has_prefix(const char *str, const char *prefix)
{
- return strncmp(str, prefix, strlen(prefix)) == 0;
+ return (*str == *prefix) ? strncmp(str, prefix, strlen(prefix)) == 0 : (*prefix == '\0');
}
static bool str_has_suffix(const char *str, const char *suffix)
--
2.42.0
next reply other threads:[~2023-10-22 11:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-22 11:35 James Tirta Halim [this message]
2023-10-22 13:10 ` [PATCH] strstarts: avoid calling strlen() if first char does not match Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231022113547.168081-1-tirtajames45@gmail.com \
--to=tirtajames45@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox