summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Rosin <peda@axentia.se>2019-07-16 19:27:15 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2019-07-16 22:23:22 -0400
commitb09757104e433447226a95eff4b92583acc0b0fb (patch)
tree35e349432b94a5351aa5dc958b40f3dd337ac897
parent4c6080cd6f8baad9f7faa3deac9a90e59726b119 (diff)
lib/string.c: allow searching for NUL with strnchr
Patch series "lib/string: search for NUL with strchr/strnchr". I noticed an inconsistency where strchr and strnchr do not behave the same with respect to the trailing NUL. strchr is standardised and the kernel function conforms, and the kernel relies on the behavior. So, naturally strchr stays as-is and strnchr is what I change. While writing a few tests to verify that my new strnchr loop was sane, I noticed that the tests for memset16/32/64 had a problem. Since it's all about the lib/string.c file I made a short series of it all... This patch (of 3): strchr considers the terminating NUL to be part of the string, and NUL can thus be searched for with that function. For consistency, do the same with strnchr. Link: http://lkml.kernel.org/r/20190506124634.6807-2-peda@axentia.se Signed-off-by: Peter Rosin <peda@axentia.se> Cc: Matthew Wilcox <willy@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--lib/string.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/string.c b/lib/string.c
index 6016eb3ac73d..461fb620f85f 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -400,6 +400,9 @@ EXPORT_SYMBOL(strncmp);
400 * strchr - Find the first occurrence of a character in a string 400 * strchr - Find the first occurrence of a character in a string
401 * @s: The string to be searched 401 * @s: The string to be searched
402 * @c: The character to search for 402 * @c: The character to search for
403 *
404 * Note that the %NUL-terminator is considered part of the string, and can
405 * be searched for.
403 */ 406 */
404char *strchr(const char *s, int c) 407char *strchr(const char *s, int c)
405{ 408{
@@ -453,12 +456,18 @@ EXPORT_SYMBOL(strrchr);
453 * @s: The string to be searched 456 * @s: The string to be searched
454 * @count: The number of characters to be searched 457 * @count: The number of characters to be searched
455 * @c: The character to search for 458 * @c: The character to search for
459 *
460 * Note that the %NUL-terminator is considered part of the string, and can
461 * be searched for.
456 */ 462 */
457char *strnchr(const char *s, size_t count, int c) 463char *strnchr(const char *s, size_t count, int c)
458{ 464{
459 for (; count-- && *s != '\0'; ++s) 465 while (count--) {
460 if (*s == (char)c) 466 if (*s == (char)c)
461 return (char *)s; 467 return (char *)s;
468 if (*s++ == '\0')
469 break;
470 }
462 return NULL; 471 return NULL;
463} 472}
464EXPORT_SYMBOL(strnchr); 473EXPORT_SYMBOL(strnchr);