public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, Tom Zanussi <zanussi@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	"Steven Rostedt (VMware)" <rostedt@goodmis.org>,
	George Guo <guodongtai@kylinos.cn>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Joe Perches <joe@perches.com>,
	Andreas Schwab <schwab@linux-m68k.org>
Subject: [PATCH 4.19 09/18] string.h: Add str_has_prefix() helper function
Date: Thu, 23 May 2024 15:12:32 +0200	[thread overview]
Message-ID: <20240523130326.087206848@linuxfoundation.org> (raw)
In-Reply-To: <20240523130325.727602650@linuxfoundation.org>

4.19-stable review patch.  If anyone has any objections, please let me know.

------------------

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

commit 72921427d46bf9731a1ab7864adc64c43dfae29f upstream.

A discussion came up in the trace triggers thread about converting a
bunch of:

 strncmp(str, "const", sizeof("const") - 1)

use cases into a helper macro. It started with:

	strncmp(str, const, sizeof(const) - 1)

But then Joe Perches mentioned that if a const is not used, the
sizeof() will be the size of a pointer, which can be bad. And that
gcc will optimize strlen("const") into "sizeof("const") - 1".

Thinking about this more, a quick grep in the kernel tree found several
(thousands!) of cases that use this construct. A quick grep also
revealed that there's probably several bugs in that use case. Some are
that people forgot the "- 1" (which I found) and others could be that
the constant for the sizeof is different than the constant (although, I
haven't found any of those, but I also didn't look hard).

I figured the best thing to do is to create a helper macro and place it
into include/linux/string.h. And go around and fix all the open coded
versions of it later.

Note, gcc appears to optimize this when we make it into an always_inline
static function, which removes a lot of issues that a macro produces.

Link: http://lkml.kernel.org/r/e3e754f2bd18e56eaa8baf79bee619316ebf4cfc.1545161087.git.tom.zanussi@linux.intel.com
Link: http://lkml.kernel.org/r/20181219211615.2298e781@gandalf.local.home
Link: http://lkml.kernel.org/r/CAHk-=wg_sR-UEC1ggmkZpypOUYanL5CMX4R7ceuaV4QMf5jBtg@mail.gmail.com

Cc: Tom Zanussi <zanussi@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Suggestions-by: Linus Torvalds <torvalds@linux-foundation.org>
Suggestions-by: Joe Perches <joe@perches.com>
Suggestions-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: George Guo <guodongtai@kylinos.cn>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 include/linux/string.h |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -492,4 +492,24 @@ static inline void memcpy_and_pad(void *
 		memcpy(dest, src, dest_len);
 }
 
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * A common way to test a prefix of a string is to do:
+ *  strncmp(str, prefix, sizeof(prefix) - 1)
+ *
+ * But this can lead to bugs due to typos, or if prefix is a pointer
+ * and not a constant. Instead use str_has_prefix().
+ *
+ * Returns: 0 if @str does not start with @prefix
+         strlen(@prefix) if @str does start with @prefix
+ */
+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;
+}
+
 #endif /* _LINUX_STRING_H_ */



  parent reply	other threads:[~2024-05-23 13:15 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-23 13:12 [PATCH 4.19 00/18] 4.19.315-rc1 review Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 01/18] Revert "selftests: mm: fix map_hugetlb failure on 64K page size systems" Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 02/18] dm: limit the number of targets and parameter size area Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 03/18] btrfs: add missing mutex_unlock in btrfs_relocate_sys_chunks() Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 04/18] tracing: Simplify creation and deletion of synthetic events Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 05/18] tracing: Add unified dynamic event framework Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 06/18] tracing: Use dyn_event framework for synthetic events Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 07/18] tracing: Remove unneeded synth_event_mutex Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 08/18] tracing: Consolidate trace_add/remove_event_call back to the nolock functions Greg Kroah-Hartman
2024-05-23 13:12 ` Greg Kroah-Hartman [this message]
2024-05-23 13:12 ` [PATCH 4.19 10/18] tracing: Use str_has_prefix() helper for histogram code Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 11/18] tracing: Use str_has_prefix() instead of using fixed sizes Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 12/18] tracing: Have the historgram use the result of str_has_prefix() for len of prefix Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 13/18] tracing: Refactor hist trigger action code Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 14/18] tracing: Split up onmatch action data Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 15/18] tracing: Generalize hist trigger onmax and save action Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 16/18] tracing: Remove unnecessary var_ref destroy in track_data_destroy() Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 17/18] serial: kgdboc: Fix NMI-safety problems from keyboard reset code Greg Kroah-Hartman
2024-05-23 13:12 ` [PATCH 4.19 18/18] docs: kernel_include.py: Cope with docutils 0.21 Greg Kroah-Hartman
2024-05-24  6:55 ` [PATCH 4.19 00/18] 4.19.315-rc1 review Harshit Mogalapalli
2024-05-24 11:23 ` Pavel Machek
2024-05-24 13:46 ` Anders Roxell
2024-05-24 15:19 ` Jon Hunter
2024-05-24 16:26 ` Shuah Khan

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=20240523130326.087206848@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=guodongtai@kylinos.cn \
    --cc=joe@perches.com \
    --cc=namhyung@kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=schwab@linux-m68k.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=zanussi@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