diff mbox series

strstarts: avoid calling strlen() if first char does not match

Message ID 20231022113547.168081-1-tirtajames45@gmail.com (mailing list archive)
State Handled Elsewhere
Headers show
Series strstarts: avoid calling strlen() if first char does not match | expand

Commit Message

James Tirta Halim Oct. 22, 2023, 11:35 a.m. UTC
---
 include/linux/string.h  | 9 ++++++---
 tools/bpf/bpftool/gen.c | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

Comments

Greg Kroah-Hartman Oct. 22, 2023, 1:10 p.m. UTC | #1
On Sun, Oct 22, 2023 at 06:35:47PM +0700, James Tirta Halim wrote:
> ---
>  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
> 
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- Your patch does not have a Signed-off-by: line.  Please read the
  kernel file, Documentation/process/submitting-patches.rst and resend
  it after adding that line.  Note, the line needs to be in the body of
  the email, before the patch, not at the bottom of the patch or in the
  email signature.

- You did not specify a description of why the patch is needed, or
  possibly, any description at all, in the email body.  Please read the
  section entitled "The canonical patch format" in the kernel file,
  Documentation/process/submitting-patches.rst for what is needed in
  order to properly describe the change.

- You did not write a descriptive Subject: for the patch, allowing Greg,
  and everyone else, to know what this patch is all about.  Please read
  the section entitled "The canonical patch format" in the kernel file,
  Documentation/process/submitting-patches.rst for what a proper
  Subject: line should look like.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot
diff mbox series

Patch

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)