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
Hi Christian, kernel test robot noticed the following build errors: [auto build test ERROR on axboe-block/for-next] [also build test ERROR on kees/for-next/hardening robh/for-next lwn/docs-next linus/master v6.12-rc1 next-20241001] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/block-add-support-for-defining-read-only-partitions/20240930-193609 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next patch link: https://lore.kernel.org/r/20240930113045.28616-4-ansuelsmth%40gmail.com patch subject: [PATCH v4 3/5] string: add strends() helper to check if a string ends with a suffix config: s390-randconfig-r061-20241001 (https://download.01.org/0day-ci/archive/20241002/202410020546.DL6BnsOs-lkp@intel.com/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 7773243d9916f98ba0ffce0c3a960e4aa9f03e81) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241002/202410020546.DL6BnsOs-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202410020546.DL6BnsOs-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from arch/s390/purgatory/string.c:3: In file included from arch/s390/purgatory/../lib/string.c:16: >> include/linux/string.h:366:20: error: call to undeclared function 'memcmp'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 366 | return n >= m && !memcmp(str + n - m, suffix, m); | ^ 1 error generated. vim +/memcmp +366 include/linux/string.h 355 356 /** 357 * strends - does @str end with @suffix? 358 * @str: string to examine 359 * @suffix: suffix to look for. 360 */ 361 static inline bool strends(const char *str, const char *suffix) 362 { 363 size_t n = strlen(str); 364 size_t m = strlen(suffix); 365 > 366 return n >= m && !memcmp(str + n - m, suffix, m); 367 } 368 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hi Christian, kernel test robot noticed the following build errors: [auto build test ERROR on axboe-block/for-next] [also build test ERROR on kees/for-next/hardening robh/for-next lwn/docs-next linus/master v6.12-rc1 next-20241001] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/block-add-support-for-defining-read-only-partitions/20240930-193609 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next patch link: https://lore.kernel.org/r/20240930113045.28616-4-ansuelsmth%40gmail.com patch subject: [PATCH v4 3/5] string: add strends() helper to check if a string ends with a suffix config: s390-randconfig-001-20241001 (https://download.01.org/0day-ci/archive/20241001/202410012202.g0GogVZR-lkp@intel.com/config) compiler: s390-linux-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241001/202410012202.g0GogVZR-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202410012202.g0GogVZR-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from arch/s390/purgatory/../lib/string.c:16, from arch/s390/purgatory/string.c:3: include/linux/string.h: In function 'strends': >> include/linux/string.h:366:27: error: implicit declaration of function 'memcmp' [-Wimplicit-function-declaration] 366 | return n >= m && !memcmp(str + n - m, suffix, m); | ^~~~~~ include/linux/string.h:65:1: note: 'memcmp' is defined in header '<string.h>'; this is probably fixable by adding '#include <string.h>' 64 | #include <asm/string.h> +++ |+#include <string.h> 65 | vim +/memcmp +366 include/linux/string.h 355 356 /** 357 * strends - does @str end with @suffix? 358 * @str: string to examine 359 * @suffix: suffix to look for. 360 */ 361 static inline bool strends(const char *str, const char *suffix) 362 { 363 size_t n = strlen(str); 364 size_t m = strlen(suffix); 365 > 366 return n >= m && !memcmp(str + n - m, suffix, m); 367 } 368 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
On Mon, Sep 30, 2024 at 01:30:10PM +0200, Christian Marangi wrote: > 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. strstarts() > To prevent out-of-bounds read, len of string is checked against the > prefix length before comparing the 2 string at the offset. ... > +/** > + * strends - does @str end with @suffix? > + * @str: string to examine > + * @suffix: suffix to look for. Please, run kernel doc validator scripts/kernel-doc -Wall -none ... and fix the warning. > + */ -- With Best Regards, Andy Shevchenko
© 2016 - 2024 Red Hat, Inc.