From: Christian Marangi <ansuelsmth@gmail.com>
To: Jens Axboe <axboe@kernel.dk>, Jonathan Corbet <corbet@lwn.net>,
Ulf Hansson <ulf.hansson@linaro.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>, Kees Cook <kees@kernel.org>,
Andy Shevchenko <andy@kernel.org>,
Christian Marangi <ansuelsmth@gmail.com>,
Daniel Golle <daniel@makrotopia.org>,
INAGAKI Hiroshi <musashino.open@gmail.com>,
Christian Brauner <brauner@kernel.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Li Lingfeng <lilingfeng3@huawei.com>,
Ming Lei <ming.lei@redhat.com>,
Christian Heusel <christian@heusel.eu>,
linux-block@vger.kernel.org, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org,
devicetree@vger.kernel.org, linux-hardening@vger.kernel.org,
Miquel Raynal <miquel.raynal@bootlin.com>,
Lorenzo Bianconi <lorenzo@kernel.org>,
upstream@airoha.com
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH v4 3/5] string: add strends() helper to check if a string ends with a suffix
Date: Mon, 30 Sep 2024 13:30:10 +0200 [thread overview]
Message-ID: <20240930113045.28616-4-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20240930113045.28616-1-ansuelsmth@gmail.com>
Add strends() helper to check if a string ends with a suffix. The
unreadable strends is chosen to keep consistency with the parallel
strstarts helper used to check if a string starts with a prefix.
To prevent out-of-bounds read, len of string is checked against the
prefix length before comparing the 2 string at the offset.
Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
include/linux/string.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 0dd27afcfaf7..2c3df6ffb326 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -353,6 +353,19 @@ static inline bool strstarts(const char *str, const char *prefix)
return strncmp(str, prefix, strlen(prefix)) == 0;
}
+/**
+ * strends - does @str end with @suffix?
+ * @str: string to examine
+ * @suffix: suffix to look for.
+ */
+static inline bool strends(const char *str, const char *suffix)
+{
+ size_t n = strlen(str);
+ size_t m = strlen(suffix);
+
+ return n >= m && !memcmp(str + n - m, suffix, m);
+}
+
size_t memweight(const void *ptr, size_t bytes);
/**
--
2.45.2
next prev parent reply other threads:[~2024-09-30 11:31 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-30 11:30 [PATCH v4 0/5] block: partition table OF support Christian Marangi
2024-09-30 11:30 ` [PATCH v4 1/5] block: add support for defining read-only partitions Christian Marangi
2024-09-30 11:30 ` [PATCH v4 2/5] docs: block: Document support for read-only partition in cmdline part Christian Marangi
2024-09-30 11:30 ` Christian Marangi [this message]
2024-09-30 12:45 ` [PATCH v4 3/5] string: add strends() helper to check if a string ends with a suffix Andy Shevchenko
2024-10-01 14:33 ` kernel test robot
2024-10-01 21:47 ` kernel test robot
2024-09-30 11:30 ` [PATCH v4 4/5] block: add support for partition table defined in OF Christian Marangi
2024-09-30 12:49 ` Andy Shevchenko
2024-09-30 11:30 ` [PATCH v4 5/5] dt-bindings: mmc: Document support for partition table in mmc-card Christian Marangi
2024-09-30 12:18 ` Rasmus Villemoes
2024-09-30 12:22 ` Christian Marangi
2024-09-30 14:00 ` Rasmus Villemoes
2024-09-30 12:54 ` [PATCH v4 0/5] block: partition table OF support Andy Shevchenko
2024-10-02 9:20 ` Rasmus Villemoes
2024-10-03 9:59 ` Andy Shevchenko
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=20240930113045.28616-4-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=andy@kernel.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=christian@heusel.eu \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@makrotopia.org \
--cc=devicetree@vger.kernel.org \
--cc=kees@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lilingfeng3@huawei.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mmc@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=lorenzo@kernel.org \
--cc=ming.lei@redhat.com \
--cc=miquel.raynal@bootlin.com \
--cc=musashino.open@gmail.com \
--cc=robh@kernel.org \
--cc=ulf.hansson@linaro.org \
--cc=upstream@airoha.com \
--cc=viro@zeniv.linux.org.uk \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.